inspiredminds/contao-news-filter-event

Contao 扩展程序,提供过滤新闻列表的事件。

资助包维护!
fritzmg

安装次数: 1,268

依赖项: 4

建议者: 0

安全性: 0

星级: 1

关注者: 4

分支: 0

开放问题: 0

类型:contao-bundle

1.1.0 2023-04-06 13:53 UTC

This package is auto-updated.

Last update: 2024-09-05 15:24:52 UTC


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)
        ;
    }
}

有关更多示例,请参阅 此处