0

Im currently using Drupal::entityQuery() to build a list of node.

I need to be able to query only contentEntities that have a specific translation not published. I struggle because I don't know how to make a condition on an entity translated field/property.

to sum up: How to query all nodes that have 'de' translation AND which their 'de' translation are not published?

Is there a way to do it with Drupal::entityQuery() or should I directly go with altering the query with QueryInterface::addTag() where I would be able to customize the query?

2 Answers 2

5

This should do what you want. Note that the id's will load the default language, you will need to use getTranslation(langCode) to get the translated node.

$query = \Drupal::entityQuery('node');
$query->condition('status', 0);
$query->condition('type', YOUR_TYPE);
$query->condition('langcode', 'de');
$ids = $query->execute();
0

$query->condition('langcode', 'de');

This condition gets only nodes, that have last translation to 'de' language or created on 'de' language and didn't translate to another language (https://www.drupal.org/project/drupal/issues/3092835)

You can use this solution:

$query = \Drupal::entityQuery('node');
$query->condition('status', 0);
$query->condition('type', YOUR_TYPE);
$ids = $query->execute();

//And then check if entity has needed translation:
$language = 'de';
$list = \Drupal::entityTypeManager()->getStorage('node')->loadMultiple($ids);
foreach ($list as $key => $node) {
  if (!$node->hasTranslation($language)) {
    unset($list[$key]);
  }
  else {
    $list[$key] = $node->getTranslation($language);
  }
}

As a result, you have a correct list of entities that have relation to requested language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.