Skip to content

Commit

Permalink
Add accessor property instead of code
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Feb 4, 2021
1 parent 49a37be commit fa3328a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 3 deletions.
4 changes: 4 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ UPGRADE 3.x
UPGRADE FROM 3.xx to 3.xx
=========================

### Deprecated the `Sonata\AdminBundle\AdminFieldDescription` `'code'` option.

Use the `accessor` option instead.

### Deprecated `Sonata\AdminBundle\Admin\AbstractAdmin::formOptions` property.

This property has been replaced by the new method `Sonata\AdminBundle\Admin\AbstractAdmin::configureFormOptions()`
Expand Down
10 changes: 10 additions & 0 deletions docs/reference/action_list.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ Here is an example::
// specific properties of a relation to the entity
->add('image.name')

// you may also use a custom accessor
->add('description1', null, [
'accessor' => 'getDescription'
])
->add('description2', null, [
'accessor' => function ($subject) {
return $this->customService->formatDescription($subject);
}
])

// You may also specify the actions you want to be displayed in the list
->add('_action', null, [
'actions' => [
Expand Down
20 changes: 17 additions & 3 deletions src/Admin/BaseFieldDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* - name (o) : the name used (label in the form, title in the list)
* - link_parameters (o) : add link parameter to the related Admin class when
* the Admin.generateUrl is called
* - code : the method name to retrieve the related value
* - accessor : the method or the method name to retrieve the related value
* - associated_tostring : (deprecated, use associated_property option)
* the method to retrieve the "string" representation
* of the collection element.
Expand Down Expand Up @@ -418,9 +418,23 @@ public function getFieldValue($object, $fieldName)
return $this->getFieldValue($child, substr($fieldName, $dotPos + 1));
}

// prefer method name given in the code option
// NEXT_MAJOR: Remove this code.
if ($this->getOption('code')) {
$getter = $this->getOption('code');
@trigger_error(
'The "code" option is deprecated since sonata-project/admin-bundle 3.x.'
.' Use the "accessor" code instead',
\E_USER_DEPRECATED
);
}

// prefer method name given in the code option
// NEXT_MAJOR: Remove this line and uncomment the following
$getter = $this->getOption('accessor', $this->getOption('code'));
// $getter = $this->getOption('accessor');
if ($getter) {
if (\is_callable($getter)) {
return $getter($object);
}

if (!method_exists($object, $getter)) {
@trigger_error(
Expand Down
25 changes: 25 additions & 0 deletions tests/Admin/BaseFieldDescriptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,31 @@ public function testGetFieldValueWithNullObject(): void
$this->assertNull($description->getFieldValue(null, 'fake'));
}

public function testGetFieldValueWithAccessor(): void
{
$description = new FieldDescription('name', ['accessor' => 'getFoo']);
$mock = $this->getMockBuilder(\stdClass::class)->addMethods(['getFoo'])->getMock();
$mock->expects($this->once())->method('getFoo')->willReturn(42);
$this->assertSame(42, $description->getFieldValue($mock, 'fake'));
}

public function testGetFieldValueWithCallableAccessor(): void
{
$description = new FieldDescription('name', [
'accessor' => static function (object $object): int {
return $object->getFoo();
},
]);
$mock = $this->getMockBuilder(\stdClass::class)->addMethods(['getFoo'])->getMock();
$mock->expects($this->once())->method('getFoo')->willReturn(42);
$this->assertSame(42, $description->getFieldValue($mock, 'fake'));
}

/**
* NEXT_MAJOR: Remove this test.
*
* @group legacy
*/
public function testGetFieldValueWithCode(): void
{
$description = new FieldDescription('name', ['code' => 'getFoo']);
Expand Down

0 comments on commit fa3328a

Please sign in to comment.