Drupal搜索结果页面中会出现一个搜索表单,与置顶的表单有重复的不好之处,另外,我也想在AMP页面等特殊情况下去掉搜索结果页中的表单。
以前好些在网上搜过有修改CSS的办法来隐藏表单的显示,今天又找到Drupal的hook_form_alter这个API调用办法,测试有效,记录如下:
function my_module_name_form_alter(&$form, &$form_state, $form_id) { // We have to be careful to alter only the contact site form, // as this hook is used for every form the system uses. if($form_id == 'contact_site_form'){//修改联系我们表单 $form['referer'] = array( '#type' => 'hidden', '#title' => t('REFERER'), '#required' => FALSE, '#maxlength' => 255, '#default_value' => $_SERVER['HTTP_REFERER'], ); } if ($form_id == 'search_form' && $_GET['q'] != 'search') {//隐藏搜索表单 unset($form['basic']); unset($form['advanced']); } }
还有帖子说调用其它的API,例如:
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']); } }
这个办法没有试过,记录下来备用。
评论2
我一般是创建一个 view page 来代替默认搜索页。
我一般是创建一个 view page 来代替默认搜索页。嗯
嗯,我们在有些情况下也用views来替代drupal原带的搜索。