扩展Drupal 7搜索 [英] Extending Drupal 7 search

查看:219
本文介绍了扩展Drupal 7搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想使用以下新字段更改搜索表单:

  function mymodule_form_search_form_alter(& $ form,& $ form_state,$ form_id){
$ form ['basic' ] ['site'] = array(
'#type'=>'select',
'#options'=>数组(
'KEY1'=>'TITLE1' ,
'KEY2'=>'TITLE2',
'KEY3'=>'TITLE3'

);我有一个名为 field_data_field_site.field_site_value的字段
我需要在此搜索中用作过滤器。



我试图阅读关于 hook_search _ * 函数,但没有得到这个想法。



我的问题如下。如何扩展搜索表单?任何人都有实例吗?

解决方案

以下是解决这个问题的最佳方法。



首先我需要用我的字段来改变Drupal的搜索框和搜索表单,并定义新的提交函数。

  / ** 
*实现hook_form_FORM_ID_alter()。
* /
函数mymodule_form_search_block_form_alter(& $ form,& $ form_state,$ form_id){
$ form ['#submit'] [] ='search_form_alter_submit'

$ form ['site'] = array(
'#type'=>'select',
'#options'=> _options(),
'#default_value'=>(($ _GET ['site'])?$ _GET ['site']:'')
);
}

/ **
*实现hook_form_FORM_ID_alter()。
* /
函数mymodule_form_search_form_alter(& $ form,& $ form_state,$ form_id){
$ form ['#submit'] [] ='search_form_alter_submit'

$ form ['basic'] ['site'] = array(
'#type'=>'select',
'#options'=> _options (),
'#default_value'=>(($ _GET ['site'])?$ _GET ['site']:'')
);
}

函数_options(){
返回数组(
''=>'选择站点',
'site-1'=> ;'站点1',
'site-2'=>'站点2'
);
}

提交功能将转发我们到默认的搜索/节点页面,但与我们的查询。页面看起来像 search / node / Our-query-string?site =我们的选项选择

  function search_form_alter_submit($ form,& $ form_state){
$ path = $ form_state ['redirect'];
$ options = array(
'query'=>数组(
'site'=> $ form_state ['values'] ['site']

);
drupal_goto($ path,$ options);
}

下一步是使用 hook_search_info (不要忘了打开它,并设置为 admin / config / search / settings 页面)。

  / ** 
*实现hook_search_info()。
* /
函数mymodule_search_info(){
return array(
'title'=>'Content',
'path'=>'node'
'conditions_callback'=>'_conditions_callback',
);
}

条件回调函数在中定义hook_search_info 。我们需要为我们的搜索提供其他查询。

  function _conditions_callback($ keys){
$ conditions = array();
if(!empty($ _ REQUEST ['site'])){
$ conditions ['site'] = $ _REQUEST ['site'];
}
返回$条件;
}

最后, hook_search_execute 将通过我们的查询过滤我们的内容。我使用这个钩子的默认代码,需要修改。

  / ** 
*实现hook_search_execute()。
* /
函数mymodule_search_execute($ keys = NULL,$ conditions = NULL){
//构建匹配条件
$ query = db_select('search_index','i' array('target'=>'slave'))
- > extend('SearchQuery')
- > extend('PagerDefault');

$ query-> join('node','n','n.nid = i.sid');

//这里我的过滤器我加入另一个表,
//按必填字段过滤
$ site =(isset($ conditions ['site']))? $ conditions ['site']:NULL;
if($ site){
$ query-> leftJoin('field_data_field_site','s','s.entity_id = i.sid');
$ query-> condition('s.field_site_value',$ site);
}
//过滤器结束

$ query
- >条件('n.status',1)
- > addTag ('node_access')
- > searchExpression($ keys,'node');

//插入特殊关键字。
$ query-> setOption('type','n.type');
$ query-> setOption('language','n.language');
if($ query-> setOption('term','ti.tid')){
$ query-> join('taxonomy_index','ti','n.nid = ti .nid');
}

//仅当第一遍查询匹配时才继续。
if(!$ query-> executeFirstPass()){
return array();
}

//添加排名表达式。
_node_rankings($ query);

//加载结果。
$ find = $ query
- > limit(10)
- > execute();

$ results = array();
foreach($ find as $ item){
//构建节点体。
$ node = node_load($ item-> sid);
node_build_content($ node,'search_result');
$ node-> body = drupal_render($ node-> content);

//获取代码段的注释。
$ node-> rendered。=''。 module_invoke('comment','node_update_index',$ node);
//获取片段的条款。
$ node-> rendered。=''。 module_invoke('taxonomy','node_update_index',$ node);

$ extra = module_invoke_all('node_search_result',$ node);

$ results [] = array(
'link'=> url(node / {$ item-> sid},array('absolute'=> TRUE) )
'type'=> check_plain(node_type_get_name($ node)),
'title'=> $ node-> title,
'user'=> theme 'username',array('account'=> $ node)),
'date'=> $ node->已更改,
'node'=> $ node,
'extra'=> $ extra,
'score'=> $ item-> calculated_score,
'snippet'=> search_excerpt($ keys,$ node-> body)
);
}

return $ results;
}



如果有人给我一个更好的答案,我会很高兴。 h2>

I want to extend default Drupal 7 node search with one additional field.

I alter search form with the following new field:

function mymodule_form_search_form_alter(&$form, &$form_state, $form_id) {
    $form['basic']['site'] = array(
        '#type' => 'select',
        '#options' => array(
            'KEY1' => 'TITLE1',
            'KEY2' => 'TITLE2',
            'KEY3' => 'TITLE3'
        )
    );
}

I have a field called field_data_field_site.field_site_value which i need to use as a filter in this search.

I've tried to read about hook_search_* functions but didn't get the idea.

My question is the following. How can I extend search form? Anyone have live examples?

解决方案

The following is the best way I solve this problem.

First of all I need to alter Drupal's search block and search form with my field and define new submit function.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_search_block_form_alter(&$form, &$form_state, $form_id) {
    $form['#submit'][] = 'search_form_alter_submit';

    $form['site'] = array(
        '#type' => 'select',
        '#options' => _options(),
        '#default_value' => (($_GET['site']) ? $_GET['site'] : '')
    );
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mymodule_form_search_form_alter(&$form, &$form_state, $form_id) {
    $form['#submit'][] = 'search_form_alter_submit';

    $form['basic']['site'] = array(
        '#type' => 'select',
        '#options' => _options(),
        '#default_value' => (($_GET['site']) ? $_GET['site'] : '')
    );
}

function _options() {
    return array(
        '' => 'Select site',
        'site-1' => 'Site 1',
        'site-2' => 'Site 2'
    );
}

Submit function will forward us to default search/node page but with our query. Page would look like search/node/Our-query-string?site=Our-option-selected.

function search_form_alter_submit($form, &$form_state) {
    $path = $form_state['redirect'];
    $options = array(
        'query' => array(
            'site' => $form_state['values']['site']
        )
    );
    drupal_goto($path, $options);
}

Next step is to use hook_search_info (Don't forget to turn it on and set as default on admin/config/search/settings page).

/**
 * Implements hook_search_info().
 */
function mymodule_search_info() {
    return array(
        'title' => 'Content',
        'path' => 'node',
        'conditions_callback' => '_conditions_callback',
    );
}

Conditions callback function defined in hook_search_info. We need to provide additional queries to our search.

function _conditions_callback($keys) {
    $conditions = array();
    if (!empty($_REQUEST['site'])) {
        $conditions['site'] = $_REQUEST['site'];
    }
    return $conditions;
}

Finally, hook_search_execute will filter our content by our query. I used default code from this hook with modifications I need.

/**
 * Implements hook_search_execute().
 */
function mymodule_search_execute($keys = NULL, $conditions = NULL) {
    // Build matching conditions
    $query = db_select('search_index', 'i', array('target' => 'slave'))
        ->extend('SearchQuery')
        ->extend('PagerDefault');

    $query->join('node', 'n', 'n.nid = i.sid');

    // Here goes my filter where I joined another table and
    // filter by required field
    $site = (isset($conditions['site'])) ? $conditions['site'] : NULL;
    if ($site) {
        $query->leftJoin('field_data_field_site', 's', 's.entity_id = i.sid');
        $query->condition('s.field_site_value', $site);
    }
    // End of my filter

    $query
        ->condition('n.status', 1)
        ->addTag('node_access')
        ->searchExpression($keys, 'node');

    // Insert special keywords.
    $query->setOption('type', 'n.type');
    $query->setOption('language', 'n.language');
    if ($query->setOption('term', 'ti.tid')) {
        $query->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
    }

    // Only continue if the first pass query matches.
    if (!$query->executeFirstPass()) {
        return array();
    }

    // Add the ranking expressions.
    _node_rankings($query);

    // Load results.
    $find = $query
        ->limit(10)
        ->execute();

    $results = array();
    foreach ($find as $item) {
        // Build the node body.
        $node = node_load($item->sid);
        node_build_content($node, 'search_result');
        $node->body = drupal_render($node->content);

        // Fetch comments for snippet.
        $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node);
        // Fetch terms for snippet.
        $node->rendered .= ' ' . module_invoke('taxonomy', 'node_update_index', $node);

        $extra = module_invoke_all('node_search_result', $node);

        $results[] = array(
            'link' => url("node/{$item->sid}", array('absolute' => TRUE)),
            'type' => check_plain(node_type_get_name($node)),
            'title' => $node->title,
            'user' => theme('username', array('account' => $node)),
            'date' => $node->changed,
            'node' => $node,
            'extra' => $extra,
            'score' => $item->calculated_score,
            'snippet' => search_excerpt($keys, $node->body)
        );
    }

    return $results;
}

I'd be happy if anyone would give me a better answer.

这篇关于扩展Drupal 7搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆