Skip to content

inspiredminds/contao-news-filter-event

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

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

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Languages