1

How can I retain the current user's search in the search block input? Please don't suggest a third party module for this unless strictly necessary. I'm also aware of views exposed filter and that's not what I want.

Currently, I managed to grab the query with this code:

if($form_id = 'search_block_form'){
  $keys = \Drupal::request()->query->get('keys');
  $form['keys']['#attributes']['value'] = $keys;
}

The issue is that the value doesn't update for new searches, I think it may be cached. So I have a few questions...

How can I update it from the URL and not the cache?

How to grab it only if the current page is /search/node?

Is it safe or can it be susceptible to SQL injections if someone directly edit the URL in the browser's address bar and press enter/return?

  • You’re referencing the form item incorrectly. – Kevin Jan 15 at 22:14
  • Possible duplicate of How can I override the search result page title dynamically? – leymannx Jan 15 at 22:16
  • Have a look at the linked question's answer. – leymannx Jan 15 at 22:16
  • Solved part of my doubts but the cache still doesn't work. – animals Jan 15 at 23:01
  • 1
    Yes, I used your answer but instead of the title applied it to the value attribute, it didn't worked. Then I also tried it with the placeholder attribute and doesn't work either. Search block is cached, it applies the first value but doesn't update afterwards. Then I used your answer for the title and it works for the title - I will leave it because it's a nice addition to the search title. – animals Jan 15 at 23:52

Your Answer

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

Browse other questions tagged or ask your own question.