function NodeTestHooks::nodeView

Implements hook_ENTITY_TYPE_view() for node entities.

Attributes

#[Hook('node_view')]

File

core/modules/node/tests/modules/node_test/src/Hook/NodeTestHooks.php, line 22

Class

NodeTestHooks
Hook implementations for node_test.

Namespace

Drupal\node_test\Hook

Code

public function nodeView(array &$build, NodeInterface $node, EntityViewDisplayInterface $display, $view_mode) : void {
  if ($node->isNew()) {
    return;
  }
  if ($view_mode == 'rss') {
    // Add RSS elements and namespaces when building the RSS feed.
    $node->rss_elements[] = [
      'key' => 'testElement',
      'value' => 'Value of testElement RSS element for node ' . $node->id() . '.',
    ];
    // Add content that should be displayed only in the RSS feed.
    $build['extra_feed_content'] = [
      '#markup' => '<p>Extra data that should appear only in the RSS feed for node ' . $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>Extra data that should appear everywhere except the RSS feed for node ' . $node->id() . '.</p>',
    ];
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.