Skip to content

Commit 23acb45

Browse files
authored
Datagrid default order now uses the native SortDirection enum (#4840)
2 parents f361945 + d35f472 commit 23acb45

3 files changed

Lines changed: 14 additions & 17 deletions

File tree

src/Component/Datagrid/Datagrid.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@
1313
use Shopsys\AdministrationBundle\Component\Datagrid\Adapter\AdapterInterface;
1414
use Shopsys\AdministrationBundle\Component\Datagrid\Adapter\EntityClassAwareAdapterInterface;
1515
use Shopsys\AdministrationBundle\Component\Datagrid\Field\FieldDescriptor;
16+
use Shopsys\FrameworkBundle\Component\Grid\DataSourceInterface;
1617
use Shopsys\FrameworkBundle\Component\Grid\GridFactory;
1718
use Shopsys\FrameworkBundle\Component\Grid\GridView;
1819
use Shopsys\FrameworkBundle\Component\Grid\Ordering\Exception\EntityIsNotOrderableException;
1920
use Shopsys\FrameworkBundle\Component\Grid\Ordering\OrderableEntityInterface;
21+
use SortDirection;
2022
use Symfony\Component\OptionsResolver\OptionsResolver;
2123

2224
/**
@@ -45,7 +47,7 @@ final class Datagrid
4547
private array $fieldsOrder = [];
4648

4749
/**
48-
* @var array{field: string, order: \Shopsys\AdministrationBundle\Component\Datagrid\OrderingEnum}|null
50+
* @var array{field: string, order: \SortDirection}|null
4951
*/
5052
private ?array $defaultOrder = null;
5153

@@ -116,7 +118,7 @@ public function setPagination(bool $pagination): self
116118
/**
117119
* Set default order of datagrid
118120
*/
119-
public function setDefaultOrder(string $field, OrderingEnum $order): self
121+
public function setDefaultOrder(string $field, SortDirection $order): self
120122
{
121123
if ($this->dragAndDropEntityClass !== null) {
122124
throw new InvalidArgumentException(
@@ -164,7 +166,7 @@ public function enableDragAndDrop(string $field): self
164166
$this->dragAndDropEntityClass = $entityClass;
165167
$this->defaultOrder = [
166168
'field' => $field,
167-
'order' => OrderingEnum::ASC,
169+
'order' => SortDirection::Ascending,
168170
];
169171

170172
return $this;
@@ -303,7 +305,13 @@ public function createView(): GridView
303305
}
304306

305307
if ($this->dragAndDropEntityClass === null && $this->defaultOrder !== null) {
306-
$grid->setDefaultOrder($this->defaultOrder['field'], $this->defaultOrder['order']->value);
308+
$grid->setDefaultOrder(
309+
$this->defaultOrder['field'],
310+
match ($this->defaultOrder['order']) {
311+
SortDirection::Ascending => DataSourceInterface::ORDER_ASC,
312+
SortDirection::Descending => DataSourceInterface::ORDER_DESC,
313+
},
314+
);
307315
}
308316

309317
if (count($this->fieldsOrder) > 0) {

src/Component/Datagrid/OrderingEnum.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/Controller/BlogArticleAuthorController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Shopsys\AdministrationBundle\Component\Config\CrudConfig;
1010
use Shopsys\AdministrationBundle\Component\Crud\Form\CrudFormConfigurator;
1111
use Shopsys\AdministrationBundle\Component\Datagrid\Datagrid;
12-
use Shopsys\AdministrationBundle\Component\Datagrid\OrderingEnum;
1312
use Shopsys\AdministrationBundle\Model\Blog\Author\BlogArticleAuthorCrudHandler;
1413
use Shopsys\FrameworkBundle\Component\Grid\DataSourceInterface;
1514
use Shopsys\FrameworkBundle\Component\Grid\Grid;
@@ -22,6 +21,7 @@
2221
use Shopsys\FrameworkBundle\Model\Blog\Article\BlogArticleRepository;
2322
use Shopsys\FrameworkBundle\Model\Blog\Author\BlogArticleAuthor;
2423
use Shopsys\FrameworkBundle\Model\Localization\Localization;
24+
use SortDirection;
2525

2626
#[CrudController(BlogArticleAuthor::class)]
2727
#[ForRole(AdminRoleConstant::ROLE_BLOG_ARTICLE_AUTHOR)]
@@ -52,7 +52,7 @@ protected function configureDatagrid(Datagrid $datagrid): void
5252
'label' => t('Name'),
5353
]);
5454

55-
$datagrid->setDefaultOrder('name', OrderingEnum::ASC);
55+
$datagrid->setDefaultOrder('name', SortDirection::Ascending);
5656
}
5757

5858
#[Override]

0 commit comments

Comments
 (0)