Skip to content

Commit

Permalink
Merge 3.x into master
Browse files Browse the repository at this point in the history
  • Loading branch information
SonataCI committed Mar 14, 2020
2 parents 83cd270 + 34b9672 commit 428bdba
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/Model/ModelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ public function getSortParameters(FieldDescriptionInterface $fieldDescription, D
{
$values = $datagrid->getValues();

if ($fieldDescription->getName() === $values['_sort_by']->getName() || $values['_sort_by']->getName() === $fieldDescription->getOption('sortable')) {
if ($this->isFieldAlreadySorted($fieldDescription, $datagrid)) {
if ('ASC' === $values['_sort_order']) {
$values['_sort_order'] = 'DESC';
} else {
Expand All @@ -391,7 +391,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 @@ -515,4 +517,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 428bdba

Please sign in to comment.