I'm trying to get a list of all of a specific content type, but my entityQuery is only returning the original language of the site (English). Here's my code:

  public function fetchDocumentReferenceOptions() {
$language = \Drupal::languageManager()->getCurrentLanguage()->getId();
$query = \Drupal::entityQuery('node')
  ->condition('status', 1)
  ->condition('type', 'document')
  ->condition('langcode', $language)
  ->sort('title', 'ASC');

$nids = $query->execute();

$nodes = node_load_multiple($nids);

$options = [];

foreach($nodes as $node)
  // check if the document (content type) has an uploaded file
  if ($node->field_document->entity) {
      $options[$node->id()] = $node->getTitle();

return $options;


This is how I call the function:

'#options' => $DocumentReferenceCore->fetchDocumentReferenceOptions(),

What I am expecting, based on condition for langcode would be that, if, for example, I am editing a Chinese language node (translated from the original English node), I should see Chinese nodes, and the content being returned, but instead I am still seeing the original English content.

What am I doing wrong here?

  • Your code should work, i have tested it i works perfectly, but i just call the function directly not in some form #options' => ... , try to dump the $language and se if it's the right language ! the condition is right.
    – berramou
    Commented Oct 8, 2018 at 21:40

1 Answer 1


Content entities have an active language concept. When loaded, they by default return values for the default translation.

With your query, you know it has that translation, so you can use this code.

$node = $node->getTranslation($language);

If you're not sure it exists, you can use this code.

$custom_tag = \Drupal::service('entity.repository')->getTranslationFromContext($node, $desired_langcode);

That will return the node matching the language, if it exists, or the best alternative. You can also leave out the language code; in that case it will use the current content language.

  • I'm having translation issues. I have a custom block in which I EntityQuery multiple nodes and deliver them to a twig template. The nodes are always in the default site language. Your $custom_tag suggestion fixes the issue @Berdir, but I would like to understand why they do not return the translated values by default, as you suggest in your first statement.
    – RominRonin
    Commented Apr 17, 2020 at 15:49
  • OK, I just understood what you meant in the opening sentence: by default, all content entity queries return the default site language. I guess the 'active language concept' means I have to 'actively' translate them to the language I need? Thanks @Berdir, as usual ;)
    – RominRonin
    Commented Apr 17, 2020 at 16:14

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.