drupal - 用于过滤结果的搜索 Hook ?

标签 drupal drupal-6 drupal-hooks

我一直在浏览文档和源代码,寻找一些不幸的东西。

是否有一个 Drupal 6 钩子(Hook)在 hook_search() 之后、$results 传递到模板系统之前被调用?

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

谢谢。

最佳答案

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

关于drupal - 用于过滤结果的搜索 Hook ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3426366/

相关文章:

mysql - 开发显示查询

drupal - 在 html.tpl.php 中获取 $node 变量 - Drupal 7

php - Drupal:子主题 CSS 不起作用

drupal-6 - 阻止 Robots.txt 中的特定页面

php - 用于 Drupal 6 的 Ubuntu 服务器 11.10 和 PHP 5.2

php - 是什么将 Drupal Hook 与特定模块联系起来?

Drupal 8 添加 javascript 并在带有自定义模块的钩子(Hook)中传递数据

php - 如何在 Linux 中激活 Drupal Cron 任务?

drupal - 当用户在 webform 中提交值并单击提交时如何创建节点?