9
九/08
1

drupal搜索功能扩展



hook_search是用来扩展你搜索功能的hook,在我的术语收藏模块中,要想对术语进行搜索,可进行如下实现。搜索表单的样式我们可以用 hook_form_alter($form_id, &$form)来修改,判断当form_id为搜索表单id时候,对form主题进行覆写。而搜索结果的定制可以使用 hook_search_page($rows)来做,比如我要对搜索结果进行表格格式化输出,代码如下:

<?php
/*
* Implemention of hook_search().
*/
function favorite_terms_search($op = ‘search’, $keys = NULL) {
  switch ($op) {
    case ‘name’:
      if (user_access(‘favorite terms search’)) {
        return t(‘Terms’);
      }

    case ‘search’:
      if (user_access(‘favorite terms search’)) {
        $header = array(t(‘Term’), t(‘Description’), t(‘Operations’));
        // Return to this page after an ‘add favorite’ operation.
        $destination = drupal_get_destination();
        // Replace wildcards with MySQL/PostgreSQL wildcards.
        $keys = preg_replace(‘!\*+!’, ‘%’, $keys);
        $sql = "SELECT * FROM {term_data} WHERE LOWER(name) LIKE LOWER(‘%%%s%%’)";
        $result = pager_query($sql, 20, 0, NULL, $keys);
        while ($term = db_fetch_object($result)) {
          $rows[] = array(
            l($term->name, "taxonomy/term/$term->tid"),
            check_plain($term->description),
            l(t(‘add to favorites’), "favorite_terms/add/term/$term->tid", array(), $destination)
          );
        }

        if (!$rows) {
          $rows[] = array(array(
            ‘data’ => t(‘No matched term found.’),
            ‘colspan’ => ’3′)
          );
        }
        return $rows;
      }
  }
}

function favorite_terms_search_page($rows) {
  drupal_add_css(drupal_get_path(‘module’, ‘favorite_terms’) . ‘/favorite_terms.css’);
  $header = array(t(‘Term’), t(‘Description’), t(‘Operations’));
  $output = theme(‘table’, $header, $rows);
  $output .= theme(‘pager’, NULL, 50, 0);
  $output .= ‘<div class="search_page_add_new">’;
  $output .= t(‘if you did not find a product above, click !here to add a new product’, array(‘!here’ => l(t(‘here’), ‘node/add/product’)));

  $output .= ‘</div>’;

  return $output;
}
?>
 


相关文章

关键字: ,
评论 (1) Trackbacks (0)
  1. 去耳畔
    10:21 on 十一月 8th, 2011

    真有你的哈,好啊啊

发表评论

No trackbacks yet.