Is there any way to add a paragraph in a module or themplate-file?
If there is please add this to the dokumentation

Comments

dafnie created an issue. See original summary.

Primsi’s picture

Component: Documentation » Code

It works the same as with other entities:

$paragraph = Paragraph::create([
     'type' => 'some_para_type',
     'some_para_field' => [
       'value' => $value['value'],
       'format' => $value['format'],
    ]]
 );
$paragraph->save();

$node->set('some_node_field', [
 'target_id' => $paragraph->id(),
 'target_revision_id' => $paragraph->getRevisionId()
]);

$node->save();
garnett2125’s picture

$paragraph = \Drupal\paragraphs\Entity\Paragraph::create([
    'type' => 'image_text',
    'field_description' => array(
      "value" => 'My description',
    ),
    'field_image' => array(
      "target_id" => $file->id(),
      "alt" => "docteur portrait",
      "title" => "docteur portrait",
    ),
  ]);
  $paragraph->save();
  $node->field_page_content->appendItem($paragraph);
  $node->save();
miro_dietiker’s picture

Status: Active » Fixed

In general, if you checkout the tests, there's a lot of code that shows how to save things via API.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.