Drupal - 搜索框无法正常工作 - 自定义主题模板

时间:2010-05-22 10:17:40

标签: search drupal-6 drupal-theming drupal-templates

我使用的是search-theme-form.tpl的自定义版本 当我使用搜索框时,我会转移到搜索页面。但搜索实际上并没有发生。搜索结果页面上的搜索框确实有效。这是我的search-them-form.tpl.php文件(demo

<input type="text" name="search_theme_form_keys" id="edit-search-theme-form-keys" value="Search" title="Enter the terms you wish to search for" class="logininput" height="24px" onblur="restoreSearch(this)" onfocus="clearInput(this)" />
  <input type="submit" name="op" id="edit-submit" value="" class="form-submit" style="display: none;" />
  <input type="hidden" name="form_token" id="edit-search-theme-form-form-token" value="<?php print drupal_get_token('search_theme_form'); ?>" />
  <input type="hidden" name="form_id" id="edit-search-theme-form" value="search_theme_form" />

还涉及一个javascript文件。我想从代码中可以清楚地看到它的使用:

 function trim(str) {  
     return str.replace(/^\s+|\s+$/g, '');  
 }  

 function clearInput(e) {  

        e.value="";                // clear default text when clicked  
    e.className="longininput_onfocus"; //change class

 }  

 function restoreSearch(e) {  
    if (trim(e.value) == '') {  
        {
   e.value="Search";             // reset default text onBlur 
         e.className="logininput";        //reset class
  } 
    }  
 }

可能是什么问题以及如何解决?

1 个答案:

答案 0 :(得分:5)

显然,您无法直接修改search-theme-form.tpl.php中的HTML,因为这不是正确的方法。所以我添加了类和onFocus以及onBlur属性是问题所在。

正确的方法是修改主题template.php文件。基本上我们将使用form_alter()来修改表单元素。由于使用HTML方式是错误的。请查看以下代码(摘自:here

<?php
/**
* Override or insert PHPTemplate variables into the search_theme_form template.
*
* @param $vars
*   A sequential array of variables to pass to the theme template.
* @param $hook
*   The name of the theme function being called (not used in this case.)
*/
function yourthemename_preprocess_search_theme_form(&$vars, $hook) {
  // Note that in order to theme a search block you should rename this function
  // to yourthemename_preprocess_search_block_form and use
  // 'search_block_form' instead of 'search_theme_form' in the customizations
  // bellow.

  // Modify elements of the search form
  $vars['form']['search_theme_form']['#title'] = t('');

  // Set a default value for the search box
  $vars['form']['search_theme_form']['#value'] = t('Search this Site');

  // Add a custom class and placeholder text to the search box
  $vars['form']['search_theme_form']['#attributes'] = array('class' => 'NormalTextBox txtSearch', 
                                                              'onfocus' => "if (this.value == 'Search this Site') {this.value = '';}",
                                                              'onblur' => "if (this.value == '') {this.value = 'Search this Site';}");

  // Change the text on the submit button
  //$vars['form']['submit']['#value'] = t('Go');

  // Rebuild the rendered version (search form only, rest remains unchanged)
  unset($vars['form']['search_theme_form']['#printed']);
  $vars['search']['search_theme_form'] = drupal_render($vars['form']['search_theme_form']);

  $vars['form']['submit']['#type'] = 'image_button';
  $vars['form']['submit']['#src'] = path_to_theme() . '/images/search.jpg';

  // Rebuild the rendered version (submit button, rest remains unchanged)
  unset($vars['form']['submit']['#printed']);
  $vars['search']['submit'] = drupal_render($vars['form']['submit']);

  // Collect all form elements to make it easier to print the whole form.
  $vars['search_form'] = implode($vars['search']);
}
?>

yourthemename_preprocess_search_theme_form - 'yourthemename'显然会反映您的自定义主题的名称。基本上代码是不言自明的。什么评论和所有。

所以,基本上就是它的工作方式。