annotate vendor/chi-teck/drupal-code-generator/templates/d7/hook/search_execute.twig @ 5:12f9dff5fda9 tip

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