It's very difficult to alter forms of inline (content blocks) placed via Layout Builder

Created on 25 January 2019, over 5 years ago
Updated 19 October 2023, 8 months ago

I was recently looking to modify the form for a content block entity that's placed as an inline block via Layout Builder.

This turns out to be quite difficult:

  • The outer most form is either \Drupal\layout_builder\Form\AddBlockForm or \Drupal\layout_builder\Form\UpdateBlockForm, so that's what needs to be form_altered()
  • Layout Builder's block form works similarly to core's \Drupal\block\BlockForm, in that it embeds configuration form for the block plugin inside of it. I guess this is called a "subform".
  • For inline blocks in layout builder, there's a block plugin class \Drupal\layout_builder\Plugin\Block\InlineBlock. The configuration form for this block plugin is created by building an entity form display for the content block entity that you're trying to create. It does this in a #process callback

Because the actual block content entity form is rendered in a #process callback, a developer that implements a form_alter for layout builder will have a bad time, since the form elements for the block content entity are not actually built yet. Here's what the form array looks like in the form_alter:

Instead, I believe the developer must alter the form to add their own #process callback. And it cannot be added to the outer form array because it's still too early there. It needs to be added to the block_form, after the processBlockForm callback is done.

I wonder if there's a way to improve the developer experience here.

✨ Feature request
Status

Needs work

Version

11.0 πŸ”₯

Component
Layout builderΒ  β†’

Last updated about 3 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States bkosborne New Jersey, USA

Live updates comments and jobs are added and updated live.
  • Blocks-Layouts

    Blocks and Layouts Initiative. See the #2811175 Add layouts to Drupal issue.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ͺπŸ‡ΈSpain guardiola86

    I tried using Block Class module and didn't work for me. My issue was that I couldn't use the same validate and submit functions that were working in the block edit page, but not when using layout builder. I ended up using something like this:

    /**
     * Implements hook_entity_insert().
     */
    function embeddocument_block_entity_insert(EntityInterface $entity) {
      _embeddocument_block_entity_insert_or_update($entity);
    }
    
    /**
     * Implements hook_entity_update().
     */
    function embeddocument_block_entity_update(EntityInterface $entity) {
      _embeddocument_block_entity_insert_or_update($entity);
    }
    
    /**
     * Custom callback used by insert and update.
     * 
     * @param $entity
     * @throws \Drupal\Core\Entity\EntityStorageException
     */
    function _embeddocument_block_entity_insert_or_update($entity) {
      if (\Drupal::moduleHandler()->moduleExists('block_content')) {
        $entity_langcode = $entity->get('langcode')->value;
        if ($entity->bundle() == 'document') {
          $entity_array = $entity->toArray();
          if (isset($entity_array['field_documents'])) {
            $mid = $entity_array['field_documents'][0]['target_id'];
            if (!empty($mid) && !empty($entity_langcode)) {
              $media = Media::load($mid);
              // If translation doesn't exist, create it.
              if (!$media->hasTranslation($entity_langcode)) {
                $media->addTranslation($entity_langcode, $media->toArray());
              }
              $media->save();
            }
          }
        }
      }
    }
    
Production build 0.69.0 2024