0

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

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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