function HistoryHooks::nodeViewAlter

Implements hook_ENTITY_TYPE_view_alter() for node entities.

File

core/modules/history/src/Hook/HistoryHooks.php, line 46

Class

HistoryHooks
Hook implementations for history.

Namespace

Drupal\history\Hook

Code

public function nodeViewAlter(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;
        }
    }
}

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