inspiredminds/contao-news-filter-event

Contao extension that provides an event to filter a news list.

Fund package maintenance!
fritzmg

Installs: 1 488

Dependents: 4

Suggesters: 0

Security: 0

Stars: 1

Watchers: 4

Forks: 0

Open Issues: 0

Type:contao-bundle

1.1.1 2024-12-05 22:33 UTC

This package is auto-updated.

Last update: 2024-12-05 22:33:17 UTC


README

Contao News Filter Event

Contao provides a way to output custom news items in the news list module via the newsListFetchItems hook. However, if two or more extensions want to filter the news items according to some parameters, only one can win. This Contao extension instead provides a NewsFilterEvent where you can basically customize the parameters of the NewsModel::findBy() call that will fetch the news items from the database. Multiple extensions can add their conditions and thus the news list can be filtered down by multiple parameters provided by these different extensions.

For example, the following event listener would filter the news list via an author query parameter:

// src/EventListener/AuthorNewsFilterListener.php
namespace App\EventListener;

use InspiredMinds\ContaoNewsFilterEvent\Event\NewsFilterEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\RequestStack;

#[AsEventListener]
class AuthorNewsFilterListener
{
    public function __construct(private readonly RequestStack $requestStack)
    {
    }

    public function __invoke(NewsFilterEvent $event): void
    {
        $request = $this->requestStack->getCurrentRequest();
        $authorId = max(0, (int) $request->query->get('author'));

        if (!$authorId) {
            return;
        }

        $event
            ->addColumn('tl_news.author = ?')
            ->addValue($authorId)
        ;
    }
}

See here for further examples.