Prepopulate programmatically nested Layout Pagagraphs

Created on 19 January 2024, 11 months ago
Updated 2 February 2024, 11 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.71.5 2024