Skip to content

Commit

Permalink
refactor: Making filter path configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
philipsorst committed Apr 4, 2024
1 parent e9e59b9 commit 7fa205f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
3 changes: 2 additions & 1 deletion src/Model/FieldDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public function __construct(
public readonly array $crudOperations = [],
public readonly ?string $formType = null,
public readonly bool $sortable = false,
public readonly bool $filterable = false
public readonly bool $filterable = false,
public readonly ?string $filterPath = null,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public function provideFieldDefinitions(string $entityClass): ?array
formType: $fieldDefinitionConfiguration['form_type'] ?? null,
sortable: $fieldDefinitionConfiguration['sortable'] ?? false,
filterable: $fieldDefinitionConfiguration['filterable'] ?? false,
filterPath: $fieldDefinitionConfiguration['filter_path'] ?? null
);
}

Expand Down
38 changes: 26 additions & 12 deletions src/Service/Pagination/DefaultPaginationProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Dontdrinkandroot\Common\Asserted;
use Dontdrinkandroot\Common\CrudOperation;
use Dontdrinkandroot\CrudAdminBundle\Model\DefaultSort;
use Dontdrinkandroot\CrudAdminBundle\Model\FieldDefinition;
use Dontdrinkandroot\CrudAdminBundle\Service\FieldDefinition\FieldDefinitionsResolverInterface;
use Dontdrinkandroot\CrudAdminBundle\Service\PaginationTarget\PaginationTargetResolver;
use Dontdrinkandroot\CrudAdminBundle\Service\Sort\DefaultSortProviderInterface;
Expand Down Expand Up @@ -50,21 +51,13 @@ public function providePagination(string $entityClass): ?PaginationInterface
$this->fieldDefinitionsResolver->resolveFieldDefinitions($entityClass, CrudOperation::LIST)
);
foreach ($fieldDefinitions as $fieldDefinition) {

if ($fieldDefinition->sortable) {
if (str_contains($fieldDefinition->propertyPath, '.')) {
/* If it contains a dot, it's a joined field, so we don't need to prefix it */
$sortFields[] = $fieldDefinition->propertyPath;
} else {
$sortFields[] = $fieldPrefix . $fieldDefinition->propertyPath;
}
$sortFields[] = $this->getSortFieldPath($fieldDefinition, $fieldPrefix);
}

if ($fieldDefinition->filterable) {
if (str_contains($fieldDefinition->propertyPath, '.')) {
/* If it contains a dot, it's a joined field, so we don't need to prefix it */
$filterFields[] = $fieldDefinition->propertyPath;
} else {
$filterFields[] = $fieldPrefix . $fieldDefinition->propertyPath;
}
$filterFields[] = $this->getFilterFieldPath($fieldDefinition, $fieldPrefix);
}
}

Expand Down Expand Up @@ -113,4 +106,25 @@ private function resolveDefaultSort(string $entityClass): ?DefaultSort

return null;
}

private function getSortFieldPath(FieldDefinition $fieldDefinition, string $prefix): string
{
if (str_contains($fieldDefinition->propertyPath, '.')) {
/* If it contains a dot, it's a joined field, so we don't need to prefix it */
return $fieldDefinition->propertyPath;
}

return $prefix . $fieldDefinition->propertyPath;
}

private function getFilterFieldPath(FieldDefinition $fieldDefinition, string $prefix): string
{
$path = $fieldDefinition->filterPath ?? $fieldDefinition->propertyPath;
if (str_contains($path, '.')) {
/* If it contains a dot, it's a joined field, so we don't need to prefix it */
return $path;
}

return $prefix . $path;
}
}

0 comments on commit 7fa205f

Please sign in to comment.