Skip to content

Commit

Permalink
Merge 46ed31b into 19f26b7
Browse files Browse the repository at this point in the history
  • Loading branch information
alterphp committed Sep 3, 2018
2 parents 19f26b7 + 46ed31b commit cb1afba
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 1 deletion.
37 changes: 36 additions & 1 deletion README.md
Expand Up @@ -228,12 +228,47 @@ Easy configurable with custom list actions by adding a `confirm` key :
```yaml
easyadmin:
entities:
User
User:
list:
actions:
- { name: disable, icon: ban, title: Disable user, label: false, target: _blank, confirm: User will lose any access to the platform ! }
```

### Exclude fields in forms

```yaml
easyadmin:
entities:
User:
form:
exclude_fields: ['references']
```

In such entity:

```php
<?php

class User
{
public $name;

public $title;

public $references;
}
```

It will show all fields but those mentioned in `exclude_fields`, equivalent to the following configuration:

```yaml
easyadmin:
entities:
User:
form:
fields: ['name', 'title']
```

### Use template show vertical boostrap

Design EasyAdmin configuration:
Expand Down
104 changes: 104 additions & 0 deletions src/Configuration/ExcludeFieldsConfigPass.php
@@ -0,0 +1,104 @@
<?php

namespace AlterPHP\EasyAdminExtensionBundle\Configuration;

use AlterPHP\EasyAdminExtensionBundle\Exception\ConflictingConfigurationException;
use EasyCorp\Bundle\EasyAdminBundle\Configuration\ConfigPassInterface;
use ReflectionClass;
use ReflectionProperty;

/**
* Adds "exclude_fields" option to forms
*
* ```yaml
* form:
* fields: ['name', 'perex', 'description', 'duration', 'capacity']
* ↓
* exclude_fields: ['trainingReferences']
* ```
*/
class ExcludeFieldsConfigPass implements ConfigPassInterface
{
/**
* @param mixed[] $backendConfig
*
* @return mixed[]
*/
public function process(array $backendConfig): array
{
if (!isset($backendConfig['entities'])) {
return $backendConfig;
}

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

$this->ensureFieldConfigurationIsValid($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;
}

$fields[] = $propertyName;
}

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

return $backendConfig;
}

/**
* @param string[] $excludedFields
*/
private function shouldSkipField(string $propertyName, array $excludedFields): bool
{
if ('id' === $propertyName) {
return true;
}

return in_array($propertyName, $excludedFields, true);
}

/**
* Explicit "fields" option and "exclude_fields" won't work together
*
* @param mixed[] $entityConfig
*/
private function ensureFieldConfigurationIsValid(array $entityConfig, string $entityName)
{
if (!isset($entityConfig['form']['fields']) || !count($entityConfig['form']['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)
));
}

/**
* @param mixed[] $entityConfig
*
* @return string[]
*/
private function getPropertyNamesForEntity(array $entityConfig, string $entityName): array
{
$entityClass = $entityConfig['class'] ?: $entityName;
$entityReflectionClass = new ReflectionClass($entityClass);

return array_map(function (ReflectionProperty $reflectionProperty) {
return $reflectionProperty->getName();
}, $entityReflectionClass->getProperties());
}
}
11 changes: 11 additions & 0 deletions src/Exception/ConflictingConfigurationException.php
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace AlterPHP\EasyAdminExtensionBundle\Exception;

use Exception;

final class ConflictingConfigurationException extends Exception
{
}
10 changes: 10 additions & 0 deletions tests/Configuration/ExcludeFieldsConfigPassSource/DummyEntity.php
@@ -0,0 +1,10 @@
<?php declare(strict_types=1);

namespace AlterPHP\EasyAdminExtensionBundle\Tests\Configuration\ExcludeFieldsConfigPassSource;

final class DummyEntity
{
public $name;

public $exclude;
}
49 changes: 49 additions & 0 deletions tests/Configuration/ExcludeFieldsConfigPassTest.php
@@ -0,0 +1,49 @@
<?php

namespace AlterPHP\EasyAdminExtensionBundle\Tests\Configuration;

use AlterPHP\EasyAdminExtensionBundle\Configuration\ExcludeFieldsConfigPass;
use AlterPHP\EasyAdminExtensionBundle\Tests\Configuration\ExcludeFieldsConfigPassSource\DummyEntity;

class ExcludeFieldsConfigPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ExcludeFieldsConfigPass
*/
private $excludeFieldsConfigPass;

protected function setUp()
{
$this->excludeFieldsConfigPass = new ExcludeFieldsConfigPass();
}

public function test()
{
$backendConfig = array(
'entities' => array(
'TestEntity' => array(
'class' => DummyEntity::class,
'form' => array(
'exclude_fields' => array('exclude')
),
),
),
);

$processedBackendConfig = $this->excludeFieldsConfigPass->process($backendConfig);

$expectedBackendConfig = array(
'entities' => array(
'TestEntity' => array(
'class' => DummyEntity::class,
'form' => array(
'exclude_fields' => array('exclude'),
'fields' => array('name')
),
),
),
);

$this->assertSame($processedBackendConfig, $expectedBackendConfig);
}
}

0 comments on commit cb1afba

Please sign in to comment.