inspiredminds / contao-news-filter-event
Contao 扩展程序,提供过滤新闻列表的事件。
1.1.0
2023-04-06 13:53 UTC
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 提供了一种通过 newsListFetchItems
钩子输出自定义新闻列表模块中的新闻项的方式。然而,如果有两个或更多的扩展程序希望根据某些参数过滤新闻项,则只有一个是胜利者。这个 Contao 扩展程序相反提供了一种 NewsFilterEvent
,其中可以基本自定义将要从数据库中检索新闻项的 NewsModel::findBy()
调用的参数。多个扩展程序可以添加它们的条件,因此新闻列表可以通过这些不同扩展程序提供的多个参数进行过滤。
例如,以下事件监听器将通过作者查询参数过滤新闻列表
// 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) ; } }
有关更多示例,请参阅 此处