Drupal:在高级搜索中重新选择分类术语

时间:2009-12-16 20:00:59

标签: search drupal drupal-6

我确信我不是第一个尝试解决这个问题的人,但谷歌对我没有任何帮助。

如果您使用Drupal中的高级搜索过滤分类术语,搜索表单会在关键字文本框中返回术语ID,如下所示:

search phrase category:32,33

在分类选择框中未再次选择所选值。

我希望在分类标准选择框中选择它们,而不是在关键字文本框中显示它们 - 这是任何用户希望这样的表单行动的方式。我一直在寻找一个可以添加此功能的模块,但无济于事。我已尝试实施hook_form_alter()根据之前的表单提交(在#default_value arg中提供)在该表单元素上设置$form_state,但a)这似乎是kludgy而b)似乎调用此函数一次以验证表单并再次重新显示它,并且第二次提交的值不可用(这是在需要时)。

有什么建议吗?

2 个答案:

答案 0 :(得分:3)

它花了比它应该的更长的时间,但我终于想出了如何做到这一点。我稍后可能会在Drupal站点上发布一个模块,但是如果其他人遇到同样的问题,这就是我遇到的解决方案。

创建一个模块并使用hook_form_alter()来修改表单。我已经有一个模块用于自定义高级搜索,所以我把它放在那里。我不会详细介绍构建自己的模块 - 你可以找到一个简单的教程,你只需要定义这个函数。

/**
 * Implementation of hook_form_alter().
 * Remove 'category:x,y,z' from the keyword textbox and select them in the taxonomy terms list
 */
function modulename_form_alter(&$form, $form_state, $form_id) {
    // Advanced node search form
    if ($form_id == 'search_form' && $form['module']['#value'] == 'node' && user_access('use advanced search')) {
        // Remove category:x,y,z from the keyword box
        $searchPhrase = $form['basic']['inline']['keys']['#default_value'];
        if(!empty($searchPhrase) && strpos($searchPhrase, 'category:') !== false) {
            $searchWords = explode(' ', $form['basic']['inline']['keys']['#default_value']);
            foreach($searchWords as $index=>$word) {
                if(strpos($word, 'category:') === 0) {
                    // Use the value to set the default on the taxonomy search
                    $word = substr($word, strlen('category:'));
                    $form['advanced']['category']['#default_value'] = explode(',', $word);
                    // Remove it from the keyword textbox
                    unset($searchWords[$index]);
                }
            }

            // Re-set the default value for the text box without the category: part
            $form['basic']['inline']['keys']['#default_value'] = implode(' ', $searchWords);
        }
    }
}

答案 1 :(得分:1)

感谢您分享此内容。经过几次drupal安装后,我现在也面临着这个问题。但是,这次我不得不使用search_config和search_files等搜索扩展来满足客户端的要求。此外,我正在使用better_select将选择列表转换为复选框列表(更容易在长列表中选择多个值)。因此,复选框总是返回一些值(如果未选中,则为id或0(零))我最终得到类似“搜索短语类别:0,0,0,0,...,0”的内容。

上面的解决方案确实从关键字搜索字段中删除了“category:0,0,0,0,0,0,...,0”字符串,并正确检查所有分类术语。对于内容类型也可以这样做,你只需要在整个脚本中用“type:”替换“category:”。