8000 Introduce a `SearchResultEvent` by fritzmg · Pull Request #7919 · contao/contao · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Introduce a SearchResultEvent #7919

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: 5.x
Choose a base branch
from

Conversation

fritzmg
Copy link
Contributor
@fritzmg fritzmg commented Jan 21, 2025

Currently there is no way to filter front end search results easily (except by replacing ModuleSearch completely). This PR would introduce a SearchResultEvent where you could then do something like this:

// src/EventListener/SearchResultEventListener.php
namespace App\EventListener;

use Contao\CoreBundle\Event\SearchResultEvent;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpFoundation\RequestStack;

#[AsEventListener]
class SearchResultEventListener
{
    public function __construct(private readonly RequestStack $requestStack)
    {
    }

    public function __invoke(SearchResultEvent $event): void
    {
        $request = $this->requestStack->getCurrentRequest();

        $event->getSearchResult()->applyFilter(static function(array $result) use ($request): bool {
            if ($request->query->get('foobar')) {
                // Filter by some custom condition 
                // …
            }

            return true;
        });
    }
}

@fritzmg fritzmg added this to the 5.6 milestone Jan 21, 2025
@fritzmg fritzmg self-assigned this Jan 21, 2025
@Toflar
Copy link
Member
Toflar commented Feb 5, 2025

I think this should be considered when reworking the search module as content element and not be merged as is, right?

@fritzmg
Copy link
Contributor Author
fritzmg commented Feb 5, 2025

Yes, ideally a completely new implementation would be more flexible in general. This solution alone also is not ze 🟡 of ze 🥚: you cannot influence the order of the search results (without resorting to Reflection currently).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0