From 272bc8b5490d7e72c7b83fc653f5a3a99afe80f7 Mon Sep 17 00:00:00 2001 From: raviks789 <33730024+raviks789@users.noreply.github.com> Date: Fri, 2 Jun 2023 15:34:10 +0200 Subject: [PATCH] Add optional `$defaultSort` parameter for `SortControl::apply` method This gives an option to set the default sort of sort control other than the `ipl\Orm\Model`s default sort as default. --- src/Compat/CompatController.php | 4 ++-- src/Control/SortControl.php | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Compat/CompatController.php b/src/Compat/CompatController.php index 30fc27d7..fb4d59ba 100644 --- a/src/Compat/CompatController.php +++ b/src/Compat/CompatController.php @@ -286,13 +286,13 @@ public function createPaginationControl(Paginatable $paginatable): PaginationCon * * @return SortControl */ - public function createSortControl(Query $query, array $columns): SortControl + public function createSortControl(Query $query, array $columns, $defaultSort = null): SortControl { $sortControl = SortControl::create($columns); $this->params->shift($sortControl->getSortParam()); - return $sortControl->apply($query); + return $sortControl->apply($query, $defaultSort); } /** diff --git a/src/Control/SortControl.php b/src/Control/SortControl.php index a457d923..9f9091b4 100644 --- a/src/Control/SortControl.php +++ b/src/Control/SortControl.php @@ -169,12 +169,13 @@ public function getSort() * Sort the given query according to the request * * @param Query $query + * @param ?array|string $defaultSort * * @return $this */ - public function apply(Query $query) + public function apply(Query $query, $defaultSort = null) { - $default = (array) $query->getModel()->getDefaultSort(); + $default = $defaultSort ?? (array) $query->getModel()->getDefaultSort(); if (! empty($default)) { $this->setDefault(SortUtil::normalizeSortSpec($default)); }