Skip to content
This repository has been archived by the owner on Jul 22, 2022. It is now read-only.

Commit

Permalink
Merge 0a21c5e into 08efe44
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Mar 21, 2020
2 parents 08efe44 + 0a21c5e commit 4f12b2c
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Model/ModelManager.php
Expand Up @@ -374,7 +374,7 @@ public function getSortParameters(FieldDescriptionInterface $fieldDescription, D
{
$values = $datagrid->getValues();

if ($fieldDescription->getName() === $values['_sort_by']->getName()) {
if ($this->isFieldAlreadySorted($fieldDescription, $datagrid)) {
if ('ASC' === $values['_sort_order']) {
$values['_sort_order'] = 'DESC';
} else {
Expand All @@ -397,7 +397,9 @@ public function getPaginationParameters(DatagridInterface $datagrid, $page)
{
$values = $datagrid->getValues();

$values['_sort_by'] = $values['_sort_by']->getName();
if (isset($values['_sort_by']) && $values['_sort_by'] instanceof FieldDescriptionInterface) {
$values['_sort_by'] = $values['_sort_by']->getName();
}
$values['_page'] = $page;

return ['filter' => $values];
Expand Down Expand Up @@ -550,4 +552,16 @@ protected function camelize($property)
{
return preg_replace(['/(^|_)+(.)/e', '/\.(.)/e'], ["strtoupper('\\2')", "'_'.strtoupper('\\1')"], $property);
}

private function isFieldAlreadySorted(FieldDescriptionInterface $fieldDescription, DatagridInterface $datagrid): bool
{
$values = $datagrid->getValues();

if (!isset($values['_sort_by']) || !$values['_sort_by'] instanceof FieldDescriptionInterface) {
return false;
}

return $values['_sort_by']->getName() === $fieldDescription->getName()
|| $values['_sort_by']->getName() === $fieldDescription->getOption('sortable');
}
}

0 comments on commit 4f12b2c

Please sign in to comment.