domain_entity_query_alter() is interpreted by core as an implementation of hook_entity_query_alter()

Created on 20 March 2025, about 1 month ago

Problem/Motivation

Drupal 10.3.0 introduced a new hook_entity_query_alter() API (see [#3363939] )

Due to the following coincidences:

  • This module's name equals the name of some other module (domain) plus the suffix _entity,
  • This module implements hook_query_alter(),
  • Drupal *.module files are loaded globally on all requests for all enabled modules.

The function domain_entity_query_alter() is considered by core to be an implementation of hook_entity_query_alter() for the domain module. (This is being considered an implementation of hook_query_alter() for the domain_entity module, as originally intended).

The only reason this does not throw an immediate Fatal Error (at least in my case) is because, of the following coincidences:

  • hook_query_alter() and hook_entity_query_alter() are identical in terms of their parameter types, even though they pass quite different kinds of object.
  • domain_entity_query_alter() starts with quite a general check for method_exists($query, 'getTables') (added 8 years ago) which fails in the hook_entity_query_alter() case, skipping the method.

This is something I discovered while debugging something unrelated. In my situation it doesn't break anything, but I thought I'd bring it to the attention of the module maintainers, as surely it is not the intention for this to be so, it strikes me as quite fragile?

If a getTables method were added to Entity queries for example, it would probably break.

Proposed resolution

Perhaps it would be a good idea to add this kind of check to the top of the function?

if (!$query instanceof '\Drupal\Core\Entity\Query\Sql\Query') {
  return;
}
🐛 Bug report
Status

Active

Component

Code

Created by

🇬🇧United Kingdom jamsilver West Midlands, UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024