Execute a search for a set of key words.
Use database API with the 'Drupal\Core\Database\Query\PagerSelectExtender' query extension to perform your search.
If your module uses hook_update_index() and search_index() to index its items, use table 'search_index' aliased to 'i' as the main table in your query, with the 'Drupal\search\SearchQuery' extension. You can join to your module's table using the 'i.sid' field, which will contain the $sid values you provided to search_index(). Add the main keywords to the query by using method searchExpression(). The functions search_expression_extract() and search_expression_insert() may also be helpful for adding custom search parameters to the search expression.
See node_search_execute() for an example of a module that uses the search index, and user_search_execute() for an example that doesn't use the search index.
$keys: The search keywords as entered by the user.
$conditions: An optional array of additional conditions, such as filters.
An array of search results. To use the default search result display, each item should have the following keys':
Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.
function hook_search_execute($keys = NULL, $conditions = NULL) {
// Build matching conditions
$query = db_select('search_index', 'i', array(
'target' => 'slave',
))
->extend('Drupal\\search\\SearchQuery')
->extend('Drupal\\Core\\Database\\Query\\PagerSelectExtender');
$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('langcode', 'n.langcode');
if ($query
->setOption('term', 'ti.tid')) {
$query
->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
}
// Only continue if the first pass query matches.
if (!$query
->executeFirstPass()) {
return array();
}
// Add the ranking expressions.
_node_rankings($query);
// Load results.
$find = $query
->limit(10)
->execute();
$results = array();
foreach ($find as $item) {
// Render the node.
$node = node_load($item->sid);
$build = node_view($node, 'search_result', $item->langcode);
unset($build['#theme']);
$node->rendered = drupal_render($build);
// Fetch comments for snippet.
$node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node, $item->langcode);
$extra = module_invoke_all('node_search_result', $node, $item->langcode);
$language = language_load($item->langcode);
$uri = $node
->uri();
$results[] = array(
'link' => url($uri['path'], array_merge($uri['options'], array(
'absolute' => TRUE,
'language' => $language,
))),
'type' => check_plain(node_get_type_label($node)),
'title' => $node
->label($item->langcode),
'user' => theme('username', array(
'account' => $node,
)),
'date' => $node
->get('changed', $item->langcode),
'node' => $node,
'extra' => $extra,
'score' => $item->calculated_score,
'snippet' => search_excerpt($keys, $node->rendered, $item->langcode),
'langcode' => $node->langcode,
);
}
return $results;
}