2010-05-22 59 views
1

我正在使用search-theme-form.tpl的自定義版本 當我使用搜索框時,我確實轉到了搜索頁面。但搜索實際上並沒有發生。搜索結果頁面上的搜索框確實可以工作。這是我搜索了他們,form.tpl.php文件(demoDrupal - 搜索框不工作 - 自定義主題模板

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

什麼可能是這個問題,我該如何解決?

+0

你的意思是'search-theme-form.tpl.php'(而不是'...- from.tpl')?您是否使用默認模板獲取搜索結果?你有沒有爲你的網站建立索引的cron工作?您是否爲用戶設置了允許搜索的權限?請參閱http://drupal.org/handbook/modules/search。 – 2010-05-22 11:31:32

+0

對不起,它是search-theme-form.tpl.php。我的錯。 是的,用戶有權搜索和正確索引的網站。我嘗試了花環主題,似乎完美的工作。 – bcosynot 2010-05-22 14:01:39

+0

看看你的例子,至少表單動作('/ whackk /')是有問題的,因爲原始的Drupal搜索表單指向'search/[searchTerm]'。主要的問題是,你通過HTML標記從頭開始構建自己的表單,完全繞過Drupal Forms API。這不被推薦。你想達到什麼標準的Drupal搜索更改? (有很多方法可以根據自己的喜好調整和調整任何Drupal表單,但爲了得到正確的答案,我需要更多關於您的目標的信息) – 2010-05-24 20:20:26

回答

5

顯然,你不能直接修改HTML中search-theme-form.tpl.php,因爲那不是做正確的方式。所以我加入類的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」將明顯地反映出您的自定義主題的名稱。基本上,代碼是不言自明的。什麼與評論和所有。

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

+1

+1用於跟進/回答自己的問題 – 2010-06-03 14:00:02