function history_read_multiple

Same name in other branches
  1. 8.9.x core/modules/history/history.module \history_read_multiple()
  2. 10 core/modules/history/history.module \history_read_multiple()
  3. 11.x core/modules/history/history.module \history_read_multiple()

Retrieves the last viewed timestamp for each of the passed node IDs.

Parameters

array $nids: An array of node IDs.

Return value

array Array of timestamps keyed by node ID. If a node has been previously viewed by the user, the timestamp in seconds of when the last view occurred; otherwise, zero.

2 calls to history_read_multiple()
HistoryController::getNodeReadTimestamps in core/modules/history/src/Controller/HistoryController.php
Returns a set of nodes' last read timestamps.
history_read in core/modules/history/history.module
Retrieves the timestamp for the current user's last view of a specified node.
1 string reference to 'history_read_multiple'
history_write in core/modules/history/history.module
Updates 'last viewed' timestamp of the specified entity for the current user.

File

core/modules/history/history.module, line 66

Code

function history_read_multiple($nids) {
    $history =& drupal_static(__FUNCTION__, []);
    $return = [];
    $nodes_to_read = [];
    foreach ($nids as $nid) {
        if (isset($history[$nid])) {
            $return[$nid] = $history[$nid];
        }
        else {
            // Initialize value if current user has not viewed the node.
            $nodes_to_read[$nid] = 0;
        }
    }
    if (empty($nodes_to_read)) {
        return $return;
    }
    $result = \Drupal::database()->query('SELECT [nid], [timestamp] FROM {history} WHERE [uid] = :uid AND [nid] IN ( :nids[] )', [
        ':uid' => \Drupal::currentUser()->id(),
        ':nids[]' => array_keys($nodes_to_read),
    ]);
    foreach ($result as $row) {
        $nodes_to_read[$row->nid] = (int) $row->timestamp;
    }
    $history += $nodes_to_read;
    return $return + $nodes_to_read;
}

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