Does anyone know how to remove the search form from the search results page. NOTE: I'm not talking about the search block but the form that gets stuck at the top of the list of results.

I've tried the stuff here http://drupal.org/node/45295 and cleared my cache like there's no tomorrow, but I think that the form on the results page might not use "search_theme_form" because I've even tried modifying "modules/search/search-theme-form.tpl.php" directly and it had no effect.

Any got any bright ideas?

Chris

Comments

rschwab’s picture

Hmm, thats a tricky one because the function is the same for building both the initial search page and the search results page - so if you want the form to appear on the original page but not results, its going to be difficult. You could use jquery and css to hide the form on a results page using display:none, or copy the search module into your sites module directory and adapt it to your purposes.

chunty’s picture

I'm using the search block in the header to submit my initial request to http://sitename.com/search (if that makes sense), basically when I arrive on the page after I've submitted a search from the search block I don't ever want a search form - there's no point having 2 forms on the page (I keep the search block in the header even when I'm on the search results page) and its looks a bit ugly and pushes everything down the page

If that makes sense? - does that make life easier?

C

rschwab’s picture

Definitely. Target it using CSS and give it a display:none setting.

For example, on a zen subtheme it might look like:

div#content form#search-form {display:none;}

You'll want to find a rule specific enough to target only the search form in the main section of your site, not the form in the block.

chunty’s picture

Is there any way to do this with a hook function or template file, whilst techinically there's nothing wrong with the CSS route it always feels a bit hacky to me.

C

rschwab’s picture

You could use hook_form_alter and remove all the visible elements. Or copy the search module to your sites module directory and remove "$output = drupal_get_form('search_form', NULL, $keys, $type);" from the page callback in search.pages.inc. This last way is "hacking core", so while its cleaner and lighter code wise, its probably not the most drupal kosher way to go.

dyesdyes’s picture

If it can help someone, i found a solution a bit dirty but still better than hidding with CSS or hacking core.

here is my code :


function theme_form($element) {
// uncomment the next line for easier use, you need to have devel module ON to use this function.
// dpm($element);

// here you have to give a condition that you are sure or all the form of your page and of the site will be removed as well
// for exemple, use form_id or whatever.
if (your_condition)
// return form as usual
return '<form ' . $element['#action'] . ' accept-charset="UTF-8" method="' . $element['#method'] . '" id="' . $element['#id'] . '"' . drupal_attributes($element['#attributes']) . ">\n" . $element['#children'] . "\n</form>\n";
// return nothing if the condition is not taken
else
return '';
}

Of course, you have to change theme_form by NameOfYourTheme_form.

Hope it will help someone.

Haidee’s picture

have you solved this issue? will you please share how?

asb’s picture

subscribing

mk31762’s picture

The problem with all the solutions presented here is that, if you want a persistent search form in your header then there's no way to consistently distinguish it from the form that gets generated on the search results page. The results page gets rendered first, so the form gets the id "search-form" and the form in the header becomes "search-form-1". Otherwise, the header becomes "search-form". So which one do you hide via CSS? Or hook_form_alter for that matter? This is one of those problems that I would expect to see more people complaining about. It would be nice if the Search module separated the form from the results. On sites I've done in the past I've simply left the form on the results page. there didn't seem to be any other way of dealing with it. I would love to be proven wrong, though.

milesw’s picture

I've been fighting with this for two days now. In Drupal 7 I completely remove the form with hook_preprocess_page() or hook_page_alter() and it still shows up! There has to be a way...

milesw’s picture

Finally figured out why removing the form via hook_page_alter() was not working. For some crazy reason the $page array is different depending on whether you are logged in or not. So when I looked at $page using Devel dsm() (logged in) and unset the form there, it was still appearing in the other browser (not logged in).

Remove search form on the results page (D7):

function mytheme_page_alter(&$page) {
  // This assumes everything being output in the "content" page region.

  // Logged in
  if (!empty($page['content']['system_main']['content']['search_form'])) {
    unset($page['content']['system_main']['content']['search_form']);
  }

  // Not logged in
  if (!empty($page['content']['system_main']['search_form'])) {
    unset($page['content']['system_main']['search_form']);
  }
}
WilliamB’s picture

Thank you!

aiphes’s picture

thanks a lot , happy to find the solution here

Dev Server Shared Hosting
8 websites powered by drupal 6,8 & 9 - Hosted by Always Data

Haidee’s picture

does this work with D6?

progpapa’s picture

The following code worked for me on Drupal 6:

function modulename_form_alter(&$form, &$form_state, $form_id) {
  // Remove the search form from the search result pages.
  if ($form_id == 'search_form' && $_GET['q'] != 'search') {
    unset($form['basic']);
    unset($form['advanced']);
  }
}

Some hidden fields are left in the code, so the entire form is not removed, but your visitors won't see the search form...

AlonGoldberg’s picture

that helped a LOT.
:)

soulston’s picture

Thanks @progpapa and just to clarify this would need to be put in your own module and replace "modulename" with the name of your new module see http://drupal.org/node/206754.

ryndog’s picture

Yes, to clarify this modules works great. However is there a possibly way to add to it to also remove the title/header 'Search', and the below options for catagories to search (mine has 3 buttons: help, users, content)?

michalczernik’s picture

You can remove rest of the search elements within your page.tpl.php.

e.g.

<?php if (!empty($tabs) && (YOUR_CONDITION_GOES_HERE)): ?>
  <div class="tabs-wrapper clearfix">
    <?php print $tabs; ?>
  </div>
<?php endif; ?>

I'm using for my condition:

$alias != 'search/node'
xcono’s picture

Thx a lot!

progpapa’s picture

And I'm facing the same problem again, now I'm using a different solution:

function modulename_form_search_form_alter(&$form, &$form_state) {
  if (arg(1) == 'node') {
    $form['#access'] = FALSE;
  }
}

It works if you are searching for nodes.

Something like

function modulename_form_search_form_alter(&$form, &$form_state) {
  if (isset(arg(1))) {
    $form['#access'] = FALSE;
  }
}

should work too.

It seems to be more elegant and leaves no code in the source.

greg.1.anderson’s picture

To keep the search form on the search page, but remove it on search results page, I used a slight variation on the solution above:

function mymodulename_form_search_form_alter(&$form, &$form_state) {
  // Remove the search form from search result page
  if ((arg(1) == 'node') && (arg(2) != NULL)) {
    $form['#access'] = FALSE;
  }
}

I was working only with nodes, but a more generic conditional would also work. The important part is testing arg(2), which is NULL on the search page, but non-null on search result pages.

peter.thorndycraft’s picture

This method worked perfectly for me, thank you.

woop_light’s picture

you're the man

luisfreire’s picture

Hi, just got into Drupal about 2 weeks ago with a big project and lots of challenges. Loving Drupal by the way.

This hook worked for me:

<?php
function [mytheme]_form_alter(&$variables) {
	
	switch($variables['#action']) {
		case base_path() . 'search/node';
			$variables = array();
			break;	
	}
	
}
?>

NOTE: replace [mytheme] with your theme name

mujuonly’s picture

While rendering content , you can unset search form like this

                        if (!empty($page['content'])):
                            unset($page['content']['system_main']['search_form']);
                            echo render($page['content']);
                        endif;
                        

Is it okay