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();

Most helpful answers

Actually you should only have Entity Plus, as EMW is deprecated.

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.

Is it correct that we should not have both Entity Metadata Wrapper and Entity Plus enabled at the same time? I am getting this and similar errors on multiple Drupal entity_X functions, with only Entity Plus installed.

Actually you should only have Entity Plus, as EMW is deprecated.

Anyone know how to alter this code to work with Entity Plus module since Entity Metadata Wrapper module is deprecated? 

$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();

In the upcoming Entity Plus dependant release the above code should be altered to this (last line changed):

$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';
entity_plus_save('paragraphs_item', $p);