Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
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();

Comments

Submitted by pushkalnath (not verified) on Fri, 03/04/2022 - 06:12 Permalink

how does the output look like? will this create a paragraph type
Add new comment

Restricted HTML

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

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

Web Posts

Search

Page Facebook