By elber on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
10.2.x
Introduced in version:
10.2.0
Issue links:
Description:
node_get_recent() is unused in core and is now deprecated. Replacement is to use either views or EntityQuery to get the most recently changed nodes.
Before:
$latest_updated_nodes_array = node_get_recent(10);
After:
If you're only concerned with published nodes:
$nids = \Drupal::entityQuery('node')
->accessCheck(TRUE)
->condition('status', NodeInterface::PUBLISHED)
->sort('changed', 'DESC')
->range(0, 10)
->addTag('node_access')
->execute();
$latest_updated_nodes_array = !empty($nids) ? Node::loadMultiple($nids) : [];
If you're interested in including recent unpublished and draft nodes as well as published nodes, you'll want to replicate the code more like it was in its original form:
$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, 10)
->addTag('node_access')
->execute();
$latest_updated_nodes_array = !empty($nids) ? Node::loadMultiple($nids) : [];
You may also wish to remove the status condition entirely and instead leverage just the ::accessCheck():
$nids = \Drupal::entityQuery('node')
->accessCheck(TRUE)
->sort('changed', 'DESC')
->range(0, 10)
->addTag('node_access')
->execute();
$latest_updated_nodes_array = !empty($nids) ? Node::loadMultiple($nids) : [];
Impacts:
Module developers