2010-08-06 72 views
2

我一直在瀏覽文檔和源代碼,尋找的東西沒有運氣。篩選結果的搜索掛鉤?

是否有一個Drupal 6鉤子在hook_search()之後被調用,但在$ results被傳遞給模板系統之前被調用?

我需要做一個相當自定義的修剪和重新排序結果返回。我可以重新實現hook_search(),但這似乎是矯枉過正。

謝謝。

回答

3

沒有; search_view()(它顯示結果)調用search_data(),調用hook_search()然後立即主題化結果。重新實施hook_search()可能是最直接的路線。

這樣說,你可以改爲hook_menu_alter()並讓搜索頁面調用你的自定義函數,而不是調用search_view()(隨後調用search_data())。例如:

function test_menu_alter(&$items) { 
    $items['search']['page callback'] = 'test_search_view'; 

    foreach (module_implements('search') as $name) { 
    $items['search/' . $name . '/%menu_tail']['page callback'] = 'test_search_view'; 
    } 
} 

// Note: identical to search_view except for --- CHANGED --- 
function test_search_view($type = 'node') { 
    // Search form submits with POST but redirects to GET. This way we can keep 
    // the search query URL clean as a whistle: 
    // search/type/keyword+keyword 
    if (!isset($_POST['form_id'])) { 
    if ($type == '') { 
     // Note: search/node can not be a default tab because it would take on the 
     // path of its parent (search). It would prevent remembering keywords when 
     // switching tabs. This is why we drupal_goto to it from the parent instead. 
     drupal_goto('search/node'); 
    } 

    $keys = search_get_keys(); 
    // Only perform search if there is non-whitespace search term: 
    $results = ''; 
    if (trim($keys)) { 
     // Log the search keys: 
     watchdog('search', '%keys (@type).', array('%keys' => $keys, '@type' => module_invoke($type, 'search', 'name')), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys)); 

     // Collect the search results: 
     // --- CHANGED --- 
     // $results = search_data($keys, $type); 
     // Instead of using search_data, use our own function 
     $results = test_search_data($keys, $type); 
     // --- END CHANGED --- 

     if ($results) { 
     $results = theme('box', t('Search results'), $results); 
     } 
     else { 
     $results = theme('box', t('Your search yielded no results'), search_help('search#noresults', drupal_help_arg())); 
     } 
    } 

    // Construct the search form. 
    $output = drupal_get_form('search_form', NULL, $keys, $type); 
    $output .= $results; 

    return $output; 
    } 

    return drupal_get_form('search_form', NULL, empty($keys) ? '' : $keys, $type); 
} 

// Note: identical to search_data() except for --- CHANGED --- 
function test_search_data($keys = NULL, $type = 'node') { 

    if (isset($keys)) { 
    if (module_hook($type, 'search')) { 
     $results = module_invoke($type, 'search', 'search', $keys); 
     if (isset($results) && is_array($results) && count($results)) { 
     // --- CHANGED --- 
     // This dsm() is called immediately after hook_search() but before 
     // the results get themed. Put your code here. 
     dsm($results); 
     // --- END CHANGED --- 

     if (module_hook($type, 'search_page')) { 
      return module_invoke($type, 'search_page', $results); 
     } 
     else { 
      return theme('search_results', $results, $type); 
     } 
     } 
    } 
    } 
} 
+0

感謝。我認爲重新實現hook_search()是我們情況的「正確」事情。 – mpdonadio 2010-08-06 18:44:20

1

您可以使用hook_search_page()重新排序或格式化搜索結果。

+1

謝謝。在這個鉤子被調用的地方,結果已經被分頁,所以你只能剔除和/或重新排列一個頁面的價值。 – mpdonadio 2011-03-04 21:48:52

0

鉤子search_execute允許你以你需要的方式修改查詢。你甚至可以觸發與自定義的SQL新的查詢,例如:

function mymodule_search_execute($keys = NULL, $conditions = NULL) { 

     // Do some query here. 
     $result = my_fancy_query(); 

     // Results in a Drupal themed way for search. 
     $results[] = array(
     'link' => (string) $result->U, 
     'title' => $title, 
     'snippet' => $snippet, 
     'keys' => check_plain($keys), 
     'extra' => array($extra), 
     'date' => NULL, 
    );