Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 68 additions & 10 deletions src/Admin/BaseFieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Inflector\Inflector;
use Sonata\AdminBundle\Exception\NoValueException;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* A FieldDescription hold the information about a field. A typical
Expand Down Expand Up @@ -126,11 +127,75 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface
*/
protected $help;

/**
* @var OptionsResolver
*/
protected $resolver;

/**
* @var array[] cached object field getters
*/
private static $fieldGetters = [];

public function __construct()
{
$this->resolver = new OptionsResolver();
$this->resolver
->setDefaults([
'placeholder' => 'short_object_description_placeholder',
'link_parameters' => [],
])
->setDefined([
'actions',
'associated_property',
'code',
'field_name',
'field_options',
'field_type',
'header_class',
'header_style',
'help',
'identifier',
'label',
'parameters',
'role',
'route',
'safe',
'sort_field_mapping',
'sort_parent_association_mappings',
'sortable',
'template',
'translation_domain',
'type',
'virtual_field',
])
->setAllowedTypes('actions', ['array[]'])
->setAllowedTypes('associated_property', 'string')
->setAllowedTypes('code', 'string')
->setAllowedTypes('field_name', 'string')
->setAllowedTypes('field_options', 'array')
->setAllowedTypes('field_type', 'string')
->setAllowedTypes('header_class', 'string')
->setAllowedTypes('header_style', 'string')
->setAllowedTypes('help', 'string')
->setAllowedTypes('identifier', 'bool')
->setAllowedTypes('label', ['bool', 'null', 'string'])
->setAllowedTypes('link_parameters', 'array')
->setAllowedTypes('parameters', 'array')
->setAllowedTypes('placeholder', 'string')
->setAllowedTypes('role', ['string', 'string[]'])
->setAllowedTypes('route', 'array')
->setAllowedTypes('safe', 'bool')
->setAllowedTypes('sort_field_mapping', 'array')
->setAllowedTypes('sort_parent_association_mappings', 'array')
->setAllowedTypes('sortable', 'bool')
->setAllowedTypes('template', 'string')
->setAllowedTypes('translation_domain', 'string')
->setAllowedTypes('type', 'string')
->setAllowedTypes('virtual_field', 'bool')
->setDeprecated('header_style', 'The option "header_style" is deprecated since sonata-project/admin-bundle 3.52 and will be removed in version 4.x. Use "header_class" instead.');
}

public function setFieldName($fieldName): void
{
$this->fieldName = $fieldName;
Expand Down Expand Up @@ -162,11 +227,13 @@ public function getOption($name, $default = null)

public function setOption($name, $value): void
{
$this->options[$name] = $value;
$this->options[$name] = $this->resolver->resolve([$name => $value])[$name];
}

public function setOptions(array $options): void
{
$options = $this->resolver->resolve($options);

// set the type if provided
if (isset($options['type'])) {
$this->setType($options['type']);
Expand All @@ -185,15 +252,6 @@ public function setOptions(array $options): void
unset($options['help']);
}

// set default placeholder
if (!isset($options['placeholder'])) {
$options['placeholder'] = 'short_object_description_placeholder';
}

if (!isset($options['link_parameters'])) {
$options['link_parameters'] = [];
}

$this->options = $options;
}

Expand Down
8 changes: 4 additions & 4 deletions tests/Datagrid/ListMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ public function testAutoSortOnAssociatedProperty(): void
[
'associated_property' => 'fooAssociatedProperty',
'sortable' => false,
'sort_parent_association_mappings' => 'fooSortParentAssociationMapping',
'sort_field_mapping' => 'fooSortFieldMapping',
'sort_parent_association_mappings' => [['fieldName' => 'fooSortParentAssociationMapping']],
'sort_field_mapping' => ['fieldName' => 'fooSortFieldMapping'],
]
);

Expand All @@ -299,8 +299,8 @@ public function testAutoSortOnAssociatedProperty(): void

$this->assertSame('fooAssociatedProperty', $fieldManualSort->getOption('associated_property'));
$this->assertFalse($fieldManualSort->getOption('sortable'));
$this->assertSame('fooSortParentAssociationMapping', $fieldManualSort->getOption('sort_parent_association_mappings'));
$this->assertSame('fooSortFieldMapping', $fieldManualSort->getOption('sort_field_mapping'));
$this->assertSame([['fieldName' => 'fooSortParentAssociationMapping']], $fieldManualSort->getOption('sort_parent_association_mappings'));
$this->assertSame(['fieldName' => 'fooSortFieldMapping'], $fieldManualSort->getOption('sort_field_mapping'));
}

public function testKeys(): void
Expand Down