From 14f439d7bd14da9d5fc0d32280d5fb91cde0d4bd Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Tue, 8 Dec 2020 16:21:57 +0100 Subject: [PATCH] Disable sorting for an associated_property callable --- src/Datagrid/ListMapper.php | 2 +- tests/Datagrid/ListMapperTest.php | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Datagrid/ListMapper.php b/src/Datagrid/ListMapper.php index a0fdbafafb..70777067b2 100644 --- a/src/Datagrid/ListMapper.php +++ b/src/Datagrid/ListMapper.php @@ -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'] = [[ diff --git a/tests/Datagrid/ListMapperTest.php b/tests/Datagrid/ListMapperTest.php index 65c4da7ad3..7b787e5e4a 100644 --- a/tests/Datagrid/ListMapperTest.php +++ b/tests/Datagrid/ListMapperTest.php @@ -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');