Browse Source

backports murph-skeleton

develop
Simon Vieille 1 month ago
parent
commit
1128b1fb9a
  1. 7
      core/Controller/Admin/Crud/CrudController.php
  2. 20
      core/Crud/CrudConfiguration.php

7
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;
}
}
}

20
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;
}
}
Loading…
Cancel
Save