codefog / contao-news_categories

Extend the Contao news module with categories
MIT License
31 stars 25 forks source link

Improve the newsListFetchItems Hook #242

Closed derMatze82 closed 1 year ago

derMatze82 commented 1 year ago

If the Contao Tags Bundle is used in parallel with this bundle, its newsListFetchItems hook no longer works. This is because you always return a collection, even if no category filter is active. Quick Fix: Codefog\NewsCategoriesBundle\EventListener\NewsListener

public function onNewsListFetchItems(array $archives, $featured, $limit, $offset, ModuleNewsList $module)
{

    if (null === ($criteria = $this->getCriteria($archives, $featured, $module))) {
        return null;
    }

    $currentParam = $this->newsCategoriesManager->getParameterName();

    if (empty(Input::get($currentParam))) {
        return false;
    }
    ...
fritzmg commented 1 year ago

There can always only be one newsListFetchItems hook that actually filters the news.

An alternative solution is to use inspiredminds/contao-news-filter-event. There exists a bridge for the codefog/contao-news_categories extension: inspiredminds/contao-categories-news-filter.

In order for this to work with the numero2/contao-tags extension you would either need to build your own bridge, or the numero2/contao-tags extension needs to switch from using the newsListFetchItems hook to the NewsFilterEvent.

derMatze82 commented 1 year ago

Just to be sure, I don't want to filter by categories and tags at the same time, but either or. The numero2/contao-tags extension would make that possible ;) But thanks for pointing out the codefog/contao-news_categories extension!

fritzmg commented 1 year ago

I agree though that NewsListener::onNewsListFetchItems should return false if category filtering is not enabled at all for the respective news list module.