Programatically create layout builder section in Drupal

A sample script which can create a one column layout and store body field in it.

namespace Drupal\custommodule\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
use Drupal\node\Entity\Node;
use Drupal\block\Entity\Block;
use Drupal\block_content\Entity\BlockContent;
use Drupal\layout_builder\Entity;

$entity = Node::load(10);
    $section = new Section('layout_onecol');
    $uuid = $entity->uuid();
    $region = 'content';
    $pluginConfiguration = [
        'id' => 'field_block:node:article:body',
        'provider' => 'layout_builder',
        'label_display' => FALSE,
        'view_mode' => 'default',
        'entity' => $entity->id(),
        'context_mapping' => [
            'entity' => 'layout_builder.entity',
        ],
    ];
    $component = new SectionComponent($uuid, $region, $pluginConfiguration);
    $section->appendComponent($component);
    $entity->layout_builder__layout->setValue($section);
    $entity->save();


////////// IMPROVED VERSION /////////////////////
// Working start.
    $block_content = BlockContent::create([
        'type' => 'message',
        'info' => 'Demo Block1',
        'reusable' => '0',
        'body' => [
            [
                'value' => 'This is the block content1',
                'format' => filter_default_format(),
            ],
            'reusable' => '0',
        ],
    ]);
    $block_content->set('body', 'This is Super demo11111');
    $block_content->save();
    $block_ids['first'] = $block_content->getRevisionId();

    $block_content = BlockContent::create([
        'type' => 'message',
        'info' => 'Demo Block2',
        'reusable' => '0',
        'body' => [
            [
                'value' => 'This is the block content2',
                'format' => filter_default_format(),
            ],
            'reusable' => '0',
        ],
    ]);
    $block_content->set('body', 'This is Super demo2');
    $block_content->save();
    $block_ids['second'] = $block_content->getRevisionId();

    $block_content = BlockContent::create([
        'type' => 'message',
        'info' => 'Demo Block3',
        'reusable' => '0',
        'body' => [
            [
                'value' => 'This is the block content3',
                'format' => filter_default_format(),
            ],
            'reusable' => '0',
        ],
    ]);
    $block_content->set('body', 'This is Super demo3');
    $block_content->save();
    $block_ids['third'] = $block_content->getRevisionId();

    $entity = Node::load(14);
    $section[0] = new Section('layout_threecol_section');
    $section[1] = new Section('layout_threecol_section');
    $section[2] = new Section('layout_threecol_section');


      \Drupal::logger('my_module')->notice("called once");
    $i = 0;
      foreach($block_ids as $region => $block_revision_id) {
        $pluginConfiguration = [
            'id' => 'inline_block:message',
            'provider' => 'layout_builder',
            'label_display' => 'visible',
            'view_mode' => 'full',
            'block_revision_id' => $block_revision_id,
        ];
        $component = new SectionComponent(\Drupal::service('uuid')->generate(), 'first', $pluginConfiguration);
        $section[$i]->appendComponent($component);
        $i++;
      }
    $entity->layout_builder__layout->setValue($section);
    $entity->save();

/////////////////////////////////////////////////////////////////////

inspired from:
https://www.droptica.com/blog/layout-builder-building-drupal-8-layouts/

core/modules/layout_builder/tests/fixtures/update/layout-builder-field-block.php

Other code references:
select * from node_revision__layout_builder__layout where entity_id='13';
https://drupal.stackexchange.com/questions/281294/delete-a-block-of-all-layouts
https://www.drupal.org/project/drupal/issues/2942975

{
  "fda2e964-4a5e-4203-81ff-09954d1fb919": {
    "uuid": "fda2e964-4a5e-4203-81ff-09954d1fb919",
    "region": "first",
    "configuration": {
      "id": "inline_block:text_block",
      "label": "Non Reusable Block",
      "provider": "layout_builder",
      "label_display": "visible",
      "view_mode": "full",
      "block_revision_id": "2",
      "block_serialized": null,
      "context_mapping": []
    },
    "additional": [],
    "weight": 0
  },
  "f21a9758-3abe-4c96-8c60-53496fd31e42": {
    "uuid": "f21a9758-3abe-4c96-8c60-53496fd31e42",
    "region": "second",
    "configuration": {
      "id": "block_content:6255d80f-07e4-455e-bd47-f7a37fa9cee0",
      "label": "Reusable Block",
      "provider": "block_content",
      "label_display": "visible",
      "status": true,
      "info": "",
      "view_mode": "full",
      "context_mapping": []
    },
    "additional": [],
    "weight": 0
  }
}

Comments

Popular posts from this blog

Code quality analysis of Drupal. Can I use Sonar?

Set up Drupal7 to Drupal8 migration in simple steps (using Drush)