我正在尝试创建自定义搜索但陷入困境。 我想要的是有一个下拉框,以便用户可以选择在哪里搜索。 这些选项可能表示一种或多种内容类型。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;
}
我做过了,我使用其他自定义搜索的视图,但这对性能不利。 分面搜索我还没有找到。 – eddy147
这是相当不错的 - 整个分面搜索概念也是如此。如果你担心性能,你也可以看看Search Lucene或Apache Solr。 –
顺便说一下,分面搜索也会钩入视图,因此您可以使用视图来显示搜索结果。 –