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
Requires
- php: >=8.1
- contao/news-bundle: ^4.13 || ^5.0
- symfony/dependency-injection: ^5.4 || ^6.4 || ^7.1
- symfony/http-kernel: ^5.4 || ^6.4 || ^7.1
Requires (Dev)
- contao/easy-coding-standard: ^6.0
- contao/rector: ^1.0
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.