How to add paragraph library item programmatically

Created on 25 August 2023, over 1 year ago
Updated 30 August 2023, over 1 year ago

I want to create (with PHP) a paragraph library item, which are listed here: /admin/content/paragraphs

I have a paragraph type "paragraph_text_simple" which has this field (text formatted long): "field_paragraph_text_long".

I think, first, I have to create a paragraph:

$paragraph = Paragraph::create([
      'type' => 'paragraph_text_simple',
      'field_paragraph_text_long' => [
        'value' => "My global paragraph Text",
        'format' => 'basic_html',
     ]
]);

That seems to work.

Then I think I have to use this paragraph as target (created paragraph has the ID 1)?

\Drupal::entityTypeManager()->getStorage('paragraph')->create([
    'type' => 'paragraphs_library_item',
    'langcode'=>'en',
    'label'=>'My global paragraph',
    'parent_id' => 1,
    'parent_type' => 'paragraphs_library_item'

  ]);
💬 Support request
Status

Fixed

Version

1.15

Component

Code

Created by

🇩🇪Germany thomaswalther Rhein-Main Area

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024