0

I am using the Paragraph module in and I need to programmatically create paragraph entities and attach them to nodes.

Using the following code in a for-loop:

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

The paragraph entities are created and attached to the target node but the data is NULL for all 3 entries as the image below shows:

enter image description here

A properly working set of Paragraph entities attached to a node look like the following:

enter image description here

So, instead of UND i:e undefined, I am trying to attach programmatically created arrays of paragraph entities to my nodes.

Can anyone advice how I can modify my code to include my array of Paragraphs data?

0

I have a similar function and would recommend that if you already know what language your node is, you capture it from the node and use it for the language in the paragraph field. I only had 1 language to worry about but you could easily replace LANGUAGE_NONE('und') with the appropriate language code from the node.

$paragraph = new ParagraphsItemEntity([
  'field_name' => $host_field_name,
  'bundle'     => $para_bundle,
]);
$paragraph->is_new = TRUE;
$paragraph->setHostEntity('node', node_load($this->node->nid));
$paragraph->field_long_text[LANGUAGE_NONE][0]['value'] = $field_content;
$paragraph->field_long_text[LANGUAGE_NONE][0]['format'] = 'wysiwyg';
$paragraph->save();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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