inspiredminds / contao-news-filter-event
Contao extension that provides an event to filter a news list.
Fund package maintenance!
fritzmg
Installs: 1 369
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 0
Open Issues: 0
Type:contao-bundle
Requires
- php: >=7.4
- contao/news-bundle: ^4.9 || ^5.0
- symfony/dependency-injection: ^4.4 || ^5.4 || ^6.2
- symfony/http-kernel: ^4.4 || ^5.4 || ^6.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16 || ^3.0
- rector/rector: ^0.14.5
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.