PHP code example of inspiredminds / contao-news-filter-event

1. Go to this page and download the library: Download inspiredminds/contao-news-filter-event library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

inspiredminds / contao-news-filter-event example snippets


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