hook_search_execute
- Versions
- 7
hook_search_execute($keys = NULL)
Execute a search for a set of key words.
We call do_search() with the keys, the module name, and extra SQL fragments to use when searching. See hook_update_index() for more information.
Parameters
$keys The search keywords as entered by the user.
Return value
An array of search results. To use the default search result display, each item should have the following keys':
- 'link': Required. The URL of the found item.
- 'type': The type of item.
- 'title': Required. The name of the item.
- 'user': The author of the item.
- 'date': A timestamp when the item was last modified.
- 'extra': An array of optional extra information items.
- 'snippet': An excerpt or preview to show with the result (can be generated with search_excerpt()).
Related topics
Code
modules/search/search.api.php, line 143
<?php
function hook_search_execute($keys = NULL) {
// Build matching conditions
$query = db_search()->extend('PagerDefault');
$query->join('node', 'n', 'n.nid = i.sid');
$query
->condition('n.status', 1)
->addTag('node_access')
->searchExpression($keys, 'node');
// Insert special keywords.
$query->setOption('type', 'n.type');
$query->setOption('language', 'n.language');
if ($query->setOption('term', 'tn.nid')) {
$query->join('taxonomy_term_node', 'tn', 'n.vid = tn.vid');
}
// Only continue if the first pass query matches.
if (!$query->executeFirstPass()) {
return array();
}
// Add the ranking expressions.
_node_rankings($query);
// Add a count query.
$inner_query = clone $query;
$count_query = db_select($inner_query->fields('i', array('sid')));
$count_query->addExpression('COUNT(*)');
$query->setCountQuery($count_query);
$find = $query
->limit(10)
->execute();
// Load results.
$results = array();
foreach ($find as $item) {
// Build the node body.
$node = node_load($item->sid);
node_build_content($node, 'search_result');
$node->body = drupal_render($node->content);
// Fetch comments for snippet.
$node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node);
// Fetch terms for snippet.
$node->rendered .= ' ' . module_invoke('taxonomy', 'node_update_index', $node);
$extra = module_invoke_all('node_search_result', $node);
$results[] = array(
'link' => url('node/' . $item->sid, array('absolute' => TRUE)),
'type' => check_plain(node_type_get_name($node)),
'title' => $node->title[FIELD_LANGUAGE_NONE][0]['value'],
'user' => theme('username', array('account' => $node)),
'date' => $node->changed,
'node' => $node,
'extra' => $extra,
'score' => $item->calculated_score,
'snippet' => search_excerpt($keys, $node->body),
);
}
return $results;
}
?>Login or register to post comments 