Hi team,
I'm having some issues trying to save some field using paragraphs,
Where i run my code happen this:
The first field has value and second is empty
and the next block the first field is empty and second has value
screenshoot
This is my code:

<?php
$paragraph_1 = Paragraph::create([
    'type' => 'field_from_paragraph',
    'field_item_1' => "abcdefghijk",
]);
$paragraph_icon->save();

$paragraph_2 = Paragraph::create([
    'type' => 'field_from_paragraph',
    'field_item_2' => "123456789",
]);
$paragraph_beneficio->save();

$node = Node::create([
    'type' => 'items',
    'field_from_content_type' => array(
        array(
              'target_id' => $paragraph_1->id(),
              'target_revision_id' => $paragraph_1->getRevisionId()
        ),
        array(
              'target_id' => $paragraph_2->id(),
              'target_revision_id' => $paragraph_2->getRevisionId()
        ),
      ),
    ]);

I don't know what to do to fill both field
Could you please help me?
Regards
Mario

Comments

wombatbuddy’s picture

1. What is the use case? 
2. Where do you run this code, is this a hook or controller?

Also you may try this:

$paragraph_1 = Paragraph::create([
  'type' => 'field_from_paragraph',
  'field_item_1' => 'abcdefghijk',
  'field_item_2' => '123456789',
]);
$paragraph_icon->save();
mxr10_’s picture

Hi @wombatbuddy thank you for, that help me so much now i can add  those field correctly.

1. I'm trying to add some items from a xml file.
2. I've created a custom module and i have a controller where is this code,

But now i have another issue trying to add some translation for paragraphs,
This is my code:

<?php
$paragraph_en = Paragraph::create([
    'type' => 'field_from_paragraph',
    'field_item_1' => "english abcdefg",
    'field_item_2' => "english 1234567",
]);
$paragraph_en->save();

$paragraph_es = Paragraph::create([
    'type' => 'field_from_paragraph',
    'field_item_1' => "spanish abcdefg",
    'field_item_2' => "spanish 1234567",
]);
$paragraph_es->save();

$node = Node::create([
    'type' => 'items',
    'field_from_content_type' => array(
        array(
            'target_id' => $paragraph_en->id(),
            'target_revision_id' => $paragraph_en->getRevisionId()
        ),
    ),
]);

$node->addTranslation('es',[
    'field_from_content_type' => array(
        array(
            'target_id' => $paragraph_es->id(),
            'target_revision_id' => $paragraph_es->getRevisionId()
        ),
    ),
]);

$node->enforceIsNew();
$node->save();

When i run this controller english and spanish have the same value, "english abcdefg" and "english 1234567",
But if i create a content type manually i can add diferent translation for spanish
Could you please help me?
How can i add a translation for paragraphs?

Regards
Mario

wombatbuddy’s picture

mxr10_’s picture

Hi @wombatbuddy, than you for your answer,I tried to implement that code but i can't get it, I'll keep going searching for an answer

wombatbuddy’s picture

When i run this controller english and spanish have the same value

What do "english" and "spanish" mean in your context? Do you want to create a node with two translation or what? Could you please explain your goal one more time? 

ressa’s picture

I was also looking for a way to cycle through a multidimensional array, and save them in a multi-value paragraph field, and found this issue. I finally found a solution, and shared it in #2707017-20: How to create paragraphs item programmatically in D8?.