Programatically initialising paragraph field in drupal 8 I would like to programatically populate a paragraph field with a default value for one of my content types.

I came up with the following solution:

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

function my_site_node_create(NodeInterface $node) {
    switch ($node-&gt;getType()){
        case 'my_content_type':
            $paragraph = Paragraph::create([
                'type' =&gt; 'references',  // paragraph type machine name
                'field_references_title' =&gt; [  // paragraph's field machine name
                    'value' =&gt; 'Some examples of what we have done' // body field value
                ],
            ]);
            $paragraph-&gt;save();

            $node-&gt;set('field_paragraphs_content', [
                [
                    'target_id' =&gt; $paragraph-&gt;id(),
                    'target_revision_id' =&gt; $paragraph-&gt;getRevisionId(),
                ],
            ]);
            break;
    }

}


This code seems to work but im worried I might be creating useless Paragraph-entities when the user doesn't save this node (because of the '$paragraph->save()').

How can I avoid this?
You should be able to rely on the auto-creating of new paragraphs, instead of assinging the ID, do not save the paragraph and assign it with 'entity' =&gt; $paragraph.

This should then actually save the paragraph when the node is saved.