Problem/Motivation

When you have translatable entity reference field and want to check that specific referenced entity is not used in other translations so you can delete it.

Example data state:

entity_id revision_id langcode delta field_tags_target_id
6590308 20 en 0 10000000
6590308 21 en 0 10000000
6590308 21 ja 0 10000001
6590308 22 en 0 10000000
6590308 22 en 1 10000001
6590308 22 ja 0 10000002

Configured languages: Japanese, German, English.

Let's say I wan't remove reference 10000001 from new EN translation, but I wan't be sure that it's not used in other translations so I can delete it.

I will run a query:

$entity_id = 6590308;
$entity_type = 'node';
$entity = Node::load($entity_id);

// IDs of referenced entities.
$ids = [10000001];
$field_name = 'field_tags';


$storage = $this->entityTypeManager->getStorage($entity_type);
$query = $storage->getQuery();
$query
  ->allRevisions()
  ->condition($entity->getEntityType()->getKey('id'), $entity->id(), '=');
$can_be_deleted = [];
$all_languages = \Drupal::languageManager()->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
foreach ($ids as $id) {
  $orConditionGroup = $query->orConditionGroup();
  foreach ($all_languages as $language) {
    if ($entity->language()->getId() != $language->getId()) {
      $orConditionGroup->condition($field_name, $id, '=', $language->getId());
    }
  }
  $query->condition($orConditionGroup);
  $result = $query->execute();
  if (empty($result)) {
    $can_be_deleted[] = $id;
  }
}
$ids = $can_be_deleted;

Produced SQL query is going to join to the field table based on langcode that were first passed to condition group.

So if the first language in the example above would be Japanese it will succeed, but if the first on would be German query will fail , well not fail but it will return nothing.

Proposed resolution

Join on the langcode only if there is one language in field condition, otherwise add the langcode to the WHERE clause.

Remaining tasks

  1. Provide test to prove the bug
  2. Agree on suggested solution
  3. Implement fix
  4. Review
  5. Commit

User interface changes

None

API changes

TBD

Data model changes

NONE

Release notes snippet

TBD

Comments

RoSk0 created an issue. See original summary.

rosk0’s picture

Title: Imporssible to query same field in different languages » Impossible to query same field in different languages
Issue summary: View changes
dww’s picture

I think this is causing trouble at #3092280: Filter by translated taxonomy term for sites with multiple languages too.

I want to be able to do an entity query for a given term name across all languages. It's not clear how to make that work.

It's too bad we have to do all these separate conditions for languages. It'd be great if there was a flag to an entity query to say "find an answer across all languages".

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Issue tags: +Bug Smash Initiative

This came up as a daily BSI target

I believe this may still be valid but I don't do a lot of multi-language sites. I am 50/50 if this is a bug vs task but will leave as is.

Maybe getting a test would be a good start?

dww’s picture

Issue tags: +Needs tests

Blast from the past. Yeah, I'm not really sure this is a bug, now that you mention it.

Tagging for a test. No time right now to write it. 😅

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.