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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.