扩展Drupal 7搜索

Vla*_*lat 4 search drupal-7

我想用一个额外的字段扩展默认的Drupal 7节点搜索.

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

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'
        )
    );
}
Run Code Online (Sandbox Code Playgroud)

我有一个字段field_data_field_site.field_site_value,我需要在此搜索中用作过滤器.

我试过阅读有关hook_search_*功能但没有得到这个想法.

我的问题如下.如何扩展搜索表单?有人有现场例子吗?

Vla*_*lat 11

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

首先,我需要使用我的字段更改Drupal的搜索块和搜索表单,并定义新的提交功能.

/**
 * 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'
    );
}
Run Code Online (Sandbox Code Playgroud)

提交功能会将我们转发到默认search/node页面但我们的查询.页面看起来像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);
}
Run Code Online (Sandbox Code Playgroud)

下一步是使用hook_search_info(不要忘记将其打开并在admin/config/search/settings页面上设置为默认值).

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

条件回调函数定义于hook_search_info.我们需要为搜索提供其他查询.

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

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

/**
 * 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;
}
Run Code Online (Sandbox Code Playgroud)

如果有人能给我一个更好的答案,我会很高兴.