georgringer / news

TYPO3 Extension news
GNU General Public License v2.0
265 stars 355 forks source link

Redundant caches when using ArgumentsViewHelper #2374

Closed vertexvaar closed 6 months ago

vertexvaar commented 7 months ago

Bug Report

Current Behavior Using the \GeorgRinger\News\ViewHelpers\MultiCategoryLink\ArgumentsViewHelper will create duplicate cache entries for the same selection of categories, because the ViewHelper does keep track of the order in which categories are selected.

Expected behavior/output Adding and removing a category must create uniform overwriteDemand.categories parameters, so the cache hash calculation returns the same hash for each unique selection.

Environment

Possible Solution Use this code to add or remove categories from the list of categories and prevent leading commas.

        // All IDs are numeric. Hence, split and type cast.
        $categoryList = GeneralUtility::intExplode(',', $arguments['list'], true);
        if ($arguments['mode'] === 'add') {
            $categoryList[] = $categoryId;
        } else {
            // 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);
        $categoryList = implode(',', $categoryList);
georgringer commented 7 months ago

wanna do a PR then?