2009-09-23 123 views
2

我正在尝试创建自定义搜索但陷入困境。 我想要的是有一个下拉框,以便用户可以选择在哪里搜索。 这些选项可能表示一种或多种内容类型。Drupal:创建自定义搜索

因此,如果他选择选项A,那么搜索将查看节点类型P,Q,R。 但是他可能不会提供这些结果,而只是将用于收集该用户特定数据的uid。

为了让它更清晰一些,假设我想为人们服务。我在搜索的内容是2种内容配置文件类型。但是,当然,你不想将这些结果显示出来,而是显示用户和一些数据的完美图像。

我开始创建一个带有文本框和下拉框的表单。 然后,在提交处理程序中,我创建了密钥并将其重定向到其他页面,并将这些键作为尾部。这个页面已经在菜单钩子中定义,就像搜索一样。

之后,我想致电hook_view通过调用node_search来进行实际搜索,并返回结果。

不幸的是,它出错了。当我点击搜索按钮,它给了我一个404.

但我在正确的轨道上?这是创建自定义搜索的方式吗?

Thx寻求帮助。

下面是一些清晰的代码:

<?php 
// $Id$ 

/* 
* @file 
* Searches on Project, Person, Portfolio or Group. 
*/ 

/** 
* returns an array of menu items 
* @return array of menu items 
*/ 
function vm_search_menu() { 

    $subjects = _vm_search_get_subjects(); 
    foreach ($subjects as $name => $description) { 
    $items['zoek/'. $name .'/%menu_tail'] = array(
     'page callback' => 'vm_search_view', 
     'page arguments' => array($name), 
     'type' => MENU_LOCAL_TASK, 
    ); 
    } 
    return $items; 
} 


/** 
* create a block to put the form into. 
* @param $op 
* @param $delta 
* @param $edit 
* @return mixed 
*/ 
function vm_search_block($op = 'list', $delta = 0, $edit = array()) { 
    switch ($op) { 
    case 'list': 
     $blocks[0]['info'] = t('Algemene zoek'); 
     return $blocks; 
    case 'view': 
     if (0 == $delta) { 
      $block['subject'] = t(''); 
      $block['content'] = drupal_get_form('vm_search_general_form'); 
     } 
     return $block; 
    } 
} 

/** 
    * Define the form. 
    */ 
function vm_search_general_form() { 
    $subjects = _vm_search_get_subjects(); 
    foreach ($subjects as $key => $subject) { 
    $options[$key] = $subject['desc']; 
    } 

    $form['subjects'] = array(
     '#type' => 'select', 
    '#options' => $options, 
    '#required' => TRUE, 
    ); 
    $form['keys'] = array(
    '#type' => 'textfield', 
    '#required' => TRUE, 
); 
    $form['submit'] = array(
     '#type' => 'submit', 
     '#value' => t('Zoek'), 
    ); 
    return $form; 
} 


function vm_search_general_form_submit($form, &$form_state) { 
    $subjects = _vm_search_get_subjects(); 
    $keys = $form_state['values']['keys']; //the search keys 
    //the content types to search in 
    $keys .= ' type:' . implode(',', $subjects[$form_state['values']['subjects']]['types']); 



    //redirect to the page, where vm_search_view will handle the actual search 
    $form_state['redirect'] = 'zoek/'. $form_state['values']['subjects'] .'/'. $keys; 
} 


/** 
* Menu callback; presents the search results. 
*/ 
function vm_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($front_page); 
    } 

    $keys = search_get_keys(); 
    // Only perform search if there is non-whitespace search term: 
    $results = ''; 
    if (trim($keys)) { 
     // Log the search keys: 
     watchdog('vm_search', '%keys (@type).', array('%keys' => $keys, '@type' => $type)); 

     // Collect the search results: 
     $results = node_search('search', $type); 

     if ($results) { 
     $results = theme('box', t('Zoek resultaten'), $results); 
     } 
     else { 
     $results = theme('box', t('Je zoek heeft geen resultaten opgeleverd.')); 
     } 
    } 
    } 
    return $results; 
} 


/** 
* returns array where to look for 
* @return array 
*/ 
function _vm_search_get_subjects() { 
    $subjects['opdracht'] = 
    array('desc' => t('Opdracht'), 
      'types' => array('project') 
     ); 
    $subjects['persoon'] = 
     array('desc' => t('Persoon'), 
      'types' => array('types_specialisatie', 'smaak_en_interesses') 
     ); 
    $subjects['groep'] = 
    array('desc' => t('Groep'), 
     'types' => array('Villamedia_groep') 
     ); 
    $subjects['portfolio'] = 
    array('desc' => t('Portfolio'), 
      'types' => array('artikel') 
     ); 
    return $subjects; 
} 

回答

0

说实话,我还没有看到很多人实现hook_search。大多数只使用Views,或者,对于高级的东西,像Faceted Search

您是否考虑过使用您当前的项目?为什么它不工作?

+0

我做过了,我使用其他自定义搜索的视图,但这对性能不利。 分面搜索我还没有找到。 – eddy147

+0

这是相当不错的 - 整个分面搜索概念也是如此。如果你担心性能,你也可以看看Search Lucene或Apache Solr。 –

+0

顺便说一下,分面搜索也会钩入视图,因此您可以使用视图来显示搜索结果。 –

0

您也可以将hook_menu用于结果,将db_queries用于您的自定义(以及优化如此之快)的查询。

例如:

搜索/%/%

这里的参数可以是任何你所需要的,例如第一个最低的价格,第二个价格的最高价格,第三为最小的卧室...您的网址看起来总是这样:

搜索/ 200/400 /空/ 3/...

我使用了一个空,但它可以是任何东西,你喜欢考虑这个领域为空。

然后,从您选择的表格中,您只需按照此url的结构重新定向并在正确的位置添加参数。

这是probalby不是最美丽的方式来建立一个url,但使用这种技术和hook_theme会让你有无限的灵活性。我可以告诉你一个项目,我们正在使用这种技术,我认为它看起来不错:-)。

有关这方面的任何评论将被大大提升:-)。