serundeputy's picture

I'm trying to create a paragraphs field on a node programmatically.  This is what I've found for Drupal 7: https://drupal.stackexchange.com/questions/188192/how-to-create-paragraphs-item-programmatically , but that is not working for me in Backdrop.  Here is the error I receive:

Error: Call to undefined function entity_save() in ParagraphsItemEntity->save() (line 429 of /app/web/modules/contrib/paragraphs/ParagraphsItemEntity.inc).
Drush command terminated abnormally due to an unrecoverable error.

Accepted answer

This requires the entity_metadata_wrapper module. In D7 the entity_save() function comes with the entity module, but in Backdrop that function doesn't exist outside of the entity_metadata_wrapper module.

With that module installed, the code should basically be the same. Here is another example that worked for me:

  • Paragraph type: my_paragraph
  • Paragraph field on node: field_paragraph_field
  • Simple text field on paragraph entity: field_simple_text
$n = node_load(239);
$p = entity_create('paragraphs_item', array('bundle' => 'my_paragraph', 'field_name' => 'field_paragraph_field'));
$p->is_new = TRUE;
$p->setHostEntity('node', $n);
$p->field_simple_text[LANGUAGE_NONE][0]['value'] = 'what what';
$p->save();

Comments

This requires the entity_metadata_wrapper module. In D7 the entity_save() function comes with the entity module, but in Backdrop that function doesn't exist outside of the entity_metadata_wrapper module.

With that module installed, the code should basically be the same. Here is another example that worked for me:

  • Paragraph type: my_paragraph
  • Paragraph field on node: field_paragraph_field
  • Simple text field on paragraph entity: field_simple_text
$n = node_load(239);
$p = entity_create('paragraphs_item', array('bundle' => 'my_paragraph', 'field_name' => 'field_paragraph_field'));
$p->is_new = TRUE;
$p->setHostEntity('node', $n);
$p->field_simple_text[LANGUAGE_NONE][0]['value'] = 'what what';
$p->save();

@daggerhart thanks!

this works like a charm.