5

I am trying to get some node's content inside a block.

I managed to get the fields i wanted, but they are always returned in my site default language instead of the current translations (ex. /node/1 and /fr/node/1 returns the same english content instead of the translation)

It works fine for simple fields, but can't get it to work for referencedEntities :

I am inside a Block build() function :

$node = \Drupal::routeMatch()->getParameter('node');

// Simple text field : Correct value depending on language
$correctValue = $node->field_test_text->getValue(); 

// Paragraphs/referencedEntities
$paras = $node->get('field_paragraphes')->referencedEntities();
foreach ($paras as $para) {
   // Not translated, always in default language ?
   $badValue = $para->get('field_test_in_para')->getValue();
}

Thanks a lot.

3 Answers 3

11

The code for your problem (You need to use getTranslation to get a translation and hasTranslation to check is is it actually has a translation or not):

$lang_code = \Drupal::service('language_manager')->getCurrentLanguage()->getId();
$node = \Drupal::routeMatch()->getParameter('node');

// Paragraphs/referencedEntities
$paras = $node->get('field_paragraphes')->referencedEntities();
foreach ($paras as $para) {
  // Remember to check if translation exists
  if ($para->hasTranslation($lang_code)) {
    $para = $para->getTranslation($lang_code);
  }
   $goodValue = $para->get('field_test_in_para')->getValue();
}

Eventhough this code will work and fix your problem, you might consider if you are doing things right. Most of the time, when doing things the proper way, you wont need to handle translations. A lot of modules/systems does this automatically when rendering stuff or using modules like views. Most of the time, when you need to handle translations yourself, there would be a simpler way to do this.

4
  • Thanks it's working. I am also surprised to have to handle translations manually. Is it due to a bug in entityReference ? The Paragraph Module ? Any way to get the referenced entity directly in the correct language ?
    – Flunch
    Mar 14, 2016 at 15:29
  • @Flunch A lot of the Drupal subsystems does this for you, fx when rendering. What exactly is your goal with the code?
    – googletorp
    Mar 14, 2016 at 16:28
  • I am using the paragraphs module to structure the pages. Each paragraph can have a "title" field. I want to list the title of each paragraph of the current page inside a menu on the side to build an "index" of the page with anchor links. Something like the left menu on this website : link (but not via JS) Thanks for your help !
    – Flunch
    Mar 14, 2016 at 17:06
  • 1
    Înstead of checking that yourself, you can also use api.drupal.org/api/drupal/… (entity.repository service). If you have multiple languages, it will for example attempt to find the best match, e.g. prefer a spanish fallback first before english on the french site. It's also what core uses everyhwere.
    – Berdir
    Mar 14, 2016 at 21:50
7

You can try to force the use of the current language:

$langcode = $this->languageManager->getCurrentLanguage()->getId();
$value = $para->getTranslation($langcode)->field_test_in_para->value;

Or try to use the node language:

$node_language = $node->language()->getId();
$value = $para->getTranslation($node_language)->field_test_in_para->value;
1
  • Thanks it worked ! I had to add : if ($entity->hasTranslation($language)) { otherwise i had errors when a content is not translated.
    – Flunch
    Mar 14, 2016 at 15:25
0

This is definitely due to a core bug. See https://www.drupal.org/project/drupal/issues/2915972 Reviews and support over there would be most welcome. Thanks! -Derek

Your Answer

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

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