From 4faefca342e3191900485e06b3008baf891d999e Mon Sep 17 00:00:00 2001 From: Martin Eiber Date: Wed, 6 Nov 2024 07:29:59 +0100 Subject: [PATCH] [Grid] [Filter] Add filter for system.id (#535) * Add Filter for id * Apply php-cs-fixer changes * Fix Test * Fix Test * use is_int(). * Apply php-cs-fixer changes --------- Co-authored-by: martineiber --- config/data_index_filters.yaml | 3 ++ config/grid.yaml | 3 ++ doc/03_Grid.md | 1 + src/DataIndex/Filter/IdFilter.php | 50 ++++++++++++++++++ src/Grid/Column/ColumnType.php | 1 + .../Column/Definition/System/IdDefinition.php | 52 +++++++++++++++++++ src/Grid/Mapper/ColumnMapper.php | 2 +- tests/Unit/Grid/Mapper/ColumnMapperTest.php | 2 +- .../Grid/Service/SystemColumnServiceTest.php | 4 +- 9 files changed, 114 insertions(+), 4 deletions(-) create mode 100644 src/DataIndex/Filter/IdFilter.php create mode 100644 src/Grid/Column/Definition/System/IdDefinition.php diff --git a/config/data_index_filters.yaml b/config/data_index_filters.yaml index 7d2cd1067..875a09677 100644 --- a/config/data_index_filters.yaml +++ b/config/data_index_filters.yaml @@ -41,6 +41,9 @@ services: Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\UserFilter: tags: [ 'pimcore.studio_backend.open_search.filter' ] + Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\IdFilter: + tags: [ 'pimcore.studio_backend.open_search.filter' ] + # DataObject Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter\DataObject\ClassNameFilter: tags: [ 'pimcore.studio_backend.open_search.data_object.filter' ] diff --git a/config/grid.yaml b/config/grid.yaml index 3bfe3a57a..dc872da3a 100644 --- a/config/grid.yaml +++ b/config/grid.yaml @@ -75,6 +75,9 @@ services: Pimcore\Bundle\StudioBackendBundle\Grid\Column\Definition\System\IntegerDefinition: tags: [ 'pimcore.studio_backend.grid_column_definition' ] + Pimcore\Bundle\StudioBackendBundle\Grid\Column\Definition\System\IdDefinition: + tags: [ 'pimcore.studio_backend.grid_column_definition' ] + Pimcore\Bundle\StudioBackendBundle\Grid\Column\Definition\System\StringDefinition: tags: [ 'pimcore.studio_backend.grid_column_definition' ] diff --git a/doc/03_Grid.md b/doc/03_Grid.md index ca02c8754..2e7a82288 100644 --- a/doc/03_Grid.md +++ b/doc/03_Grid.md @@ -43,6 +43,7 @@ Available filters are: | system.datetime | integer | `from`, `to`, or `on` | true | | system.tag | object | `considerChildTags`, `tags` | false | | system.pql | string | PQL Query | false | +| system.id | integer | | false | diff --git a/src/DataIndex/Filter/IdFilter.php b/src/DataIndex/Filter/IdFilter.php new file mode 100644 index 000000000..15247aaf3 --- /dev/null +++ b/src/DataIndex/Filter/IdFilter.php @@ -0,0 +1,50 @@ +getSimpleColumnFilterByType(ColumnType::SYSTEM_ID->value); + + if (!$filter) { + return $query; + } + + if (!is_int($filter->getFilterValue())) { + throw new InvalidArgumentException('Filter value for this filter must be a integer'); + } + + $query->searchByIds([$filter->getFilterValue()]); + + return $query; + } +} diff --git a/src/Grid/Column/ColumnType.php b/src/Grid/Column/ColumnType.php index 78bbd53ec..fec5ee0ab 100644 --- a/src/Grid/Column/ColumnType.php +++ b/src/Grid/Column/ColumnType.php @@ -24,6 +24,7 @@ enum ColumnType: string case SYSTEM_STRING = 'system.string'; case SYSTEM_FILE_SIZE = 'system.fileSize'; case SYSTEM_INTEGER = 'system.integer'; + case SYSTEM_ID = 'system.id'; case SYSTEM_DATETIME = 'system.datetime'; case SYSTEM_TAG = 'system.tag'; case SYSTEM_PQL_QUERY = 'system.pql'; diff --git a/src/Grid/Column/Definition/System/IdDefinition.php b/src/Grid/Column/Definition/System/IdDefinition.php new file mode 100644 index 000000000..fe7c08aa5 --- /dev/null +++ b/src/Grid/Column/Definition/System/IdDefinition.php @@ -0,0 +1,52 @@ +value; + } + + public function getConfig(mixed $config): array + { + return []; + } + + public function isSortable(): bool + { + return true; + } + + public function getFrontendType(): string + { + return FrontendType::INPUT->value; + } + + public function isExportable(): bool + { + return true; + } +} diff --git a/src/Grid/Mapper/ColumnMapper.php b/src/Grid/Mapper/ColumnMapper.php index b2009f9ff..540512777 100644 --- a/src/Grid/Mapper/ColumnMapper.php +++ b/src/Grid/Mapper/ColumnMapper.php @@ -27,7 +27,7 @@ { private const COLUMN_MAPPING = [ 'preview' => 'image', - 'id' => 'integer', + 'id' => 'id', 'type' => 'string', 'fullpath' => 'string', 'filename' => 'string', diff --git a/tests/Unit/Grid/Mapper/ColumnMapperTest.php b/tests/Unit/Grid/Mapper/ColumnMapperTest.php index f5f88cfac..ba419db0a 100644 --- a/tests/Unit/Grid/Mapper/ColumnMapperTest.php +++ b/tests/Unit/Grid/Mapper/ColumnMapperTest.php @@ -43,7 +43,7 @@ public function testMapperForPreview(): void public function testMapperForId(): void { $mapper = new ColumnMapper(); - $this->assertSame('integer', $mapper->getType('id')); + $this->assertSame('id', $mapper->getType('id')); } public function testMapperForType(): void diff --git a/tests/Unit/Grid/Service/SystemColumnServiceTest.php b/tests/Unit/Grid/Service/SystemColumnServiceTest.php index a4c7a41ec..86e934391 100644 --- a/tests/Unit/Grid/Service/SystemColumnServiceTest.php +++ b/tests/Unit/Grid/Service/SystemColumnServiceTest.php @@ -32,7 +32,7 @@ public function testGetSystemColumnsForAssets(): void $this->assertSame([ 'preview' => 'image', - 'id' => 'integer', + 'id' => 'id', 'type' => 'string', 'fullpath' => 'string', 'filename' => 'string', @@ -48,7 +48,7 @@ public function testGetSystemColumnsForDataObjects(): void $systemColumnService = new SystemColumnService($mapper); $this->assertSame([ - 'id' => 'integer', + 'id' => 'id', 'fullpath' => 'string', 'key' => 'string', 'published' => 'boolean',