Layout Builder + Auto Generated Content

Last updated on
7 February 2024

Sometimes content generated while placing a block is a bit much...

Too much auto generated content!

Field Sample Value

Out of the box Layout Builder + uses cores generateSampleItems to get some stuff on the page. Sometimes it produces a bit more than we'd like. Layout Builder + integrates with Field Sample Value to address this.

Install
  1. If your site is managed via Composer, use Composer to download the lb_plus module running composer require drupal/field_sample_value. Otherwise copy/upload the field_sample_value module to the modules directory of your Drupal installation.

  2. Enable the 'Field Sample Value' module and desired sub-modules in 'Extend'. (/admin/modules)

  3. Navigate Structure > Block Types > Manage fields > Edit. (/admin/structure/block-content/manage/basic/fields/block_content.basic.body)

  4. On a text field you should have options to Set a sample value.

The nuclear option 

If a field needs a Field Sample Value plugin that hasn't been created yet lets add it! But sometimes Field Sample Value won't get you there either. In that case you can subscribe to the PlaceBlockEvent to tweak anything about the block before it's placed. First, add a service definition.

your.services.yml
services:
  place_block.basic:
    class: Drupal\your_module\EventSubscriber\TextBlocks
    arguments: []
    tags:
      - { name: event_subscriber }

Then add an event subscriber to src/EventSubscriber/PlaceBlockBasic.php.

PlaceBlockBasic.php
<?php

namespace Drupal\your_module\EventSubscriber;

use Drupal\lb_plus\Event\PlaceBlockEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class PlaceBasicBlock implements EventSubscriberInterface {

  public function onPrePlaceBlockFormBuild(PlaceBlockEvent $event) {
    if ($event->getBlockPluginId() === 'inline_block' && $event->getBundle() === 'basic') {
      $block_plugin = $event->getBlockPlugin();
      // Change the block plugin configuration.
      $configuration = $block_plugin->getConfiguration();
      $configuration['label_display'] = 'visible';
      $configuration['label_tag'] = 'h2';
      $configuration['label'] = $block_plugin->label();
      $block_plugin->setConfiguration($configuration);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      // After PlaceBlockFormBuild is called.
      PlaceBlockEvent::class => ['onPrePlaceBlockFormBuild'],
    ];
  }

}

You can change anything in the block configuration in the event subscriber. If you want to get at the BlockContent within the InlineBlock unserialize the block > tweak it > serialize it back like so:
 

      $block_plugin = $event->getBlockPlugin();
      $configuration = $block_plugin->getConfiguration();
      $block_content = unserialize($configuration['block_serialized']);
      $block_content->set('field_my_select_list', 'option_42');
      $configuration['block_serialized'] = serialize($block_content);
      $configuration['label_display'] = 'visible';
      $configuration['label'] = $block_plugin->label();
      $block_plugin->setConfiguration($configuration);

Help improve this page

Page status: No known problems

You can: