Add multiple paragraphs programmatically in an AJAX handler

Created on 1 August 2019, almost 6 years ago
Updated 21 February 2023, about 2 years ago

need to add Paragraph fields programmatically to an entity in an AJAX handler.

This is my AJAX callback:

function myAjaxCallback(array &$form, \Drupal\Core\Form\FormState $form_state)  {
  $paragraph3 = \Drupal\paragraphs\Entity\Paragraph::create([
    'type' => 'media',
    'field_caption' => 'Apart from the included Paragraph types, you can create your own simply by going to Structure -> Paragraphs types.',
  ]);
  $paragraph3->save();

  // Now I need to add $paragraph3 to $form['field_gallery_items']['widget']

  return $form;
}

I need to add the new Paragraph I created to the widget so that when the $form is returned I have added a paragraph.

How Can I do that?

💬 Support request
Status

Active

Version

1.0

Component

Code

Created by

🇮🇹Italy nicola.peluchetti

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024