Skip to content

Commit

Permalink
[Grid] [Filter] Add filter for system.id (#535)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
martineiber and martineiber authored Nov 6, 2024
1 parent 8c5d797 commit 4faefca
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 4 deletions.
3 changes: 3 additions & 0 deletions config/data_index_filters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]
Expand Down
3 changes: 3 additions & 0 deletions config/grid.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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' ]

Expand Down
1 change: 1 addition & 0 deletions doc/03_Grid.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |



Expand Down
50 changes: 50 additions & 0 deletions src/DataIndex/Filter/IdFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\DataIndex\Filter;

use Pimcore\Bundle\StudioBackendBundle\DataIndex\Query\QueryInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\InvalidArgumentException;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnType;
use Pimcore\Bundle\StudioBackendBundle\MappedParameter\Filter\SimpleColumnFiltersParameterInterface;
use function is_int;

/**
* @internal
*/
final class IdFilter implements FilterInterface
{
public function apply(mixed $parameters, QueryInterface $query): QueryInterface
{
if (!$parameters instanceof SimpleColumnFiltersParameterInterface) {
return $query;
}

$filter = $parameters->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;
}
}
1 change: 1 addition & 0 deletions src/Grid/Column/ColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
52 changes: 52 additions & 0 deletions src/Grid/Column/Definition/System/IdDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Grid\Column\Definition\System;

use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnDefinitionInterface;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\ColumnType;
use Pimcore\Bundle\StudioBackendBundle\Grid\Column\FrontendType;

/**
* @internal
*/
final readonly class IdDefinition implements ColumnDefinitionInterface
{
public function getType(): string
{
return ColumnType::SYSTEM_ID->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;
}
}
2 changes: 1 addition & 1 deletion src/Grid/Mapper/ColumnMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
{
private const COLUMN_MAPPING = [
'preview' => 'image',
'id' => 'integer',
'id' => 'id',
'type' => 'string',
'fullpath' => 'string',
'filename' => 'string',
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Grid/Mapper/ColumnMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Grid/Service/SystemColumnServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testGetSystemColumnsForAssets(): void

$this->assertSame([
'preview' => 'image',
'id' => 'integer',
'id' => 'id',
'type' => 'string',
'fullpath' => 'string',
'filename' => 'string',
Expand All @@ -48,7 +48,7 @@ public function testGetSystemColumnsForDataObjects(): void
$systemColumnService = new SystemColumnService($mapper);

$this->assertSame([
'id' => 'integer',
'id' => 'id',
'fullpath' => 'string',
'key' => 'string',
'published' => 'boolean',
Expand Down

0 comments on commit 4faefca

Please sign in to comment.