Problem/Motivation

Use Case

  1. You have a content type (i.e. Basic Page) that uses Layout Builder.
  2. You have a custom block type which is added as block content to the content type (i.e. hero)
  3. You want blocks to be populated in layout builder on node creation, without the content editor needing to go into layout builder and add each manually. The editor would only need to edit the existing blocks and could add more if desired.

Im not sure if this 100% falls into the purview of this module, but I was attempting to use this as a solution for the above use case. Initially I tried:

  1. Create block content items in the default layout for the content type
  2. Export the configuration
  3. Use this module to export the block_content items (and corresponding files+media)

This worked (sometimes) on a fresh installation, but other times would fail with the message:

Call to getEntityTypeId on NULL

This was due to the block id / revision id being different between what was in default configuration vs what the blocks actually were. I was not sure the best way to handle this or if there is a straightforward way.

My current solution is to use a hook_node_insert() and programmatically create the blocks within the layout using the UUIDs of the block_content items created from the Default Content module, as the UUIDs will be consistent whereas ids can change based on the site.

I guess what I am wondering is...is this a good solution? Are there better ways to handle this?

Here is the code:

<?php

use Drupal\block_content\Entity\BlockContent;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
use Drupal\node\Entity\Node;

function mymodule_default_content_node_insert(Node $node) {
  if ($node->bundle() !== 'page') {
    return;
  }
  // Load default media items provided by default_content.
  $default_img =\Drupal::entityTypeManager()->getStorage('media')->loadByProperties(['uuid' => 'bb84cc22-643f-45eb-b2f4-5ecbb513c802']);
  $default_img = reset($default_img);
  $default_svg = \Drupal::entityTypeManager()->getStorage('media')->loadByProperties(['uuid' => '03c48d3a-0dbb-4787-a26b-6cb0acef5a1b']);
  $default_svg = reset($default_svg);
  // Create home banner.
  $section_settings = [
    'label' => 'Banner',
    'layout_builder_styles_style' => [
      'banner' => 'banner',
    ],
  ];
  $sections[] = new Section('layout_onecol', $section_settings);
  $block_content = BlockContent::create([
    'type' => 'page_banner',
    'info' => 'Page Banner Default',
    'reusable' => '0',
    'field_banner_image' => [
      'target_id' => $default_img->id(),
    ],
    'field_banner_title' => "This is the default banner title.",
  ]);
  $block_content->save();
  $block_revision_id = $block_content->getRevisionId();
  $plugin_config = [
    'id' => 'inline_block:page_banner',
    'provider' => 'layout_builder',
    'label_display' => 'visible',
    'view_mode' => 'full',
    'block_revision_id' => $block_revision_id,
  ];
  $component = new SectionComponent(\Drupal::service('uuid')->generate(), 'content', $plugin_config);
  $sections[0]->appendComponent($component);

  // Create Quicklinks section.
  $section_settings = [
    'label' => 'Quick Links',
    'layout_builder_styles_style' => [
      'padding_large' => 'padding_large',
      'small_flexed_container' => 'small_flexed_container',
    ],
  ];
  $sections[] = new Section('layout_onecol', $section_settings);

  // Create quicklink 1.
  $block_content = BlockContent::create([
    'type' => 'quick_links',
    'info' => 'Quick Links',
    'reusable' => '0',
    'field_card_icon' => [
      'target_id' => $default_svg->id(),
    ],
    'field_card_title' => "Quicklink",
    'field_card_link' => [
      'uri' => 'https://www.google.com',
      'title' => 'Click Me',
    ]
  ]);
  $block_content->save();
  $block_revision_id = $block_content->getRevisionId();
  $plugin_config = [
    'id' => 'inline_block:quick_links',
    'provider' => 'layout_builder',
    'label_display' => 'visible',
    'view_mode' => 'full',
    'block_revision_id' => $block_revision_id,

  ];
  $component = new SectionComponent(\Drupal::service('uuid')->generate(), 'content', $plugin_config);
  $sections[1]->appendComponent($component);

  // Create quicklink2.
  $block_content = BlockContent::create([
    'type' => 'quick_links',
    'info' => 'Quick Links 2',
    'reusable' => '0',
    'field_card_icon' => [
      'target_id' => $default_svg->id(),
    ],
    'field_card_title' => "Quicklink 2",
    'field_card_link' => [
      'uri' => 'https://www.google.com',
      'title' => 'Click Me',
    ]
  ]);
  $block_content->save();
  $block_revision_id = $block_content->getRevisionId();
  $plugin_config = [
    'id' => 'inline_block:quick_links',
    'provider' => 'layout_builder',
    'label_display' => 'visible',
    'view_mode' => 'full',
    'block_revision_id' => $block_revision_id,
  ];
  $component = new SectionComponent(\Drupal::service('uuid')->generate(), 'content', $plugin_config);
  $sections[1]->appendComponent($component);

  $section_settings = [
    'label' => 'Ping Pong 1',
  ];

  // Create ping pong 1 section.
  $sections[] = new Section('layout_onecol', $section_settings);
  // Create ping pong 1.
  $block_content = BlockContent::create([
    'type' => 'ping_pong',
    'info' => 'Ping Pong 1',
    'reusable' => '0',
    'field_alignment' => 'image_left',
    'field_asset_type' => 'image',
    'body' => [
      'value' => '<p>This is the default body for a ping pong.</p>',
      'format' => 'basic_html',
    ],
    'field_media_item' => [
      'target_id' => $default_img->id(),
    ],
    'field_ping_pong_super_title' => 'This is the super title',
    'field_ping_pong_title' => 'Default Title.',
  ]);
  $block_content->save();
  $block_revision_id = $block_content->getRevisionId();
  $plugin_config = [
    'id' => 'inline_block:ping_pong',
    'provider' => 'layout_builder',
    'label_display' => 'visible',
    'view_mode' => 'full',
    'block_revision_id' => $block_revision_id,
  ];
  $component = new SectionComponent(\Drupal::service('uuid')->generate(), 'content', $plugin_config);
  $sections[2]->appendComponent($component);

  // Create ping pong 2 section.
  $section_settings = [
    'label' => 'Ping Pong 2',
  ];
  $sections[] = new Section('layout_onecol', $section_settings);
  // Create ping pong 2.
  $block_content = BlockContent::create([
    'type' => 'ping_pong',
    'info' => 'Ping Pong 2',
    'reusable' => '0',
    'field_alignment' => 'image_right',
    'field_asset_type' => 'image',
    'body' => [
      'value' => '<p>This is the default body for a ping pong.</p>',
      'format' => 'basic_html',
    ],
    'field_media_item' => [
      'target_id' => $default_img->id(),
    ],
    'field_ping_pong_super_title' => 'This is the super title',
    'field_ping_pong_title' => 'Default Title.',
  ]);
  $block_content->save();
  $block_revision_id = $block_content->getRevisionId();
  $plugin_config = [
    'id' => 'inline_block:ping_pong',
    'provider' => 'layout_builder',
    'label_display' => 'visible',
    'view_mode' => 'full',
    'block_revision_id' => $block_revision_id,
  ];
  $component = new SectionComponent(\Drupal::service('uuid')->generate(), 'content', $plugin_config);
  $sections[3]->appendComponent($component);
  $node->layout_builder__layout->setValue($sections);
  $node->save();
}

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Comments

dan612 created an issue.