node_get_recent

node_get_recent($number = 10)

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.

File

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

Code

function node_get_recent($number = 10) {
  $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')
      ->condition('uid', $account->id())
      ->condition('status', NODE_NOT_PUBLISHED);
    if ($account->hasPermission('view own unpublished content') && ($own_unpublished = $access_query->execute())) {
      $query->orConditionGroup()
        ->condition('status', NODE_PUBLISHED)
        ->condition('nid', $own_unpublished, 'IN');
    }
    else {
      // If not, restrict the query to published nodes.
      $query->condition('status', NODE_PUBLISHED);
    }
  }
  $nids = $query
  ->sort('changed', 'DESC')
    ->range(0, $number)
    ->addTag('node_access')
    ->execute();

  $nodes = Node::loadMultiple($nids);

  return $nodes ? $nodes : array();
}
doc_Drupal
2016-10-29 09:31:54
Comments
Leave a Comment

Please login to continue.