function hook_view

Display a node.

This hook is invoked only on the module that defines the node's content type (use hook_node_view() to act on all node views).

This hook is invoked during node viewing after the node is fully loaded, so that the node type module can define a custom method for display, or add to the default display.

Parameters

\Drupal\Core\Entity\EntityInterface $node: The node to be displayed, as returned by node_load().

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

$view_mode: View mode, e.g. 'full', 'teaser', ...

Return value

The passed $node parameter should be modified as necessary and returned so it can be properly presented. Nodes are prepared for display by assembling a structured array, formatted as in the Form API, in $node->content. As with Form API arrays, the #weight property can be used to control the relative positions of added elements. After this hook is invoked, node_view() calls field_attach_view() to add field views to $node->content, and then invokes hook_node_view() and hook_node_view_alter(), so if you want to affect the final view of the node, you might consider implementing one of these hooks instead.

Related topics

31 functions implement hook_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().
comment_view in drupal/core/modules/comment/comment.module
Generates an array for rendering a comment.
custom_block_test_custom_block_view in drupal/core/modules/block/custom_block/tests/modules/custom_block_test/custom_block_test.module
Implements hook_custom_block_view().
entity_view in drupal/core/includes/entity.inc
Returns the render array for an entity.

... See full list

File

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

Code

function hook_view(\Drupal\Core\Entity\EntityInterface $node, \Drupal\entity\Plugin\Core\Entity\EntityDisplay $display, $view_mode) {
  if ($view_mode == 'full' && node_is_page($node)) {
    $breadcrumb = array();
    $breadcrumb[] = l(t('Home'), NULL);
    $breadcrumb[] = l(t('Example'), 'example');
    $breadcrumb[] = l($node->field1, 'example/' . $node->field1);
    drupal_set_breadcrumb($breadcrumb);
  }

  // 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',
    );
  }
  return $node;
}