Hi guys,

I am using Paragraphs to create subitems in a node. For one particular node I need to populate the node add form (through hook_form_alter) with already created paragraphs.

My last effort is this:

if ($form_id == "node_schedule_form") {
      $paragraph = \Drupal\paragraphs\Entity\Paragraph::create([
        'type' => 'agenda_item',
        'field_title' => 'Test agenda 1',
        'field_description' =>  'This is test agenda item 1',
      ]);
      $paragraph->save();
      $form['field_agenda_items']['widget'][0]['target_id']['#value'] = $paragraph;

$paragraph = \Drupal\paragraphs\Entity\Paragraph::create([
        'type' => 'agenda_item',
        'field_title' => 'Test agenda 2',
        'field_description' =>  'This is test agenda item 2',
      ]);
      $paragraph->save();
      $form['field_agenda_items']['widget'][1]['target_id']['#value'] = $paragraph;
  }

I tried a lot of variations of $form['field_agenda_items']['widget'] and everything that comes next, but it's not showing up in the form at all.

Can someone point me in the right direction? I am stuck at this for weeks now. I hope someone can help me with this.

Best,
Jessica

Comments

interactivex created an issue.

joewhitsitt’s picture

I got stuck at the same point you did. This is a simplified version of what I ended up doing.

use Drupal\paragraphs\Entity\Paragraph;

/**
 * Implements hook_ENTITY_TYPE_prepare_form().
 */
function mymodule_node_prepare_form(\Drupal\Core\Entity\EntityInterface $entity, $operation, \Drupal\Core\Form\FormStateInterface $form_state) {
  // On new basic page node.
  if ($entity->bundle() == 'page' && $entity->isNew()) {
    // Create "text" blank paragraph item.
    $paragraph = Paragraph::create([
      'type' => 'text', // Paragraph type.
    ]);
    $paragraph->isNew();

    // Add new "text" paragraph item to "field_name" (Entity Reference Revisions field).
    $field = $entity->get('field_name');
    $field->appendItem($paragraph);
  }
}

You can also save the paragraph item, but the item will still exist if the editor decides not to save the node. I had a bunch of orphan paragraph items when trying to tackle this issue.

$paragraph->save();

berdir’s picture

Try to use hook_node_create() instead of doing it in the form, that should be easer.

joshua.boltz’s picture

dhirendra.mishra’s picture

It does not work in my scenario :

- I was able to achieve above scenario and it does work properly when node is created.
- It also updates the same paragraph if used in other entity (for ex. user entity)
- When now editing node and trying to update value of same paragraph field, then it does get updated in node but same paragraph values does not get updated in user entity.
- I debugged further and found that same paragraph reference is being used but different paragraph revision is loaded (on User entity and node entity).
- My requirement is : It should also update on user entity when node is edited.

can you please guide me how to achieve this ?