Same name and namespace in other branches
  1. 8.9.x core/modules/history/history.module \history_node_view_alter()
  2. 9 core/modules/history/history.module \history_node_view_alter()

Implements hook_ENTITY_TYPE_view_alter() for node entities.

File

core/modules/history/history.module, line 139
Records which users have read which content.

Code

function history_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display) {
  if ($node
    ->isNew() || isset($node->in_preview)) {
    return;
  }

  // Update the history table, stating that this user viewed this node.
  if ($display
    ->getOriginalMode() === 'full') {
    $build['#cache']['contexts'][] = 'user.roles:authenticated';
    if (\Drupal::currentUser()
      ->isAuthenticated()) {

      // When the window's "load" event is triggered, mark the node as read.
      // This still allows for Drupal behaviors (which are triggered on the
      // "DOMContentReady" event) to add "new" and "updated" indicators.
      $build['#attached']['library'][] = 'history/mark-as-read';
      $build['#attached']['drupalSettings']['history']['nodesToMarkAsRead'][$node
        ->id()] = TRUE;
    }
  }
}