function hook_node_view

Act on a node that is being assembled before rendering.

The module may add elements to $node->content prior to rendering. This hook will be called after hook_view(). The structure of $node->content is a renderable array as expected by drupal_render().

When $view_mode is 'rss', modules can also add extra RSS elements and namespaces to $node->rss_elements and $node->rss_namespaces respectively for the RSS item generated for this node. For details on how this is used, see node_feed().

Parameters

\Drupal\Core\Entity\EntityInterface $node: The node that is being assembled for rendering.

\Drupal\entity\Plugin\Core\Entity\EntityDisplay $display: The entity_display object holding the display options configured for the node components.

string $view_mode: The $view_mode parameter from node_view().

string $langcode: The language code used for rendering.

See also

forum_node_view()

comment_node_view()

hook_entity_view()

Related topics

6 functions implement hook_node_view()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

book_node_view in drupal/core/modules/book/book.module
Implements hook_node_view().
comment_node_view in drupal/core/modules/comment/comment.module
Implements hook_node_view().
forum_node_view in drupal/core/modules/forum/forum.module
Implements hook_node_view().
node_test_node_view in drupal/core/modules/node/tests/modules/node_test/node_test.module
Implements hook_node_view().
statistics_node_view in drupal/core/modules/statistics/statistics.module
Implements hook_node_view().

... See full list

File

drupal/core/modules/node/node.api.php, line 821
Hooks provided by the Node module.

Code

function hook_node_view(\Drupal\Core\Entity\EntityInterface $node, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display, $view_mode, $langcode) {

  // Only do the extra work if the component is configured to be displayed.
  // This assumes a 'mymodule_addition' extra field has been defined for the
  // node type in hook_field_extra_fields().
  if ($display
    ->getComponent('mymodule_addition')) {
    $node->content['mymodule_addition'] = array(
      '#markup' => mymodule_addition($node),
      '#theme' => 'mymodule_my_additional_field',
    );
  }
}