Problem/Motivation

node_get_recent() is unused and untested in core. We should deprecate node_get_recent() in Drupal 10 for removal in Drupal 11, and point people to entity query or views as alternatives.

Since this is a straightforward deprecation with no bc layer we don't need any test coverage for it.

Tagging this as a novice task.

Proposed resolution

We need to add @deprecated to the function documentation, and trigger_error() to the function itself, details of the docs/message format can be found in https://www.drupal.org/about/core/policies/core-change-policies/drupal-d... (and/or search for @deprecated in core to see examples).

Remaining tasks

  • Patch
  • Change Record

User interface changes

N/a

API changes

CR #3356654: node_get_recent() is deprecated

Data model changes

N/a

Release notes snippet

Issue fork drupal-3356516

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

catch created an issue. See original summary.

o_tymoshchuk made their first commit to this issue’s fork.

o_timoshchuk’s picture

Status: Active » Needs review

node_get_recent() function noticed as deprecated in drupal:10.1.0 and is removed from drupal:11.0.0.

elber’s picture

I will revise it.

elber’s picture

I just rebased, about the deprecate method I also revised it .

Deprecation document is acording with drupal standards see: https://www.drupal.org/about/core/policies/core-change-policies/drupal-d...

Waiting someone to review the rebase and this issue as well.

longwave’s picture

Status: Needs review » Needs work
Issue tags: +Needs change record

This needs a change record with an example of a call to node_get_recent() and the replacement query. The deprecation notice should then link to the change record instead of this issue.

elber’s picture

Hi please can you give example about it?

longwave’s picture

elber’s picture

Hi please can you check it https://www.drupal.org/node/3356654

DanielVeza’s picture

The CR could use a bit of background about why this was deprecated. The first line in the issue statement is a good start.

The example also needs to be fleshed out, in particular the after section. The replacement isn't \Drupal::entityQuery('node'), it's using EntityQuery to find the most recently changed nodes that the user can access.

jwilson3’s picture

On Drupal 10.0, executing node_get_recent(1); anywhere in your codebase returns a fatal error:

The website encountered an unexpected error. Please try again later.

Drupal\Core\Entity\Query\QueryException: Entity queries must explicitly set whether the query should be access checked or not. See Drupal\Core\Entity\Query\QueryInterface::accessCheck(). in Drupal\Core\Entity\Query\Sql\Query->prepare() (line 141 of core/lib/Drupal/Core/Entity/Query/Sql/Query.php).
Drupal\Core\Entity\Query\Sql\Query->execute() (Line: 713)
node_get_recent(1)

This is due to CR #3201242: Access checking must be explicitly specified on content entity queries, which is now enforced in Drupal 10 by throwing an exception if ::accessCheck() is not explicitly included in any content entity queries. To be honest, this is no surprise because this issue explains the situation succinctly:

node_get_recent() is unused and untested in core.

I thought about opening a bug report, but then found this issue. As this function is slated to be deprecated in 10.1.0 (this issue) and removed in D11, the pragmatic approach is to just hit the WSOD error like I did; then, stumble across this issue (by searching for the error message I included above) and fixing your code to go ahead and replace the function call with your own entity query.

The fix in Drupal core would technically be a one-liner and might still make sense to open an issue.

function node_get_recent($number = 10) {
  $account = \Drupal::currentUser();
  $query = \Drupal::entityQuery('node');
+  $query->accessCheck(TRUE);
  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

But anyone looking for a quickfix replacement for their own code, would likely look something like this (in a bare-bones case). YMMV.

+use Drupal\node\NodeInterface;
+use Drupal\node\Entity\Node;

-$last_updated_node_array = node_get_recent(1);
+$nids = \Drupal::entityQuery('node')
+  ->accessCheck(FALSE)
+  ->condition('status', NodeInterface::PUBLISHED)
+  ->sort('changed', 'DESC')
+  ->range(0, 1)
+  ->addTag('node_access')
+  ->execute();
+ $last_updated_node_array = !empty($nids) ? Node::loadMultiple($nids) : [];

What do you think? should I open a separate bug report anyway?

jwilson3’s picture

Is the "After" example in this issue's CR correct?

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

It seems like it is missing several things, like what I outlined in the second diff in comment #12 above.

longwave’s picture

@jwilson3 as noted in #11 the change record is incomplete and needs work.

I am sort of tempted to leave node_get_recent() broken, given that it is untested and we are removing it anyway - otherwise we need to add a test to prove that the fix works. But if you want to open a separate bug report, we can look at fixing the access check so it works until it is removed in Drupal 11.

jwilson3’s picture

But if you want to open a separate bug report, we can look at fixing the access check so it works until it is removed in Drupal 11.

All it takes is a core committer to block getting the fix in because it doesn't have tests and we're basically back at this issue.

jwilson3’s picture

👆 That being said, I've opened an issue. 😀

#3357368: EntityQuery Access Check in node_get_recent()

Let's see what happens.

catch’s picture

That might already have been fixed in #3357368: EntityQuery Access Check in node_get_recent() (which is what prompted me to open this issue, since I had no idea node_get_recent() still existed until I saw it again there).

DanielVeza’s picture

Status: Needs work » Needs review
Issue tags: -Needs change record

Updated the CR. I added the example from #12 since I think thats a pretty clean example. However it's not exactly a 1-1 replacement since node_get_recent does some extra checking around if a user can see unpublished nodes etc. Do we want to add that in?

The CR is also currently published.. That feels incorrect to me since this hasn't gone in yet. I didn't want to unpublish it just in case it is meant to be published, so thought I'd flag it.

sharayurajput’s picture

Assigned: Unassigned » sharayurajput

I will review this issue

jwilson3’s picture

Issue summary: View changes

Added CR to issue summary and filled out what I could of the standard issue summary template.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

CR looks good.
Deprecation looks simple enough.
Assuming since it's a task that no test deprecation will be needed.
Confirmed node_get_recent is not used in the repo.

Looks good.

jwilson3’s picture

Issue summary: View changes

I've taken a stab at adding Release Notes Snippet to the IS. Please have a look.

jwilson3’s picture

Re #18:

it's not exactly a 1-1 replacement since node_get_recent does some extra checking around if a user can see unpublished nodes etc. Do we want to add that in?

I think it makes sense to have a simple option and a more advanced option that copies/pastes the functionality more or less verbatim from Drupal 10.1 with all the extra permissions and unpublished and own node logic.

I've added two more examples to the CR:

  • One for a more complete version that is copy/paste of the original function code.
  • Another one for a simplified version that leverages ::accessCheck() to do the heavy lifting.

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

Please review

jwilson3’s picture

Issue summary states:

point people to entity query or views as alternatives.

We have not explicitly pointed people to entity query (link?) or mentioned views as alternatives on the CR.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

  • catch committed b4554185 on 11.x
    Issue #3356516 by o_tymoshchuk, elber, jwilson3, catch, longwave,...
catch’s picture

Issue summary: View changes
Status: Reviewed & tested by the community » Fixed

The CR covers this fine now - mentions views at the top and shows explicit entity query alternatives.

We don't need a release note for this one, just the CR is fine.

Committed/pushed to 11.x, thanks! Note per the new core branching scheme this means it'll be in 10.2.x. I updated the deprecation version on commit from 10.1 to 10.2

andypost’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.