From 98d841e5fcd752797dde3317a33681ad9663ec86 Mon Sep 17 00:00:00 2001 From: Daniel Siepmann Date: Wed, 1 Dec 2021 16:52:19 +0100 Subject: [PATCH] Add natural sorting of TYPO3 tested / compatible versions --- .../NaturalSortingProcessor.php | 65 +++++++++++++++++++ .../ContentElements/MenuAbstract.typoscript | 9 ++- .../TypoScript/Setup/Page.typoscript | 11 +++- 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 Classes/Frontend/DataProcessing/NaturalSortingProcessor.php diff --git a/Classes/Frontend/DataProcessing/NaturalSortingProcessor.php b/Classes/Frontend/DataProcessing/NaturalSortingProcessor.php new file mode 100644 index 0000000..b7eed5e --- /dev/null +++ b/Classes/Frontend/DataProcessing/NaturalSortingProcessor.php @@ -0,0 +1,65 @@ + + * + * 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. + */ + +namespace DanielSiepmann\DsSite\Frontend\DataProcessing; + +use TYPO3\CMS\Core\Utility\ArrayUtility; +use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer; +use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface; + +/** + * SQLite does not support natural sorting. + * Yet we wanna have that, e.g. for TYPO3 compatible version to have 11, 10, 9, 8 instead of 10, 11, 8, 9. + * + * This processor allows to sort a given variable $variablePath by given $variableSubPath in natural reverse sorting. + */ +class NaturalSortingProcessor implements DataProcessorInterface +{ + public function process( + ContentObjectRenderer $cObj, + array $contentObjectConfiguration, + array $processorConfiguration, + array $processedData + ) { + $variablePath = $cObj->stdWrapValue('variablePath', $processorConfiguration, ''); + $variableSubPath = $cObj->stdWrapValue('variableSubPath', $processorConfiguration, ''); + + if ($variablePath === '' || $variableSubPath === '') { + throw new \Exception('Provide variablePath as well as variableSubPath.', 1638373263); + } + + if (ArrayUtility::isValidPath($processedData, $variablePath) === false) { + return $processedData; + } + + $valuesToSort = ArrayUtility::getValueByPath($processedData, $variablePath); + usort($valuesToSort, function (array $variable1, array $variable2) use ($variableSubPath) { + $value1 = ArrayUtility::getValueByPath($variable1, $variableSubPath); + $value2 = ArrayUtility::getValueByPath($variable2, $variableSubPath); + return strnatcasecmp($value2, $value1); + }); + $processedData = ArrayUtility::setValueByPath($processedData, $variablePath, $valuesToSort); + + return $processedData; + } +} diff --git a/Configuration/TypoScript/Setup/ContentElements/MenuAbstract.typoscript b/Configuration/TypoScript/Setup/ContentElements/MenuAbstract.typoscript index 632380d..566c6fa 100644 --- a/Configuration/TypoScript/Setup/ContentElements/MenuAbstract.typoscript +++ b/Configuration/TypoScript/Setup/ContentElements/MenuAbstract.typoscript @@ -26,9 +26,14 @@ tt_content.menu_abstract { leftjoin = sys_category_record_mm ON sys_category_record_mm.uid_local = sys_category.uid orderBy = sorting } - - 11 < .10 + 11 = DanielSiepmann\DsSite\Frontend\DataProcessing\NaturalSortingProcessor 11 { + variablePath = compatibleWith + variableSubPath = data/title + } + + 12 < .10 + 12 { as = topics where = sys_category_record_mm.uid_foreign = {field:uid} AND sys_category.parent = 1 } diff --git a/Configuration/TypoScript/Setup/Page.typoscript b/Configuration/TypoScript/Setup/Page.typoscript index fc79443..4600156 100644 --- a/Configuration/TypoScript/Setup/Page.typoscript +++ b/Configuration/TypoScript/Setup/Page.typoscript @@ -72,6 +72,11 @@ page { where = sys_category_record_mm.uid_foreign = {page:uid} AND sys_category.parent = 2 orderBy = sorting } + 12 = DanielSiepmann\DsSite\Frontend\DataProcessing\NaturalSortingProcessor + 12 { + variablePath = compatibleWith + variableSubPath = data/title + } 50 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor 50 { @@ -90,9 +95,13 @@ page { 101 < .100 101 { as = allTypo3Compatible - orderBy = title asc, title asc parent = 2 } + 102 = DanielSiepmann\DsSite\Frontend\DataProcessing\NaturalSortingProcessor + 102 { + variablePath = allTypo3Compatible + variableSubPath = data/title + } 500 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor 500 {