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 committed Jun 7, 2023
1 parent 62ea192 commit 57a6a16
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Compat/CompatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,17 @@ 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
*/
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);
}

/**
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 57a6a16

Please sign in to comment.