Advance Search form $form is not the same when invoked a second time? 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.


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(&amp;$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: 

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

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.
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