W3cubDocs

/Drupal 8

function history_node_view_alter

history_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display)

Implements hook_ENTITY_TYPE_view_alter() for node entities.

File

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

Code

function history_node_view_alter(array &$build, EntityInterface $node, EntityViewDisplayInterface $display) {
  // 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;
    }
  }

}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!modules!history!history.module/function/history_node_view_alter/8.1.x