Domanda

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();
È stato utile?

Soluzione

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a drupal.stackexchange
scroll top