1

I am trying to get some entities using the "entity.query" service.

What am I doing:

in my module (file "mymodule.module") I execute:

function mymodule_preprocess_page(&$variables) {
  $query = \Drupal::entityQuery('node');
  $query->condition('type', 'article');
  $nids = $query->execute();
}

What I get:

  • when I browse a page under the name of an authorized user - the request returns the results enter image description here

  • when I browse the site as a guest, the query does not return any results.

enter image description here

Why am I not getting any results when I browse the site as a guest? Any ideas?

2
  • 3
    Do anonymous users have permissions to view the article content type/any content? Or are they unpublished perhaps? The entityquery will run factor in the users access permissions
    – Leigh
    Commented Jan 16, 2020 at 13:57
  • Articles were published, but none of them were tied to a domain on which I tried to get them. Thank you very much for your answer, he pushed me on the right path :)
    – Vlad
    Commented Jan 16, 2020 at 14:05

1 Answer 1

3

Entity query respects access by default. Add the ->accessCheck(FALSE) method to prevent access checking. Be careful, removing access checks may expose restricted data. It may be better to configure view permissions or whatever access system you're using to allow anonymous users to access content instead of removing the check.

1
  • Thank you very much for the addition.
    – Vlad
    Commented Jan 16, 2020 at 17:30

Your Answer

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

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