搜索钩子过滤结果? [英] Search hook for filtering results?
问题描述
有没有在hook_search()之后调用的Drupal 6钩子,但是之前$结果被交付给模板系统?
我需要对返回的结果进行相当自定义的修剪和重新排序。我可以重新执行hook_search(),但这似乎是过分的。
谢谢。
没有; search_view()
(其中调用结果)调用 search_data()
,它调用 hook_search()
然后立即主题结果。重新实现 hook_search()
可能是最简单的路线。
a href =http://api.drupal.org/api/function/hook_menu_alter/6 =nofollow noreferrer> hook_menu_alter()
和让搜索页面调用您的自定义函数,而不是调用 search_view()
(随后调用 search_data()
)。如下:
function test_menu_alter(& $ items){
$ items ['search'] ['page callback'] ='test_search_view';
foreach(module_implements('search')as $ name){
$ items ['search /'。 $名称。 '/%menu_tail'] ['page callback'] ='test_search_view';
}
}
//注意:与...相同,除了--- CHANGED ---
函数test_search_view($ type ='node'){
//搜索表单提交POST,但重定向到GET。这样我们可以保持
//搜索查询URL清理为口哨:
// search / type / keyword + keyword
if(!isset($ _ POST ['form_id']) ){
if($ type ==''){
//注意:搜索/节点不能是默认选项卡,因为它将在其父项的
//路径上搜索)。当
//切换选项卡时,会阻止记住关键字。这就是为什么我们从父母drupal_goto它。
drupal_goto('search / node');
}
$ keys = search_get_keys();
//如果存在非空白搜索项,则仅执行搜索:
$ results ='';
if(trim($ keys)){
//记录搜索键:
watchdog('search','%keys(@type)'',array('%keys' ='$','@type'=> module_invoke($ type,'search','name')),WATCHDOG_NOTICE,l(t('results'),'search /'$ type。 。$ keys));
//收集搜索结果:
// --- CHANGED ---
// $ results = search_data($ keys,$ type);
//而不是使用search_data,使用我们自己的函数
$ results = test_search_data($ keys,$ type);
// --- END CHANGED ---
if($ results){
$ results = theme('box',t('Search results'),$结果);
}
else {
$ results = theme('box',t('你的搜索没有结果'),search_help('search#noresults',drupal_help_arg()));
}
}
//构造搜索表单。
$ output = drupal_get_form('search_form',NULL,$ keys,$ type);
$ output。= $ results;
return $ output;
}
返回drupal_get_form('search_form',NULL,空($ keys)?'':$ keys,$ type);
}
//注意:除了--- CHANGED ---
函数test_search_data($ keys = NULL,$ type ='node')之外,与search_data {
if(isset($ keys)){
if(module_hook($ type,'search')){
$ results = module_invoke($ type,'search' ,'search',$ keys);
if(isset($ results)&& is_array($ results)&& count($ results)){
// --- CHANGED ---
//这个dsm()在hook_search()之后立即被调用,但在
//之前被调用。把你的代码放在这里
dsm($ results);
// --- END CHANGED ---
if(module_hook($ type,'search_page')){
return module_invoke($ type,'search_page',$结果);
}
else {
return theme('search_results',$ results,$ type);
}
}
}
}
}
I have been going through the docs and source code looking for something without luck.
Is there a Drupal 6 hook that gets called after hook_search(), but before the $results gets handed off to the template system?
I need to do a fairly custom pruning and reordering of results that get returned. I could just reimplement hook_search(), but this seems like overkill.
Thanks.
There isn't; search_view()
(which displays the results) calls search_data()
, which invokes hook_search()
then immediately themes the results. Re-implementing hook_search()
is probably the most straightforward route.
With that said, you could instead implement hook_menu_alter()
and have the search page call your custom function instead of calling search_view()
(and subsequently calling search_data()
). Something like:
function test_menu_alter(&$items) {
$items['search']['page callback'] = 'test_search_view';
foreach (module_implements('search') as $name) {
$items['search/' . $name . '/%menu_tail']['page callback'] = 'test_search_view';
}
}
// Note: identical to search_view except for --- CHANGED ---
function test_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('search/node');
}
$keys = search_get_keys();
// Only perform search if there is non-whitespace search term:
$results = '';
if (trim($keys)) {
// Log the search keys:
watchdog('search', '%keys (@type).', array('%keys' => $keys, '@type' => module_invoke($type, 'search', 'name')), WATCHDOG_NOTICE, l(t('results'), 'search/'. $type .'/'. $keys));
// Collect the search results:
// --- CHANGED ---
// $results = search_data($keys, $type);
// Instead of using search_data, use our own function
$results = test_search_data($keys, $type);
// --- END CHANGED ---
if ($results) {
$results = theme('box', t('Search results'), $results);
}
else {
$results = theme('box', t('Your search yielded no results'), search_help('search#noresults', drupal_help_arg()));
}
}
// Construct the search form.
$output = drupal_get_form('search_form', NULL, $keys, $type);
$output .= $results;
return $output;
}
return drupal_get_form('search_form', NULL, empty($keys) ? '' : $keys, $type);
}
// Note: identical to search_data() except for --- CHANGED ---
function test_search_data($keys = NULL, $type = 'node') {
if (isset($keys)) {
if (module_hook($type, 'search')) {
$results = module_invoke($type, 'search', 'search', $keys);
if (isset($results) && is_array($results) && count($results)) {
// --- CHANGED ---
// This dsm() is called immediately after hook_search() but before
// the results get themed. Put your code here.
dsm($results);
// --- END CHANGED ---
if (module_hook($type, 'search_page')) {
return module_invoke($type, 'search_page', $results);
}
else {
return theme('search_results', $results, $type);
}
}
}
}
}
这篇关于搜索钩子过滤结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!