Question

I'm trying to programmatically update a multivalue paragraph field and add a translation. In the code below field_product_description is the paragraph's field that I'm updating and field_images_descriptions is the node's field that references the paragraph. The code only updates the first iteration ([0]) of the paragraph. How can I also update the other paragraph values?

<?php
                  $lang_node->field_images_descriptions->entity
              ->addTranslation($lang_code, [
                  // field_product_description is a multi-value field
                  // how can I update it's other values
                  'field_product_description' => $desc1,
              ])->save();
    ?>

SOLVED: Working code:

<?php

foreach ($lang_node->field_images_descriptions as $delta => $item) {

          $item->entity->addTranslation($lang_code, [
              'field_product_description' => $desc1[$delta],
          ])->save();
        }

        $lang_node->save();
Was it helpful?

Solution

You have to loop over the multi-value field:

foreach ($lang_node->field_images_descriptions as $delta => $item) {
  $item->entity->addTranslation($lang_code, [
    'field_product_description' => $desc[$delta],
  ]);
}

$lang_node->save();
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top