6

I have a search box in a sidebar and I'm happy with it. I don't want Drupal to output another one on the results page. So far I've managed to remove it with the following code

function mythemename_preprocess_page(&$variables) {
    if(array_key_exists('page', $variables)) {
        if(array_key_exists('content', $variables['page'])) {
            if(array_key_exists('system_main', $variables['page']['content'])) {
                if(array_key_exists('search_form', $variables['page']['content']['system_main'])) {
                    unset($variables['page']['content']['system_main']['search_form']);
                }
            }
        }
    }
}

but it doesn't look very clean to me. Is there another way?

5

Write a form alter:

function module_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == "search_form") {
     $form['#type'] = 'hidden';
  }
}
  • Sure, but that requires a module. This should be a templating issue. – Morpheu5 Jan 10 '12 at 11:24
  • @Morpheu5, you can call the function mytheme_form_alter and put it in template.php of your theme, thus you have a possibility not to use any module – Alex K Jul 1 '13 at 11:35
  • @Alex unfortunately, no matter how hard I tried, mytheme_form_alter did never get registered and called. – Morpheu5 Jul 3 '13 at 17:51
  • Above code doesn't work. You need to change the 3rd line into $form['#access'] = FALSE; – timofey.com Jan 31 '14 at 9:09
4

There is the exact same question in drupal.org, and your solution seems to be similar - but with more code - with this. Keep in mind that there is always the option of using css to target specific elements and make them not display (i.e. display: none;).

  • 1
    Using isset() instead of !empty() and hide() instead of unset() is probably better practice. – mpdonadio Nov 26 '11 at 21:14
  • Hiding stuff via css has the obvious disadvantage of leaving it in the code, which is something you don't want if you're going to target screen readers and stuff like that. hide() seems to be better than unset() and I'm probably going for that, but I'm still surprised there's no easier way to control such an important feature. – Morpheu5 Nov 26 '11 at 23:19
3

search_view() is the function that builds the search page. So Another way is to implement thehook_menu_alter() and change page callback from search_view to your own function. You can copy the search_view's body to your function and remove the following line:

$build['search_form'] = drupal_get_form('search_form', NULL, $keys, $info['module']);

Of course, this also doesn't seem to be very clean!

1

In Drupal 7 You can use hook_page_alter: http://drupalcontrib.org/api/drupal/drupal%21modules%21system%21system.api.php/function/hook_page_alter/7

/**
 * implements hook_page_alter()
 *
 * Alter the page output before render.
*/
function NAME_OF_YOUR_THEME_page_alter(&$page) {
  // kpr($page); //use this to find the item you want to remove - you need the devel running.
  // Remove the search form from the search results page.
  if (arg(0) == 'search') {
    if (!empty($page['content']['system_main']['search_form'])) {
      hide($page['content']['system_main']['search_form']);
    }
  }
}

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.