2

I have a simple bit of test code to load nodes by UUID:

$uuids = ['5e331255-adf6-423e-a355-dc02c621ffcd'];
$nids = \Drupal::entityTypeManager()->getStorage('node')
  ->getQuery()
  ->condition('uuid', $uuids, 'IN')
  ->execute();

When I execute this code in hook_page_attachments_alter(), or anywhere else in a rendered page build, $nids is populated as expected with the correct node ID.

When I execute exactly the same code, verbatim, in a drush command, $nids is empty.

Loading the node by ID works fine in both contexts:

$node = \Drupal::entityTypeManager()->getStorage('node')->load(12345);

This happens with the same code/database in multiple environments (Lando, ACSF, standard LAMP), and I've confirmed that drush is connecting to the correct database everywhere.

Are there any obvious properties of the node or storage system, or configuration of drush, which could be responsible for this?

Content moderation, revisions, and translations are all enabled for the node's content type, in case it matters.

2
  • Does it help to disable the query access check? ->accessCheck(FALSE)
    – 4uk4
    Jul 27, 2022 at 19:36
  • If I had a penny for every time I've forgotten that...thank you hugely @4uk4, that is indeed the problem and now I know which module I need to patch and file a bug report to. If you put an answer in I'll accept it
    – Clive
    Jul 27, 2022 at 19:40

1 Answer 1

3

Disable the query access check

  ->accessCheck(FALSE)

or use this service

$node = \Drupal::service('entity.repository')->loadEntityByUuid('node', $uuid);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.