Skip to main content
Category:

How to attach paragraphs to node programmatically in drupal 8 & 9

 

Code snippet that can be used to attach paragraphs to node programmatically in drupal 8 & 9.

 

Example how to create paragraphs programmatically in drupal 8 & 9


$paragraph1 = Paragraph::create([
  'type' => 'text',
  'field_text' => array(
    "value"  =>  '<h2> texte</h2>',
    "format" => "full_html"
  ),
]);
$paragraph1->save();

 

Create the node and attach the paragraphs

$node = Node::create([
    'type'        => 'page',
    'title'       => 'Title',
    'field_paragraphs'  => array(
      array(
        'target_id' => $paragraph1->id(),
        'target_revision_id' => $paragraph1->getRevisionId(),
      ),
    ),
    'created' => time(),
]);
$node->save();

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook