Skip to content

Commit

Permalink
[BUGFIX] Sort the list of categories to reduce the amount of possible…
Browse files Browse the repository at this point in the history
… combinations

Each combination of category uids will result in a different cache hash. This means, that 1,3 and 3,1 are cached separately, although the order does not make a difference for the news listing.
Sorting the category uids ensures, that the number of possible combinations is as low as possible.

Fixes: #2374
  • Loading branch information
vertexvaar committed Apr 12, 2024
1 parent 7850073 commit 3b954f2
Showing 1 changed file with 10 additions and 17 deletions.
27 changes: 10 additions & 17 deletions Classes/ViewHelpers/MultiCategoryLink/ArgumentsViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,21 @@ public static function renderStatic(
$allArguments = (array)$arguments['additionalParams'];
$categoryId = (int)$arguments['item'];

$categoryList = $arguments['list'];
// All IDs are numeric. Hence, split and type cast.
$categoryList = GeneralUtility::intExplode(',', $arguments['list'], true);
if ($arguments['mode'] === 'add') {
if (!GeneralUtility::inList($categoryList, $categoryId)) {
$categoryList .= ',' . $categoryId;
}
$categoryList[] = $categoryId;
} else {
$categoryList = self::rmFromList($categoryId, $categoryList);
// array_diff has the advantage, that it does not care how often the searched value occurs.
$categoryList = array_diff($categoryList, [$categoryId]);
}
// Ensure each ID to only occur once
$categoryList = array_unique($categoryList);
// Sort IDs, so lists are more uniform and less duplicate caches are generated
sort($categoryList);

if (!empty($categoryList)) {
$categoryList = trim($categoryList, ',');
$categoryList = implode(',', $categoryList);;
$categoryArray = [
'tx_news_pi1' => [
'overwriteDemand' => [
Expand All @@ -94,15 +98,4 @@ public static function renderStatic(

return $allArguments;
}

private static function rmFromList($element, $list)
{
$items = explode(',', $list);
foreach ($items as $k => $v) {
if ($v == $element) {
unset($items[$k]);
}
}
return implode(',', $items);
}
}

0 comments on commit 3b954f2

Please sign in to comment.