1

I Want the users to have an expanded advance search form if no results are returned from a search. The advance search form on the search page always defaults to collapsed.

enter image description here I found that I can check whether I have results in two different ways: theme or module, depending on the hook I want to use, I chose module because it fires earlier than the theme's.

I also can effect the advanced search form's attributes (whether it's collapsed, or maybe display other data inside of the advanced search form, etc) with drupal's hook_FORMID_form_alter() with MYTHEME_form_search_form_alter's first call. The issue I have is MYTHEME_form_search_form_alter() gets called before results are returned.

So I can alter the advance search form first, then I can check if we returned any results from the search... which doesn't help me, because I need it to be vice versa - If I know I have no results then I know to expand the advanced search form's collapsible form.

My quick solution was to invoke drupal_get_form() with drupal_get_form("search_form", "no_results"); inside of my module's preprocess search hook MYMODULE_search_preprocess_search_results(&$variables) and just pass in the variable "no_results". This (as most of you probably know) will invoke hook_form_search_form_alter again, where I can edit the advance search form now and I will alter the advance search form only if my variable "no_results" comes through.

Here is my crux though! Even though I am invoking that theme hook again and checking for the variable, when I go to edit the $form's "advanced" entry... its gone now. So it's clearly there when theme's hook is called, and I can indeed effect the arrays, but if I try to effect the advanced array when I invoke the theme's hook (inside of my module's hook) it's not there. Where would I invoke this theme's hook if not on the module layer? Hell, the theme's preprocess hook is fired AFTER the module's and has proven to be the same circumstance, seems in either of the two ways of checking my results I cannot invoke the form_alter hook and get the exact same form back.

This is before I check results when THEME's alter hook is first being called: enter image description here

This is after I check results in my module's hook and invoke the THEME's alter hook once more: enter image description here

You can see my new variable denoting that we have no result (in this case it's LarryU) under the action key, but there is no advanced key. I tried just remaking it but it doesn't to the form.

1 Answer 1

1

As I understood from your question you need to add additional style when no results found.

The file search-block-form.tpl.php in search module folder is responsible about displaying the form you could copy it to your theme folder and alter the markup as you need (You have to clear cache in order to flush theme registry).

Also take a look at the two files search-result.tpl.php and search-results.tpl.php the same could be done depending on your needs and use case.

For more clarification about theme suggestions read

2
  • I have solved with jQuery, but If the advanced search form has a key called '#collapsed' and when I set it to 0 it will automatically add the class for me. To me, this seems cleaner to adjust it on the backend instead of on the front, but I think perhaps your solution is still better than my jQuery one. Mine is dirty I feel, I grab all h1 tags, see if any of them have a no result string, if they do then I use jquery to click on the advance search link to expand the form. But .tpl sounds like it may be cleaner, ill just add the class to the form in the .tpl file. Ill let you know if it works.
    – Daedalus
    Mar 3, 2016 at 13:57
  • So My issue with search-result.tpl.php and search-results.tpl.php is that they are not triggered if users just search without any text in the input field. With jQuery technique I can check if 1/2 strings are within a h1 tag - var error_result = ['Error message', 'Your search yielded no results']; and then I check if the array of h1 tags has either of these entries, if they do then expand the advanced search form. I really wish I could change the '#collapsed' to 0 if I get no results, but damn it drupal_get_form returns an augmented form by the time I get to see if I got a result :-(.
    – Daedalus
    Mar 3, 2016 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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