4

How do I store values to either a field collection or a paragraphs field programmatically?

Could somebody please provide a working code example of how you do this?

I would have a field holder containing the following three fields where I want to store down my data:

  • field_title - (1 value text field)
  • field_text - (1 value text field - long)
  • field_sources - (unlimited no of values text field)

Thanks for helping me out.

9

Paragraphs and field collections are both their own entity and the fields pointing to them are just entity referenced.

So if you want to create a node with a referenced paragraph, you first create the paragraph with all the values you want, save it, then create the node and point it to the paragraph you created.

The Paragraph demo module has example code that does exactly that in paragraphs_demo_install():

  use Drupal\node\Entity\Node;
  use Drupal\paragraphs\Entity\Paragraph;

  $paragraph3 = Paragraph::create([
    'type' => 'text',
    'field_text_demo' => 'Apart from the included Paragraph types, you can create your own simply by going to Structure -> Paragraphs types.',
  ]);
  $paragraph3->save();

  // Add demo content with two paragraphs.
  $node = Node::create(array(
    'type' => 'paragraphed_content_demo',
    'title' => 'Welcome to the Paragraphs Demo module!',
    'field_paragraphs_demo' => array(
      array(
        'target_id' => $paragraph3->id(),
        'target_revision_id' => $paragraph3->getRevisionId(),
      ),
    ),
  ));
  $node->save();
  • This was extremely helpful. Seems to be very few examples of this anywhere. – Jason Glisson Jun 21 at 17:55
1

I'm on Drupal 8.3.4 with paragraphs 8.x-1.1. When I use the code in the other answer, the node gets created without any reported problem.

But when I go to view the node, I get this error: Error: Call to a member function displaySubmitted() on null in template_preprocess_node() (line 621 of core\modules\node\node.module).. When I try to edit it, I get this error: Error: Call to a member function getPreviewMode() on null in Drupal\node\NodeForm->actions() (line 182 of core\modules\node\src\NodeForm.php).

The code in this comment worked for me.

use Drupal\paragraphs\Entity\Paragraph;  // Don't forget to use a little Class...

/**
 * Create a New Paragraph and attach it to it's node
 * @param int $nid
 *  The node id this paragraph should be attached to
 * @param longtext $content
 *  The body text of the paragraph
 * @param int fid
 *  The file id if any, or FALSE
 * 
 */
function CUSTOM_MODULE_create_paragraph($nid, $content, $fid = FALSE) {
  $node = entity_load('node', $nid);
  $paragraph = Paragraph::create(['type' => 'PARAGRAPH_TYPE',]);
  $paragraph->set('TEXT_FIELD_NAME', $content); 
  if ($fid) {
    $file = file_load($fid);
    $paragraph->set('IMAGE_FIELD_NAME', $file);
  }
  $paragraph->isNew();
  $paragraph->save();   

  // Grab any existing paragraphs from the node, and add this one 
  $current = $node->get('NODE_FIELD_NAME')->getValue();
  $current[] = array(
      'target_id' => $paragraph->id(),
      'target_revision_id' => $paragraph->getRevisionId(),
    );
  $node->set('NODE_FIELD_NAME', $current);
  $node->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.