n Create paragraphs and attach to node programmatically in drupal 10 | CodimTh

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:

In Drupal 10, programmatically creating paragraphs and attaching them to a node involves a series of steps using the Paragraphs module and Drupal's API functions. Below is a step-by-step guide on how to achieve this:

  1. Install and Configure Paragraphs Module: First, make sure you have the Paragraphs module installed and enabled on your Drupal 10 site.

  2. Create a Paragraph Type: Go to the Drupal administration interface and create a new Paragraph Type. Define the fields that you want to include in your paragraphs.

  3. Create a Node: Create the node to which you want to attach paragraphs programmatically using the node_save() function.

// Replace 'your_node_type' with the actual machine name of your content type.
$node = \Drupal\node\Entity\Node::create([
  'type' => 'your_node_type',
  'title' => 'Your Node Title',
  // Add other fields as needed.
]);
$node->save();

 

Create and Attach Paragraphs: Now, you can create paragraph entities and attach them to the node you just created. Use the paragraphs_item_create() and paragraphs_save() functions.

// Replace 'your_paragraph_type' with the actual machine name of your paragraph type.
$paragraph = paragraphs_item_create(['type' => 'your_paragraph_type']);

// Set field values for your paragraph.
$paragraph->set('field_example', 'Example Value');

// Attach the paragraph to the node.
$node->field_paragraph_field_name[] = [
  'target_id' => $paragraph->id(),
  'target_revision_id' => $paragraph->getRevisionId(),
];

// Save the node with attached paragraphs.
$node->save();

 

Note: Replace 'field_paragraph_field_name' with the actual field name on your node type where you want to attach paragraphs.

 

Programmatic creation of paragraphs

Before adding anything to nodes, we have to create the paragraphs first.

$body = '<p>My html text</p>'
$textparagraph = Paragraph::create([
  'type' => 'text',
  'field_text' => array(
    "value"  =>  $body,
    "format" => "basic_html"
  ),
]);
$textparagraph->save();

Second, the media entity paragraph. This takes a bit longer.

$directory = public://media;
$url = 'https://mysite.com/mymediafile.jpg';
if(file_prepare_directory($directory, FILE_CREATE_DIRECTORY)) {
  $file = system_retrieve_file(trim($url), $directory, true);
}
$drupalMedia = Media::create([
  'bundle' => 'image',
  'uid' => '0',
  'field_media_image' => [
    'target_id' => $file->id(),
  ],
]);
$drupalMedia->setPublished(TRUE)
  ->save();

$imageparagraph = Paragraph::create([
  'type' => 'image',
  'field_media' => array(
    'target_id'  =>  $drupalMedia->id(),
  )
]);
$imageparagraph->save();

 

Create the node and attach the paragraphs

$node = Node::create([
    'type'        => 'article',
    'title'       => 'My title',
    'field_paragraphs'  => array(
      array(
        'target_id' => $imageparagraph->id(),
        'target_revision_id' => $imageparagraph->getRevisionId(),
      ),
      array(
        'target_id' => $textparagraph->id(),
        'target_revision_id' => $textparagraph->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