Hey Guys

Im trying to programatically create two paragraphs programatically with the code from this example: https://forum.backdropcms.org/forum/how-does-one-create-paragraphs-field-programmatically

However when I create the second one the values of the field in the first paragraph are getting set to blank.

Simplified example of the behavior:

$node = node_load(208) // Test node created without any paragraphs. 


$item1 = entity_create('paragraphs_item', array('bundle' => 'bundle_item_1', 'field_name' => 'field_item_1'));
$item1->is_new = TRUE;
$item1->field_amount[LANGUAGE_NONE][0]['value'] = "5";
$item1->setHostEntity('node', $node);

// prints this:
[field_amount] => Array
    [und] => Array
          [0] => Array
                 [value] => 5
                 [format] => 
                 [safe_value] => 5



$item2 = entity_create('paragraphs_item', array('bundle' => 'bundle_item_2', 'field_name' => 'field_item_2'));
$item2->is_new = TRUE;
$item2->field_amount[LANGUAGE_NONE][0]['value'] = "5";
$item2->setHostEntity('node', $node);

//prints this (notice that it is for the first item - item 2 now has values)
[field_amount] => Array

Has anybody got a clue to why that could be happening? Could it be related to Backdrop 1.5.1 because i had something simular working about a week ago, but now it does not, and I even tried rolling it back to the code that was working at some point, but didnt make any difference.

Hope somebody can help :)


Your are referencing the same index 0 in both $item1 and $item2.


On your INIT ITEM 2 try:

$item2->field_amount[LANGUAGE_NONE][1]['value'] = "5";

i.e change 0 to 1.


Thank you for tour reply..

The 0 should be correct though as these are the first items of two different paragraph Fields. I have two paragraph fields and want to add an item to each one.. 

Are you using Paragraphs 1.x-1.0.1? If so, can you try with the latest 1.x-1.x (note that this includes a dependency on Entity Plus, so I recommend activating that before upgrading Paragraphs).

Hey laryn

Thank you for your answer.

Yes i am using version 1.x.1.0.1.

So the version 1.x.1.x, how far is that from being stable? I would not want it to cause any issues with my other paragraphs in the other hand. However if a release is planned in near future it would be nice to already have the code set Up for that version.. 

If i am using that version along with Entity Plus, do i have to change anything for the above code to Work?

I think it's close to a release but I'd like to get some additional testing from others first. If you're able to test on a development server or sandbox that would be ideal.

I haven't had time to test the code you have above, but the latest version seems to fix a raft of other issues so I was curious if you could test to see if it fixed your issue, too.

Hey Laryn

I have used the 1.x.1.x module, and looked like it fixed the main issue with the fields being blank :)

However there was a couple of bumps along the way..:

To be able to create paragraphs programatically i had to change line 418 of ParagraphsItemEntity.inc:

return entity_save($this->hostEntityType, $host_entity);

Changed to:

return entity_plus_save($this->hostEntityType, $host_entity);

Before this change i got an error: 

Error: Call to undefined function entity_save() in ParagraphsItemEntity->save() (line 418 of <site>/modules/paragraphs/ParagraphsItemEntity.inc).

After this change I was able to create paragraphs using this code below, and it seems like I am still able to create paragraphs through the interface as well.

$item1 = entity_create('paragraphs_item', array('bundle' => 'bundle_item_1', 'field_name' => 'field_item_1'));
$item1->is_new = TRUE;
$item1->field_amount[LANGUAGE_NONE][0]['value'] = "5";
$item1->setHostEntity('node', $node);
entity_plus_save('paragraphs_item', $item1);

Also, I get a warning globally on the site after the paragraphs module update:

Warning: Declaration of paragraphs_handler_relationship::options_form(array &$form, array &$form_state) should be compatible with views_handler_relationship::options_form(&$form, &$form_state) in require_once() (line 6 of /<site>/modules/paragraphs/views/paragraphs_handler_relationship.inc).

Which is caused by this line, but I am not sure why, or what to do about it.

class paragraphs_handler_relationship extends views_handler_relationship{

Can you help me with that? Of course, I would also like to know if you think the entity_save / entity_plus_save correction above could cause problems elsewhere :)

Thank you again :)

I will, of course, report back if I find any other issues along the way...

I just added fixes for these and they should be in the next release.

Hey Laryn

I am sorry that I never got back to you on this one - I was assigned to another project for a few weeks, so I have not been able to work on this since. But it is perfect that you have added these fixes, thank you very much, I really appreciate it :)

Do you have any estimate on when the next release will be?