From 1128b1fb9a230bcb1a3be41d343ff7f0a3cef63a Mon Sep 17 00:00:00 2001 From: Simon Vieille Date: Sat, 15 May 2021 18:27:40 +0200 Subject: [PATCH] backports murph-skeleton --- core/Controller/Admin/Crud/CrudController.php | 7 +++++-- core/Crud/CrudConfiguration.php | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/core/Controller/Admin/Crud/CrudController.php b/core/Controller/Admin/Crud/CrudController.php index 8248a9d..e73d129 100644 --- a/core/Controller/Admin/Crud/CrudController.php +++ b/core/Controller/Admin/Crud/CrudController.php @@ -207,9 +207,10 @@ abstract class CrudController extends AdminController protected function applySort(string $context, RepositoryQuery $query, Request $request) { $configuration = $this->getConfiguration(); + $defaultSort = $configuration->getDefaultSort($context); - $name = $request->query->get('_sort'); - $direction = strtolower($request->query->get('_sort_direction')); + $name = $request->query->get('_sort', $defaultSort['label'] ?? null); + $direction = strtolower($request->query->get('_sort_direction', $defaultSort['direction'] ?? 'asc')); if (!in_array($direction, ['asc', 'desc'])) { $direction = 'asc'; @@ -238,6 +239,8 @@ abstract class CrudController extends AdminController 'label' => $label, 'direction' => $direction, ]; + + return; } } } diff --git a/core/Crud/CrudConfiguration.php b/core/Crud/CrudConfiguration.php index 4af2e4b..38cc8cd 100644 --- a/core/Crud/CrudConfiguration.php +++ b/core/Crud/CrudConfiguration.php @@ -2,6 +2,8 @@ namespace App\Core\Crud; +use App\Core\Crud\Exception\CrudConfigurationException; + /** * class CrudConfiguration. * @@ -20,6 +22,7 @@ class CrudConfiguration protected array $fields = []; protected array $maxPerPage = []; protected array $locales = []; + protected array $defaultSort = []; protected ?string $defaultLocale = null; protected static $self; @@ -225,4 +228,21 @@ class CrudConfiguration { return !empty($this->locales); } + + /* -- */ + + public function setDefaultSort(string $context, string $label, string $direction = 'asc'): self + { + $this->defaultSort[$context] = [ + 'label' => $label, + 'direction' => $direction, + ]; + + return $this; + } + + public function getDefaultSort(string $context) + { + return $this->defaultSort[$context] ?? null; + } }