5

We are looking for the best way to export a defined/built layout with inline blocks and then use that to programmatically set both them during a module install.

eg.

  • module install
    • create node in content type
    • apply section
    • create and apply block to section in node

Currently we are having to print the array, strip UUIDs.. then attach them to the layout field value of the node we create. Which can lead to issues if not cleaned up correctly.

1
  • Did you solve this? Can you post an answer? – Jason May 3 '20 at 21:10
1

Put this code in an .install file:

// Create a Home web page with alias 'home'.
  $node = Node::create([
    'type' => '<node_type>',
    'langcode' => 'en',
    'uid' => 1,
    'title' => 'Home',
    'status' => 1,
    'path' => [
      'alias' => '/home',
      'pathauto' => PathautoState::SKIP,
    ],
  ]);

  // Variable to hold the sections for this node.
  // Defaulting to have a blank section first.
  $sections[] = new Section('layout_onecol');

  // Create a new section.
  $section = new Section('layout_onecol');

  // Config for the multi-type list block.
  $pluginConfiguration = [
    'id' => '<block_id>',
    'provider' => 'entity_block',
    'label_display' => FALSE,
    'view_mode' => 'default',
  ];

  // Create a new section component using the node and plugin config.
  $component = new SectionComponent($node->uuid(), 'content', $pluginConfiguration);

  // Add the component to the section.
  $section->appendComponent($component);

  // Add the section to the sections array.
  $sections[] = $section;

  // Set the sections.
  $node->layout_builder__layout->setValue($sections);

  // Save the node.
  $node->save();
2
  • You can't just "put this code inside a .install file" and expect it to work, right? Wouldn't you need to put it inside a hook_install() function? – sonfd Aug 5 '20 at 12:25
  • This code will result in: Call to a member function setValue() on null – DiDebru Mar 2 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.