【问题标题】:Search hook for filtering results?过滤结果的搜索钩子?
【发布时间】:2010-08-06 17:50:31
【问题描述】:

我一直在浏览文档和源代码,寻找一些没有运气的东西。

是否有一个 Drupal 6 钩子在 hook_search() 之后被调用,但在 $results 被移交给模板系统之前?

我需要对返回的结果进行相当自定义的修剪和重新排序。我可以重新实现 hook_search(),但这似乎有点过头了。

谢谢。

【问题讨论】:

    标签: drupal drupal-6 drupal-hooks


    【解决方案1】:

    没有; 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);
            }
          }
        }
      }
    }
    

    【讨论】:

    • 谢谢。我认为重新实现 hook_search() 对于我们的情况来说是“正确的”事情。
    【解决方案2】:

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

    【讨论】:

    • 谢谢。到调用此钩子时,结果已经分页,因此您只能剔除和/或重新排序一页的价值。
    【解决方案3】:

    Hook 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,
          );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-02
      • 2012-07-05
      • 2017-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多