我正在嘗試創建自定義搜索但陷入困境。 我想要的是有一個下拉框,以便用戶可以選擇在哪裏搜索。 這些選項可能表示一種或多種內容類型。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。 –
順便說一下,分面搜索也會鉤入視圖,因此您可以使用視圖來顯示搜索結果。 –