Skip to content

Commit

Permalink
Extends exclude_fields to edit and new sections
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Sep 3, 2018
1 parent 46ed31b commit a07a296
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions src/Configuration/ExcludeFieldsConfigPass.php
Expand Up @@ -31,26 +31,28 @@ public function process(array $backendConfig): array
}

foreach ($backendConfig['entities'] as $entityName => $entityConfig) {
if (!isset($entityConfig['form']['exclude_fields'])) {
continue;
}
foreach (array('form', 'edit', 'new') as $section) {
if (!isset($entityConfig[$section]['exclude_fields'])) {
continue;
}

$this->ensureFieldConfigurationIsValid($entityConfig, $entityName);
$this->ensureFieldConfigurationIsValid($entityConfig, $entityName, $section);

$propertyNames = $this->getPropertyNamesForEntity($entityConfig, $entityName);
$propertyNames = $this->getPropertyNamesForEntity($entityConfig, $entityName);

// filter fields to be displayed
$fields = [];
foreach ($propertyNames as $propertyName) {
if ($this->shouldSkipField($propertyName, $entityConfig['form']['exclude_fields'])) {
continue;
// filter fields to be displayed
$fields = [];
foreach ($propertyNames as $propertyName) {
if ($this->shouldSkipField($propertyName, $entityConfig[$section]['exclude_fields'])) {
continue;
}

$fields[] = $propertyName;
}

$fields[] = $propertyName;
// set it!
$backendConfig['entities'][$entityName][$section]['fields'] = $fields;
}

// set it!
$backendConfig['entities'][$entityName]['form']['fields'] = $fields;
}

return $backendConfig;
Expand All @@ -72,18 +74,20 @@ private function shouldSkipField(string $propertyName, array $excludedFields): b
* Explicit "fields" option and "exclude_fields" won't work together
*
* @param mixed[] $entityConfig
* @param string $entityName
* @param string $section
*/
private function ensureFieldConfigurationIsValid(array $entityConfig, string $entityName)
private function ensureFieldConfigurationIsValid(array $entityConfig, string $entityName, string $section)
{
if (!isset($entityConfig['form']['fields']) || !count($entityConfig['form']['fields'])) {
if (!isset($entityConfig[$section]['fields']) || !count($entityConfig[$section]['fields'])) {
return;
}

throw new ConflictingConfigurationException(sprintf(
'"%s" and "%s" are mutually conflicting. Pick just one of them in %s YAML configuration',
'exclude_fields',
'fields',
sprintf('easy_admin_bundle > entities > %s > form', $entityName)
sprintf('easy_admin > entities > %s > %s', $entityName, $section)
));
}

Expand Down

0 comments on commit a07a296

Please sign in to comment.