You are here

function node_test_node_view in Drupal 8

Same name and namespace in other branches
  1. 7 modules/node/tests/node_test.module \node_test_node_view()
  2. 9 core/modules/node/tests/modules/node_test/node_test.module \node_test_node_view()
  3. 10 core/modules/node/tests/modules/node_test/node_test.module \node_test_node_view()

Implements hook_ENTITY_TYPE_view() for node entities.

File

core/modules/node/tests/modules/node_test/node_test.module, line 20
A dummy module for testing node related hooks.

Code

function node_test_node_view(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) {
  if ($view_mode == 'rss') {

    // Add RSS elements and namespaces when building the RSS feed.
    $node->rss_elements[] = [
      'key' => 'testElement',
      'value' => t('Value of testElement RSS element for node @nid.', [
        '@nid' => $node
          ->id(),
      ]),
    ];

    // Add content that should be displayed only in the RSS feed.
    $build['extra_feed_content'] = [
      '#markup' => '<p>' . t('Extra data that should appear only in the RSS feed for node @nid.', [
        '@nid' => $node
          ->id(),
      ]) . '</p>',
      '#weight' => 10,
    ];
  }
  if ($view_mode != 'rss') {

    // Add content that should NOT be displayed in the RSS feed.
    $build['extra_non_feed_content'] = [
      '#markup' => '<p>' . t('Extra data that should appear everywhere except the RSS feed for node @nid.', [
        '@nid' => $node
          ->id(),
      ]) . '</p>',
    ];
  }
}