How to Use EntityQuery accessCheck(TRUE) vs accessCheck(FALSE) Properly in Drupal 10 and 11

When upgrading custom modules to Drupal 10 or 11, one of the first exceptions developers hit is Entity queries must explicitly set whether the query should be access checked or not. The fastest—and riskiest—reaction is to paste ->accessCheck(FALSE) onto every \Drupal::entityQuery() call just to make the error go away. Doing that blindly on a user-facing controller or API endpoint can expose unpublished drafts or restricted content to anonymous visitors.

Editorial illustration comparing Drupal EntityQuery accessCheck(TRUE) vs accessCheck(FALSE)

Editorial illustration: choose accessCheck(TRUE) for user-facing queries and accessCheck(FALSE) only for deliberate system checks.

Summary

  • Per Drupal’s official change record on explicit access checking for content entity queries, Drupal 10 and newer require every content entity query to call ->accessCheck(TRUE) or ->accessCheck(FALSE) explicitly.
  • Use accessCheck(TRUE) whenever results will be displayed, counted, or returned to a user (controllers, custom blocks, search listings, JSON endpoints) so Drupal enforces node and entity view permissions.
  • Use accessCheck(FALSE) only for deliberate system operations—such as cron jobs, queue workers, Drush commands, or uniqueness/integrity validation—where bypassing user permissions is required and results are not leaked.
  • Never load entities from an accessCheck(FALSE) query and render them directly without checking $entity->access('view') first.

Why Drupal enforces accessCheck()

In Drupal 8 and 9, EntityQuery defaulted to checking entity access automatically unless you opted out. That implicit default caused two opposite problems:

  1. Developers writing background cron tasks or unique-field validators forgot access checking was enabled, so validation missed unpublished nodes belonging to other users.
  2. Other developers assumed accessCheck() was optional and did not think carefully about whose permissions applied to a query.

As documented in Drupal.org change record #3201242, Drupal 10 made accessCheck() mandatory on content entity queries so every query states its security intent in code. (Note that configuration entities such as views or roles do not go through content entity access grants the same way, though checking the exact query type in your module is good practice.)

When to use accessCheck(TRUE)

Choose accessCheck(TRUE) whenever a query runs on behalf of a human visitor or authenticated API caller:

$nids = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->getQuery()
  ->accessCheck(TRUE)
  ->condition('type', 'article')
  ->condition('status', 1)
  ->sort('created', 'DESC')
  ->range(0, 10)
  ->execute();

Notice two subtle points here:

  • Even though ->condition('status', 1) already filters published nodes, custom node-access modules (such as Group, Domain Access, or Content Access) hook into accessCheck(TRUE) to restrict which published nodes the current user is allowed to see.
  • If you inject @entity_type.manager into a service or controller instead of calling \Drupal::entityTypeManager(), unit testing and dependency tracking stay much cleaner.

When accessCheck(FALSE) is actually right

Use accessCheck(FALSE) only when skipping user permissions is the intended behavior—and never pass those raw records to an untrusted response:

  1. Uniqueness and data-integrity checks. As discussed in Drupal core issue #3202125, if a validator checks whether a SKU or slug already exists across all nodes, it must check all nodes—including unpublished ones the current editor cannot view.
  2. Cron jobs, Queue API workers, and Drush commands. Automated background tasks often run as the anonymous user (uid 0) in CLI or cron contexts; accessCheck(TRUE) would skip unpublished items that a cleanup or indexing worker needs to process.
  3. Private file or system cleanup routines. When auditing orphaned files or expired records, system jobs need visibility across the whole dataset. For related permission checks on uploaded files, see our guide on Drupal private file access checks.

Step-by-step: Audit custom modules for accessCheck() issues

  1. Search your custom modules for every entityQuery and getQuery() call. Check web/modules/custom/ for both \Drupal::entityQuery( and ->getQuery().
  2. Classify each call by caller context. Ask: “Does a user see or influence the output of this query?” If yes—or if you are unsure—default to accessCheck(TRUE).
  3. Verify node-access and role behavior on staging. Test the page or endpoint as an anonymous user, an authenticated editor, and an administrator. Remember to check cacheability metadata as well; per-user access results often need user.permissions or entity tags, which we covered in debugging Drupal cache tags without clearing all caches.
  4. Keep environment-specific dev modules separated. While testing queries locally with Devel, make sure dev tools stay out of production using Drupal Configuration Split.

Common mistakes

  • Globally replacing missing accessCheck() calls with accessCheck(FALSE) during a Drupal 10/11 upgrade. That silences the QueryException, but it quietly disables node access modules and unpublished-content protections on custom listings.
  • Forgetting that accessCheck(TRUE) checks query-level access, not field-level access. If an entity contains restricted fields, still render via Drupal’s entity view builder or check $entity->get($field)->access('view').
  • Caching an accessCheck(TRUE) query result without proper cache contexts. If one role sees five items and another sees ten, your render array needs matching cache contexts so role A’s cached list is not served to role B.

Why this matters

Explicit accessCheck() calls turn security boundaries from an invisible assumption into a readable line of code. Spending five minutes reviewing each query during a Drupal 10 or 11 upgrade prevents subtle information disclosure bugs that automated linters alone cannot catch.

My Take

Treat accessCheck(FALSE) like sudo in a shell script: it is necessary for cron workers, Drush scripts, and uniqueness checks, and a red flag almost everywhere else. Whenever you write accessCheck(FALSE), add a one-line comment explaining why bypassing entity access is safe in that exact method.

Conclusion

Audit every getQuery() and \Drupal::entityQuery() call in your custom Drupal modules, set accessCheck(TRUE) on all user-facing queries, restrict accessCheck(FALSE) to verified system or integrity checks, and test with non-admin roles on staging.

Sources