From 1929a4a56610eff27a754ceb3d9d0b6c6153d3bb Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Tue, 23 Mar 2021 16:34:38 +0100 Subject: [PATCH] Use sorting of categories Extbase will properly use mm sorting. But categories can't be sorted within the TCA tree component. Therefore it makes more sense to use actual sys_category sorting. In order to do so, we add our own model and pass the sorting. That way PHP can do the sorting. That's the easiest approach. Also events shouldn't contain to many categories. A performance impact should not be high. Relates: #8459 --- Classes/Domain/Model/Category.php | 40 +++++++++++++++++++ Classes/Domain/Model/Event.php | 16 ++++---- Configuration/Extbase/Persistence/Classes.php | 9 +++++ Configuration/TCA/Overrides/sys_category.php | 16 ++++++++ 4 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 Classes/Domain/Model/Category.php create mode 100644 Configuration/Extbase/Persistence/Classes.php create mode 100644 Configuration/TCA/Overrides/sys_category.php diff --git a/Classes/Domain/Model/Category.php b/Classes/Domain/Model/Category.php new file mode 100644 index 0000000..1b1412e --- /dev/null +++ b/Classes/Domain/Model/Category.php @@ -0,0 +1,40 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ + +use TYPO3\CMS\Extbase\Domain\Model\Category as ExtbaseCategory; + +/** + * Extend original model to include furher properties. + */ +class Category extends ExtbaseCategory +{ + /** + * @var int + */ + protected $sorting = 0; + + public function getSorting(): int + { + return $this->sorting; + } +} diff --git a/Classes/Domain/Model/Event.php b/Classes/Domain/Model/Event.php index a613211..79d5772 100644 --- a/Classes/Domain/Model/Event.php +++ b/Classes/Domain/Model/Event.php @@ -4,7 +4,6 @@ namespace Wrm\Events\Domain\Model; use TYPO3\CMS\Extbase\Annotation as Extbase; use TYPO3\CMS\Extbase\DomainObject\AbstractEntity; -use TYPO3\CMS\Extbase\Domain\Model\Category; use TYPO3\CMS\Extbase\Domain\Model\FileReference; use TYPO3\CMS\Extbase\Object\ObjectManager; use TYPO3\CMS\Extbase\Persistence\ObjectStorage; @@ -209,7 +208,7 @@ class Event extends AbstractEntity /** * categories * - * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage<\TYPO3\CMS\Extbase\Domain\Model\Category> + * @var \TYPO3\CMS\Extbase\Persistence\ObjectStorage */ protected $categories; @@ -763,12 +762,15 @@ class Event extends AbstractEntity $this->categories->attach($category); } - /** - * @return $categories - */ - public function getCategories() + public function getCategories(): array { - return $this->categories; + $categories = $this->categories->toArray(); + + usort($categories, function (Category $catA, Category $catB) { + return $catA->getSorting() <=> $catB->getSorting(); + }); + + return $categories; } /** diff --git a/Configuration/Extbase/Persistence/Classes.php b/Configuration/Extbase/Persistence/Classes.php new file mode 100644 index 0000000..ff15775 --- /dev/null +++ b/Configuration/Extbase/Persistence/Classes.php @@ -0,0 +1,9 @@ + [ + 'tableName' => 'sys_category', + ], +]; diff --git a/Configuration/TCA/Overrides/sys_category.php b/Configuration/TCA/Overrides/sys_category.php new file mode 100644 index 0000000..4c79736 --- /dev/null +++ b/Configuration/TCA/Overrides/sys_category.php @@ -0,0 +1,16 @@ + [ + 'sorting' => [ + 'config' => [ + // Allow extbase to map this column to model + 'type' => 'passthrough', + ], + ], + ], + ]); +})('events', 'sys_category');