Prepopulate programmatically nested Layout Pagagraphs

Created on 19 January 2024, 5 months ago
Updated 2 February 2024, 5 months ago

Hello everyone,
thanks for this fantastic module. I need to prepopulate a layout paragraphs field when a new node is added, and the user will modify it. For that I use hook_node_prepare_form.
I manage to create the paragraphs:

- line Β» section
-- column Β» section
--- my final paragraph 'panel'

But all three appear at the same level, not nested.
I've found information about BehaviorSettings, but nothing works. Do you have any idea how to unblock my situation?
Here's my code for now.

function ff_prepopulate_layout_paragraphs_node_prepare_form($entity, $operation, $form_state)
{
    if ($entity->getType() === 'evenement' && $operation === 'default') {
        $field = $entity->get('field_para_contenus');
        $line = Paragraph::create([
          'type' => 'para_layout',
        ]);
        $column = Paragraph::create([
          'type' => 'colonne',
        ]);
        $panel = Paragraph::create([
          'type' => 'para_panneau',
        ]);
        $column_settings = [
          'parent_uuid' => $line->uuid(),
          'region' => ''
        ];
        $panel_settings = [
          'parent_uuid' => $column->uuid(),
          'region' => ''
        ];
        $column->setBehaviorSettings('layout_paragraphs', $column_settings);
        $panel->setBehaviorSettings('layout_paragraphs', $panel_settings);
        $field->appendItem($line);
    }
}
πŸ’¬ Support request
Status

Fixed

Version

2.0

Component

Code

Created by

πŸ‡«πŸ‡·France elektrorl

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

Comments & Activities

Production build 0.69.0 2024