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

Delegate filter query by empty value to filters #6402

Merged
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
24 changes: 24 additions & 0 deletions UPGRADE-3.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,30 @@ This interface has been deprecated without replacement.

`ModelManagerInterface::getDefaultSortValues()` won't be used anymore.

### Empty values in datagrid filters

Empty values are passed to datagrid filters. If you have custom datagrid filters, you MUST add empty string checks to them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VincentLanglet please review

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding an exemple could be helpful:

Something like

In the following exemple

->add('with_open_comments', CallbackFilter::class, [
    'callback' => static function(ProxyQueryInterface $queryBuilder, string $alias, string $field, array $value): bool {
        if (!$value['value']) {
            return false;
        }

        $queryBuilder
            ->leftJoin(sprintf('%s.comments', $alias), 'c')
            ->andWhere('c.status = :status')
            ->setParameter('status', Comment::STATUS_MODERATE);

         return true;
    },
    'field_type' => CheckboxType::class
 ]);

The !$value['value'] check is required to not filtering by '' if you didn't used the filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I add this

```php
->add('with_open_comments', CallbackFilter::class, [
'callback' => static function (ProxyQueryInterface $queryBuilder, string $alias, string $field, array $value): bool {
if (!$value['value']) {
return false;
}

$queryBuilder
->leftJoin(sprintf('%s.comments', $alias), 'c')
->andWhere('c.moderation = :moderation')
->setParameter('moderation', CommentModeration::APPROVED);

return true;
},
'field_type' => CheckboxType::class,
]);
```

The `!$value['value']` check is required to avoid the filtering by `''` if you didn't used the filter.

UPGRADE FROM 3.77 to 3.78
=========================

Expand Down
109 changes: 66 additions & 43 deletions src/Datagrid/Datagrid.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,50 +146,11 @@ static function ($value) {
$this->form = $this->formBuilder->getForm();
$this->form->submit($this->values);

$data = $this->form->getData();

foreach ($this->getFilters() as $name => $filter) {
$this->values[$name] = $this->values[$name] ?? null;
$filterFormName = $filter->getFormName();
if (isset($this->values[$filterFormName]['value']) && '' !== $this->values[$filterFormName]['value']) {
$filter->apply($this->query, $data[$filterFormName]);
}
}

if (isset($this->values['_sort_by'])) {
if (!$this->values['_sort_by'] instanceof FieldDescriptionInterface) {
throw new UnexpectedTypeException($this->values['_sort_by'], FieldDescriptionInterface::class);
}

if ($this->values['_sort_by']->isSortable()) {
$this->query->setSortBy($this->values['_sort_by']->getSortParentAssociationMapping(), $this->values['_sort_by']->getSortFieldMapping());

$this->values['_sort_order'] = $this->values['_sort_order'] ?? 'ASC';
$this->query->setSortOrder($this->values['_sort_order']);
}
}

$maxPerPage = 25;
if (isset($this->values['_per_page'])) {
if (isset($this->values['_per_page']['value'])) {
$maxPerPage = $this->values['_per_page']['value'];
} else {
$maxPerPage = $this->values['_per_page'];
}
}
$this->pager->setMaxPerPage($maxPerPage);

$page = 1;
if (isset($this->values['_page'])) {
if (isset($this->values['_page']['value'])) {
$page = $this->values['_page']['value'];
} else {
$page = $this->values['_page'];
}
}

$this->pager->setPage($page);
$this->applyFilters($this->form->getData() ?? []);
$this->applySorting();

$this->pager->setMaxPerPage($this->getMaxPerPage(25));
$this->pager->setPage($this->getPage(1));
$this->pager->setQuery($this->query);
$this->pager->init();

Expand Down Expand Up @@ -330,6 +291,68 @@ public function getPaginationParameters(int $page): array
return ['filter' => $values];
}

private function applyFilters(array $data): void
{
foreach ($this->getFilters() as $name => $filter) {
$this->values[$name] = $this->values[$name] ?? null;
$filterFormName = $filter->getFormName();
if (isset($this->values[$filterFormName]['type'], $this->values[$filterFormName]['value']) &&
('' !== $this->values[$filterFormName]['type'] || '' !== $this->values[$filterFormName]['value'])
) {
$filter->apply($this->query, $data[$filterFormName]);
}
}
}

private function applySorting(): void
{
if (!isset($this->values['_sort_by'])) {
return;
}

if (!$this->values['_sort_by'] instanceof FieldDescriptionInterface) {
throw new UnexpectedTypeException($this->values['_sort_by'], FieldDescriptionInterface::class);
}

if (!$this->values['_sort_by']->isSortable()) {
return;
}

$this->query->setSortBy(
$this->values['_sort_by']->getSortParentAssociationMapping(),
$this->values['_sort_by']->getSortFieldMapping()
);

$this->values['_sort_order'] = $this->values['_sort_order'] ?? 'ASC';
$this->query->setSortOrder($this->values['_sort_order']);
}

private function getMaxPerPage(int $default): int
{
if (!isset($this->values['_per_page'])) {
return $default;
}

if (isset($this->values['_per_page']['value'])) {
return (int) $this->values['_per_page']['value'];
}

return (int) $this->values['_per_page'];
}

private function getPage(int $default): int
{
if (!isset($this->values['_page'])) {
return $default;
}

if (isset($this->values['_page']['value'])) {
return (int) $this->values['_page']['value'];
}

return (int) $this->values['_page'];
}

private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription): bool
{
$values = $this->getValues();
Expand Down