0

I have code running from a hook_node_update which renders the full content so that I can parse the result.

  $view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
  // Drupal fails to handle language correctly here.. need to fake it out.. ughh!!
  $pre_render = $view_builder->view($node, 'full', $langcode);
  $html = \Drupal::service('renderer')->render($pre_render);
  $dom = Html::load($html);

this has been working fine when I edit the EN version of the page or when I edit the FR version of the page. I have now realized that I need to do both EN and FR when either is updated.

I figured looping through en/fr would do this, but no matter what I try; I seem to be only able to render in the current language.

I have even tried using $node->getTranslation('fr') when updating the EN page, but no luck.

What am I missing here? viewBuilder() seems to be language aware; yet it isn't.

EDIT

Apparently my issue stems from having Paragraphs on the page. It seems that there is no recursive method to render nodes with referenced entities like Paragraphs.

Solution seems to be that it is necessary to go though all the paragraph files on the node and create render arrays for each one (in the correct language) and then attach them to an overall render array. For my particular use case where content order of the rendered output will not matter; this is unfortunate but possible.

6
  • When you say you've tried $node->getTranslation('fr')how did you use it? e.g. $fr = $node->getTranslation('fr'); $pre_render = $view_builder->view($fr, 'full', $langcode); as i would have thought that would work Feb 15, 2023 at 15:12
  • correct, also does not work.
    – liquidcms
    Feb 15, 2023 at 16:21
  • Ahh.. i see the issue now. $pre_render = $view_builder->view($node, 'full', $langcode); is language aware but it only renders the direct node content in that language; it doesn't use the $langcode for the paragraphs i have on the node. Hmm.. suspect a core bug as opposed to a paragraph bug.
    – liquidcms
    Feb 15, 2023 at 16:40
  • 1
    Paragraphs may ignore the language of the node, and use the current language instead. You may need to temporarily change the current language to the language you want the translation for, fetch the translated node, then switch the current language back. Maybe this can help drupal.stackexchange.com/a/216051/66659
    – Jaypan
    Feb 15, 2023 at 17:29
  • @Jaypan, thanks for this. I knew there had to be a better solution than trying to recursively render and replace each paragraph with its individually translated one. Unfortunately none of the solutions on that page quite work - perhaps just not valid with D9 anymore. Will see if i can find updated way of doing what Alerberto Silva's answer is trying to do.
    – liquidcms
    Feb 15, 2023 at 19:20

0

Your Answer

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

Browse other questions tagged or ask your own question.