/Drupal 8

function history_read_multiple


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


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.


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


function history_read_multiple($nids) {
  $history = &drupal_static(__FUNCTION__, array());

  $return = array();

  $nodes_to_read = array();
  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 = db_query('SELECT nid, timestamp FROM {history} WHERE uid = :uid AND nid IN ( :nids[] )', array(
    ':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;

© 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.