Score:0

How to render alternate language page (with paragraphs)?

us flag

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.

unusedspoon avatar
aq flag
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
liquidcms avatar
us flag
correct, also does not work.
liquidcms avatar
us flag
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.
Jaypan avatar
de flag
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 https://drupal.stackexchange.com/a/216051/66659
liquidcms avatar
us flag
@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 avatar
us flag
Nope, my bad, possibly class file has to be in subfolder off src Hmm, odd. Anyway, Alberto Silva's answer worked.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.