Problem/Motivation

Drupal 10.3.0 introduced a new hook_entity_query_alter() API (see change record here).

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 in addition to 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() is covariant in its parameter type with respect to hook_entity_query_alter(). (Entity Query instances implement the same AlterableInterface as DB queries do).
  • 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 function.

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?

For example: If a getTables method were added to Entity queries, or if the Domain module added its own hook_entity_query_alter() implementation, things would break horribly.

Proposed resolution

I suppose renaming the module is out of the question, but perhaps it would be a good idea to add this kind of check to the top of the function?

// Due to various coincidences this function is incorrectly interpreted, as of Drupal 10.3.0,
// as an implementation of hook_entity_query_alter(). No thanks.
if ($query instanceof \Drupal\Core\Entity\Query\QueryInterface) {
  return;
}
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:

Comments

jamsilver created an issue. See original summary.

jamsilver’s picture

Title: domain_entity_query_alter() is interpreted by core as an implementation of hook_entity_query() » domain_entity_query_alter() is interpreted by core as an implementation of hook_entity_query_alter()
jamsilver’s picture

Issue summary: View changes
jamsilver’s picture

Issue summary: View changes
jamsilver’s picture

Issue summary: View changes
jamsilver’s picture

Issue summary: View changes
jamsilver’s picture

Issue summary: View changes
jamsilver’s picture

Issue summary: View changes
hchonov’s picture

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

vitaliyb98 changed the visibility of the branch 3514467-domainentityqueryalter-is-interpreted to hidden.

vitaliyb98’s picture

Version: 8.x-1.0-rc1 » 2.0.x-dev

vitaliyb98’s picture

I moved domain_entity_query_alter into the OOP hook in DomainEntityHook. But this will work only from Drupal 11.1+.

But if we should have backward compatibility, we should add something like this inside .module (for Drupal 10.1+):

/**
 * Implements hook_query_alter().
 *
 * Alter the enabled entities select query, add domain access conditions.
 */
#[LegacyHook]
function domain_entity_query_alter(AlterableInterface $query) {
  if ($query instanceof QueryInterface) {
    return;
  }

  \Drupal::service('Drupal\domain_entity\Hook\DomainEntityHook')->queryAlter($query);
}

vitaliyb98’s picture

Status: Active » Needs review
vitaliyb98’s picture

I decided to add support for Drupal 10.1+ by adding a legacy hook.

Also, I added phpstan.neon file to skip notice about #[LegacyHook] and #[Hook] (as example used phpstan.neon from commerce, which solves a similar problem https://git.drupalcode.org/project/commerce/-/blob/3.x/phpstan.neon?ref_...)

Tested hook with Drupal 10.1 and Drupal 11.3.5
#[LegacyHook] correctly called in Drupal 10.1 and #[Hook] is skipped.
And
#[Hook] correctly called in Drupal 11.3.5 and #[LegacyHook] is skipped

  • bohart committed 81c9c156 on 2.0.x authored by vitaliyb98
    fix: #3514467 Moved hook_query_alter() into DomainEntityHook.
    
    By:...
bohart’s picture

Status: Needs review » Fixed

@vitaliyb98, thanks!
Committed to 2.0.x dev branch, and this one is marked as fixed now.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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