Skip to content

Commit

Permalink
Add optional $defaultSort parameter for SortControl::apply method
Browse files Browse the repository at this point in the history
This gives an option to set the default sort of sort control other than the
`ipl\Orm\Model`s default sort as default.
  • Loading branch information
raviks789 authored and nilmerg committed Jun 12, 2023
1 parent 0b345cf commit 7855ce3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/Compat/CompatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ public function createPaginationControl(Paginatable $paginatable): PaginationCon
*
* @param Query $query
* @param array $columns Possible sort columns as sort string-label pairs
* @param ?array|string $defaultSort Optional default sort column
*
* @return SortControl
*/
Expand All @@ -292,7 +293,13 @@ public function createSortControl(Query $query, array $columns): SortControl

$this->params->shift($sortControl->getSortParam());

return $sortControl->apply($query);
$defaultSort = null;

if (func_num_args() === 3) {
$defaultSort = func_get_args()[2];
}

return $sortControl->apply($query, $defaultSort);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Control/SortControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 7855ce3

Please sign in to comment.