I have set up a view for searching using Search API. In my specific setup, I need this search to be context sensitive, i.e. I have several pages /foo/search, /bar/search, ... and these return different results.

Currently, I do this by making a content type which has a field containing the necessary information and additionally a search processor, which alters the query accordingly.

Additionally, I use Twig Tweak to embed the search view in this content type as described here. This works if I load the search with the query string, e.g. localhost/foo/search?search_api_fulltext=something.

My problem is now that if I click the "Apply" button in the search, I am being redirect to the page created by the view. This page is lacking the content type's data and I no longer get the context sensitive search results.

Is it possible to change this behaviour of the view's Apply button or do you know another way this could be implemented?



Following this reply on drupal.org and this answer on SO, I added a hook:

function my_module_views_pre_build(ViewExecutable $view) {
  if ($view->id() == 'my_view_id') {
    $view->override_url = Url::fromRoute('<current>');

Some of the assumptions in Drupal and the workarounds are so annoying. Days wasted. :(

  • Why Page display over Block? – Kevin Jan 11 at 12:42
  • @Kevin: I tried to use a block on my content type but I didn't get the search field and button, just the results. Or do you mean something else? – drupalnewbie Jan 14 at 8:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.