Score:1

Programatically create paragraphs entity form

fr flag

I am in a situation where I would like to split out Paragraph entities to separate forms.

Edit forms are no problem:

$entity = Paragraph::load($entity_id);
return \Drupal::service('entity.form_builder')->getForm($entity);

But how do I get the create form? So far I have this:

$entity = Paragraph::create();
return \Drupal::service('entity.form_builder')->getForm($entity);

... but the information about the node and field to attach the paragraph entity to is needed.

Thank you

Score:2
cn flag

The only information you need to get the create form of a content entity is the bundle (if the entity has bundles):

$entity = Paragraph::create(['type' => 'my_paragraph_type']);
return \Drupal::service('entity.form_builder')->getForm($entity);

You can do the rest after submitting, but you need to update both, the information of the parent entity in the paragraph and vice versa. Paragraphs references are bi-directional.

fr flag
That was easy. Thanks!
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.