Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable sorting for an associated_property callable #6675

Merged
merged 1 commit into from
Dec 9, 2020
Merged
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
2 changes: 1 addition & 1 deletion src/Datagrid/ListMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function add($name, $type = null, array $fieldDescriptionOptions = [])
// Default sort on "associated_property"
if (isset($fieldDescriptionOptions['associated_property'])) {
if (!isset($fieldDescriptionOptions['sortable'])) {
$fieldDescriptionOptions['sortable'] = true;
$fieldDescriptionOptions['sortable'] = !\is_callable($fieldDescriptionOptions['associated_property']);
}
if (!isset($fieldDescriptionOptions['sort_parent_association_mappings'])) {
$fieldDescriptionOptions['sort_parent_association_mappings'] = [[
Expand Down
26 changes: 26 additions & 0 deletions tests/Datagrid/ListMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,32 @@ public function testAutoSortOnAssociatedProperty(): void
$this->assertSame('fooSortFieldMapping', $fieldManualSort->getOption('sort_field_mapping'));
}

public function testCallableAssociationPropertyCannotBeSortable(): void
{
$this->listMapper->add(
'fooNameNotSortable',
null,
[
'associated_property' => static function ($value) {
return (string) $value;
},
]
);
$this->listMapper->add(
'fooNameSortable',
null,
[
'associated_property' => 'fooProperty',
]
);

$fieldSortable = $this->listMapper->get('fooNameSortable');
$fieldNotSortable = $this->listMapper->get('fooNameNotSortable');

$this->assertTrue($fieldSortable->getOption('sortable'));
$this->assertFalse($fieldNotSortable->getOption('sortable'));
}

public function testKeys(): void
{
$fieldDescription1 = $this->getFieldDescriptionMock('fooName1', 'fooLabel1');
Expand Down