1

How to create paragraphs item programmatically?

In Drupal 7 I am using the following code.

    $node = node_load($nid);
    $paragraph = new ParagraphsItemEntity(array('field_name' => 'field_name', 'bundle' => 'para_bundlename'));
    $paragraph->is_new = TRUE;
    $paragraph->field_ptest1['und'][0]['value'] = 'Foo';
    $paragraph->setHostEntity('node', $node);
    $paragraph->save();
    node_save($node);

But its not working ?

1 Answer 1

0

This is the code used in various places to create an entity in Drupal 7

$node = node_load(...);

$paragraph = new ParagraphsItemEntity(array('field_name' => 'name_of_field_in_node', 'bundle' => 'name_of_paragraph_bundle'));
$paragraph->is_new = TRUE;
$paragraph->setHostEntity('node', $node);
$paragraph->save();

Field collection module using the following method instead

$node = node_load(...);
$entity = entity_create('paragraphs_item', array('field_name' => 'name_of_field_in_node', 'bundle' => 'name_of_paragraph_bundle'));
$entity->setHostEntity('node', $node);
$entity->save();

Both should work fine. You code looks good to me just remove the last line.

1
  • I already tried with both scenario but its not working :( Commented Jan 27, 2016 at 14:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.