Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / chi-teck / drupal-code-generator / templates / d7 / hook / search_execute.twig
1 /**
2  * Implements hook_search_execute().
3  */
4 function {{ machine_name }}_search_execute($keys = NULL, $conditions = NULL) {
5   // Build matching conditions
6   $query = db_select('search_index', 'i', array('target' => 'slave'))->extend('SearchQuery')->extend('PagerDefault');
7   $query->join('node', 'n', 'n.nid = i.sid');
8   $query
9     ->condition('n.status', 1)
10     ->addTag('node_access')
11     ->searchExpression($keys, 'node');
12
13   // Insert special keywords.
14   $query->setOption('type', 'n.type');
15   $query->setOption('language', 'n.language');
16   if ($query->setOption('term', 'ti.tid')) {
17     $query->join('taxonomy_index', 'ti', 'n.nid = ti.nid');
18   }
19   // Only continue if the first pass query matches.
20   if (!$query->executeFirstPass()) {
21     return array();
22   }
23
24   // Add the ranking expressions.
25   _node_rankings($query);
26
27   // Load results.
28   $find = $query
29     ->limit(10)
30     ->execute();
31   $results = array();
32   foreach ($find as $item) {
33     // Build the node body.
34     $node = node_load($item->sid);
35     node_build_content($node, 'search_result');
36     $node->body = drupal_render($node->content);
37
38     // Fetch comments for snippet.
39     $node->rendered .= ' ' . module_invoke('comment', 'node_update_index', $node);
40     // Fetch terms for snippet.
41     $node->rendered .= ' ' . module_invoke('taxonomy', 'node_update_index', $node);
42
43     $extra = module_invoke_all('node_search_result', $node);
44
45     $results[] = array(
46       'link' => url('node/' . $item->sid, array('absolute' => TRUE)),
47       'type' => check_plain(node_type_get_name($node)),
48       'title' => $node->title,
49       'user' => theme('username', array('account' => $node)),
50       'date' => $node->changed,
51       'node' => $node,
52       'extra' => $extra,
53       'score' => $item->calculated_score,
54       'snippet' => search_excerpt($keys, $node->body),
55     );
56   }
57   return $results;
58 }