Score:0

Custom block plugin does not show in Layout Builder options

in flag

Newish to Layout Builder - we made a custom block plugin that renders a custom form. When I go to a page with Layout Builder and try to add that block... it is not in the list of blocks.

/**
 * Provides a form block.
 *
 * @Block(
 *   id = "my_block",
 *   admin_label = @Translation("My Block with Form"),
 *   category = @Translation("Custom")
 * )
 */
class MyBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * The form builder.
   *
   * @var \Drupal\Core\Form\FormBuilderInterface
   */
  protected $formBuilder;

  /**
   * Constructs a new MyBlock instance.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Form\FormBuilderInterface $form_builder
   *   The form builder.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, FormBuilderInterface $form_builder) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->formBuilder = $form_builder;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('form_builder')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $build['access_form'] = $this->formBuilder->getForm('Drupal\mymodule\Form\MyForm');
    return $build;
  }

}

Is there something I need to add to the annotation or class to make it an option for Layout Builder? It works fine as a normal Drupal block.

edit: I notice in one random custom module, they have to alter Layout Builder to get this functionality. Is that correct??

https://git.drupalcode.org/project/layout_builder_block/-/blob/1.x/src/EventSubscriber/LayoutBuilderBlockAddControllerSubscriber.php

Score:1
in flag

This project has Layout Builder Browser installed. After adding that block to a category, it is now an option in Layout Builder. Drupal core only seems to otherwise recognize block_content blocks.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.