Search code examples
typo3typoscripttypo3-9.xtx-news

How to limit showPrevNext in news to categories?


In the TYPO3 CMS 9.5.18 LTS with tx_news 8.3.0 we use the following extension Typoscript:

plugin.tx_news.settings{
  # what is allowed to overwrite with TS
  overrideFlexformSettingsIfEmpty := addToList(categories)
  overrideFlexformSettingsIfEmpty := addToList(categoryConjunction)
  # ids of categories
  categories = 3
  # category conjunction mode
  categoryConjunction = or
}

I wonder why I have to add categories to overrideFlexformSettingsIfEmpty to get the result below. Never the less this post is more about how to achieve that the prev/next links (settings.detail.showPrevNext) does respect the category definition at all.

Our customer has 3 categories for news. If I go to a single page that does has a one category limitation (for the detail and the list page) I still e.g. can go "forward" to newer news in a total different category. However the list page only shows the news of that one selected category.

<f:if condition="{paginated.prev}">
    <n:link newsItem="{paginated.prev}" settings="{settings}" class="ts-prev">
        {paginated.prev.title}
    </n:link>
</f:if>

Wasn't that never the case? Do I have to add some Typoscript or make a change in Fluid? The original code uses this settings variable as argument which contains the category limitation.


Solution

  • Okay I've had a look into the GeorgRinger\News\ViewHelpers\SimplePrevNextViewHelper and there aren't any limitation for the current chosen categories.

    So here is what I did:

    1. register a new optional argument categories to the viewhelper
    2. add categories="{settings.categories}" to the simplePrevNext tag in the Detail.html
    3. add an 'extra where' for the main query in the getNeighbours function
    4. add the content for the additional where ( I did that first in the getNeighbours function )

    Extra where:

    if( is_array($newsOfCategory) && count($newsOfCategory) > 0 ){
        $extraWhere[] = $queryBuilder->expr()->in('uid', $queryBuilder->createNamedParameter($newsOfCategory, Connection::PARAM_INT_ARRAY));
    }
    

    Content for the additional where:

    if( is_string($categories) && preg_match('/^[0-9]+(,[0-9]+)*$/',$categories) ){
        $categories = explode(',', $categories);
        $tmpCon = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_category_record_mm');
        $tmpQB = $tmpCon->createQueryBuilder();
        $rows = $tmpQB
            ->select('uid_foreign')
            ->from('sys_category_record_mm')
            ->where(
                $tmpQB->expr()->in('uid_local', $tmpQB->createNamedParameter($categories, Connection::PARAM_INT_ARRAY))
            )
            ->andWhere(
                $tmpQB->expr()->like('tablenames', $tmpQB->createNamedParameter('tx_news_domain_model_news'))
            )
            ->execute()->fetchAll();
        if( is_array($rows) && count($rows) > 0 ){
            foreach($rows as $row){
                $newsOfCategory[] = $row['uid_foreign'];
            }
        }
    }
    

    May be someone can use that in the future or the will integrate that to the repository.