You are here

function history_write in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/history/history.module \history_write()
  2. 9 core/modules/history/history.module \history_write()

Updates 'last viewed' timestamp of the specified entity for the current user.

Parameters

$nid: The node ID that has been read.

$account: (optional) The user account to update the history for. Defaults to the current user.

1 call to history_write()
HistoryController::readNode in core/modules/history/src/Controller/HistoryController.php
Marks a node as read by the current user right now.

File

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

Code

function history_write($nid, $account = NULL) {
  if (!isset($account)) {
    $account = \Drupal::currentUser();
  }
  if ($account
    ->isAuthenticated()) {
    $request_time = \Drupal::time()
      ->getRequestTime();
    \Drupal::database()
      ->merge('history')
      ->keys([
      'uid' => $account
        ->id(),
      'nid' => $nid,
    ])
      ->fields([
      'timestamp' => $request_time,
    ])
      ->execute();

    // Update static cache.
    $history =& drupal_static('history_read_multiple', []);
    $history[$nid] = $request_time;
  }
}