W3cubDocs

/Drupal 8

function hook_node_search_result

hook_node_search_result(\Drupal\node\NodeInterface $node)

Act on a node being displayed as a search result.

This hook is invoked from the node search plugin during search execution, after loading and rendering the node.

Parameters

\Drupal\node\NodeInterface $node: The node being displayed in a search result.

Return value

array Extra information to be displayed with search result. This information should be presented as an associative array. It will be concatenated with the post information (last updated, author) in the default search result theming.

See also

template_preprocess_search_result()

search-result.html.twig

Related topics

Entity CRUD, editing, and view hooks
Hooks used in various entity operations.
Hooks
Define functions that alter the behavior of Drupal core.

File

core/modules/node/node.api.php, line 376
Hooks specific to the Node module.

Code

function hook_node_search_result(\Drupal\node\NodeInterface $node) {
  $rating = db_query('SELECT SUM(points) FROM {my_rating} WHERE nid = :nid', array('nid' => $node->id()))->fetchField();
  return array('rating' => \Drupal::translation()->formatPlural($rating, '1 point', '@count points'));
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!node!node.api.php/function/hook_node_search_result/8.1.x