3

I have a node that has different translations already and I'm trying to populate Paragraph fields in different languages programmatically. I can edit the node and update the paragraph fields manually and they work correctly, so I know the translation configuration is setup correctly.

However, when using the code below, when I create a new paragraph in, say Dutch, and save it to the translated node object it always overwrites the English translation with the Dutch one.

For example:

English Node->MY_PARAGRAPH_VALUE = "English Paragraph Text"
...run code...
English Node->MY_PARAGRAPH_VALUE = "Now has Dutch Paragraph text "

<?php
  /*
   * $node is an existing Node entity with empty multi-value paragraph
   *  fields to populate in different language below
   */
  $lang_code = 'nl';
  $lang_node = $node->getTranslation($lang_code); // copy En node into Dutch Node

  /*
   * Using a Paragraph View to verify,
   * this part works and creates a paragraph with the correct language
   */
  $paragraph = Paragraph::create([
      'type' => 'images_descriptions',
      'field_product_image' => $img[$key],
      'field_product_description' => $desc[$key],
      'langcode' => $lang_code,
  ]);
  $paragraph->save();

  $current[] = [
      'target_id' => $paragraph->id(),
      'target_revision_id' =>$paragraph->getRevisionId(),
  ];

  $lang_node->set('field_images_descriptions', $current);
  $lang_node->save();
  
?>

2 Answers 2

2

The paragraphs ERR field only contains one (pertinent) bit of info - the relationship to the paragraph entity. The field itself shouldn't be translated, because every time you set it, you're setting the paragraph for every translation of that node.

To provide translations, you translate the paragraph entity itself, e.g.

$node->field_images_descriptions->entity
  ->addTranslation($lang_code, [
    'field_product_image' => $img[$key],
    'field_product_description' => $desc[$key],
  ])
  ->save();
1
  • Thank you Clive! I had actually started to figure out that the paragraphs needed to be translated but I've ran into another issue. No matter what I try, when I save the image field (field_product_image) it always gives me the following error: *** InvalidArgumentException: Invalid translation language (nl) specified. *** Do I also need to make a new file and file object for the translated Paragraph? Commented Feb 13, 2021 at 20:12
0

this is the right solution for me :

 $paragraph = Paragraph::create(['type' => 'statistics',]);
$paragraph->set('field_title', " $title $element->year");
$paragraph->set('field_unit', " $unit");
$paragraph->set('field_count', $element->file_count);
$paragraph->addTranslation( $lang, [
  'field_title' => " $titleEn $element->year",
  'field_unit' => $unitEn,
  'field_count' => $element->file_count,
]);
$paragraph->isNew();
$paragraph->save();

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.