Add natural sorting of TYPO3 tested / compatible versions

This commit is contained in:
Daniel Siepmann 2021-12-01 16:52:19 +01:00
parent 02fcfb94f8
commit 98d841e5fc
3 changed files with 82 additions and 3 deletions

View file

@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
/*
* Copyright (C) 2021 Daniel Siepmann <coding@daniel-siepmann.de>
*
* 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;
}
}

View file

@ -26,9 +26,14 @@ tt_content.menu_abstract {
leftjoin = sys_category_record_mm ON sys_category_record_mm.uid_local = sys_category.uid leftjoin = sys_category_record_mm ON sys_category_record_mm.uid_local = sys_category.uid
orderBy = sorting orderBy = sorting
} }
11 = DanielSiepmann\DsSite\Frontend\DataProcessing\NaturalSortingProcessor
11 < .10
11 { 11 {
variablePath = compatibleWith
variableSubPath = data/title
}
12 < .10
12 {
as = topics as = topics
where = sys_category_record_mm.uid_foreign = {field:uid} AND sys_category.parent = 1 where = sys_category_record_mm.uid_foreign = {field:uid} AND sys_category.parent = 1
} }

View file

@ -72,6 +72,11 @@ page {
where = sys_category_record_mm.uid_foreign = {page:uid} AND sys_category.parent = 2 where = sys_category_record_mm.uid_foreign = {page:uid} AND sys_category.parent = 2
orderBy = sorting orderBy = sorting
} }
12 = DanielSiepmann\DsSite\Frontend\DataProcessing\NaturalSortingProcessor
12 {
variablePath = compatibleWith
variableSubPath = data/title
}
50 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor 50 = TYPO3\CMS\Frontend\DataProcessing\DatabaseQueryProcessor
50 { 50 {
@ -90,9 +95,13 @@ page {
101 < .100 101 < .100
101 { 101 {
as = allTypo3Compatible as = allTypo3Compatible
orderBy = title asc, title asc
parent = 2 parent = 2
} }
102 = DanielSiepmann\DsSite\Frontend\DataProcessing\NaturalSortingProcessor
102 {
variablePath = allTypo3Compatible
variableSubPath = data/title
}
500 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor 500 = TYPO3\CMS\Frontend\DataProcessing\FilesProcessor
500 { 500 {