Same name and namespace in other branches
  1. 7.x modules/node/node.module \node_get_recent()
  2. 8.9.x core/modules/node/node.module \node_get_recent()
  3. 9 core/modules/node/node.module \node_get_recent()

Finds the most recently changed nodes that are available to the current user.

Parameters

$number: (optional) The maximum number of nodes to find. Defaults to 10.

Return value

\Drupal\node\NodeInterface[] An array of node entities or an empty array if there are no recent nodes visible to the current user.

Deprecated

in drupal:10.1.0 and is removed from drupal:11.0.0. Use \Drupal::entityQuery() instead.

See also

https://www.drupal.org/node/3356516

File

core/modules/node/node.module, line 726
The core module that allows content to be submitted to the site.

Code

function node_get_recent($number = 10) {
  @trigger_error(__METHOD__ . ' is deprecated in drupal:10.2.0 and is removed from drupal:11.0.0. Use \\Drupal::entityQuery() instead. See https://www.drupal.org/node/3356516', E_USER_DEPRECATED);
  $account = \Drupal::currentUser();
  $query = \Drupal::entityQuery('node');
  if (!$account
    ->hasPermission('bypass node access')) {

    // If the user is able to view their own unpublished nodes, allow them
    // to see these in addition to published nodes. Check that they actually
    // have some unpublished nodes to view before adding the condition.
    $access_query = \Drupal::entityQuery('node')
      ->accessCheck(TRUE)
      ->condition('uid', $account
      ->id())
      ->condition('status', NodeInterface::NOT_PUBLISHED);
    if ($account
      ->hasPermission('view own unpublished content') && ($own_unpublished = $access_query
      ->execute())) {
      $query
        ->orConditionGroup()
        ->condition('status', NodeInterface::PUBLISHED)
        ->condition('nid', $own_unpublished, 'IN');
    }
    else {

      // If not, restrict the query to published nodes.
      $query
        ->condition('status', NodeInterface::PUBLISHED);
    }
  }
  $nids = $query
    ->accessCheck(TRUE)
    ->sort('changed', 'DESC')
    ->range(0, $number)
    ->addTag('node_access')
    ->execute();
  $nodes = Node::loadMultiple($nids);
  return $nodes ? $nodes : [];
}