EntityQuery doesn't support OR condition for different langcodes

Created on 31 January 2018, over 7 years ago
Updated 11 February 2023, over 2 years ago

Problem

When 2 conditions on the same multiple field with different langcodes, it joins the field table only once.
Example (can be used in any controller)

$langcode = $this->languageManager()->getCurrentLanguage()->getId();
$defaultLangcode = $this->languageManager()->getDefaultLanguage()->getId();
$query = $this->entityTypeManager->getStorage('station')->getQuery();
$query->condition('status', 1);
$orCondition = $query->orConditionGroup();
$orCondition->condition('name', $q, 'CONTAINS', $langcode)
  ->condition('name', $q, 'CONTAINS', $defaultLangcode);
$query->condition($orCondition);
$stations = $query->execute();

Station is a custom entity type, name is a base field of type string with cardinality set to FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED
The resulting query looks like

SELECT base_table.id AS id, base_table.id AS base_table_id
FROM 
{station} base_table
INNER JOIN {station_field_data} station_field_data ON station_field_data.id = base_table.id
LEFT JOIN {station__name} station__name ON station__name.entity_id = base_table.id AND station__name.langcode = :langcode0
WHERE (station_field_data.status = :db_condition_placeholder_1) AND ((station__name.name_value LIKE :db_condition_placeholder_2 ESCAPE '\\') or (station__name.name_value LIKE :db_condition_placeholder_3 ESCAPE '\\'))
GROUP BY base_table.id

But expected

SELECT base_table.id AS id, base_table.id AS base_table_id
FROM 
{station} base_table
INNER JOIN {station_field_data} station_field_data ON station_field_data.id = base_table.id
LEFT JOIN {station__name} station__name ON station__name.entity_id = base_table.id AND station__name.langcode = :langcode0
LEFT JOIN {station__name} station__name_1 ON station__name_1.entity_id = base_table.id AND station__name_1.langcode = :langcode1
WHERE (station_field_data.status = :db_condition_placeholder_1) AND ((station__name.name_value LIKE :db_condition_placeholder_2 ESCAPE '\\') or (station__name_1.name_value LIKE :db_condition_placeholder_3 ESCAPE '\\'))
GROUP BY base_table.id

So, when different langcodes are used for query, it doesn't join field table again using another value for condition with langcode.
As I see joining of field tables happens here \Drupal\Core\Entity\Query\Sql\Tables::ensureFieldTable. And here it doesn't take in account that langcode may be different.

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
EntityΒ  β†’

Last updated about 22 hours ago

Created by

πŸ‡·πŸ‡ΊRussia sergei_brill

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024