diff --git a/src/Datagrid/Datagrid.php b/src/Datagrid/Datagrid.php index 5062462d029..129e0c48112 100644 --- a/src/Datagrid/Datagrid.php +++ b/src/Datagrid/Datagrid.php @@ -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(); @@ -330,6 +291,64 @@ 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'])) { + 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']); + } + } + } + + private function getMaxPerPage(int $default): int + { + if (isset($this->values['_per_page'])) { + if (isset($this->values['_per_page']['value'])) { + return (int) $this->values['_per_page']['value']; + } + + return (int) $this->values['_per_page']; + } + + return $default; + } + + private function getPage(int $default): int + { + if (isset($this->values['_page'])) { + if (isset($this->values['_page']['value'])) { + return (int) $this->values['_page']['value']; + } + + return (int) $this->values['_page']; + } + + return $default; + } + private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription): bool { $values = $this->getValues();