Add support for Views blocks and code defined blocks

Created on 20 October 2023, over 1 year ago

Currently only content blocks are supported.

This MR will add support for Views blocks and code defined blocks.

Feature request
Status

Needs work

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom Dan.Ashdown

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Dan.Ashdown
  • @danashdown opened merge request.
  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 🇦🇹Austria fago Vienna

    I don't think it makes sense to start special casing various type of blocks and adding individual support for each. Can we find some generic way to allow all-kind of blocks to opt-into rendering correctly?

    in particular since we have two place rendering blocks - layout-builder and blocks-layout integration, we have to resolve it without code-duplication.

    this is how blocks are handled generically for layout-builder:
    https://git.drupalcode.org/project/custom_elements/-/blob/8.x-2.x/src/Pr...

    Could we simply do the same here and have both content-entity and views block working?

    Still, I think we need to introduce a way to add custom code per-block class that builds the block into custom-elements. So far, we've been doing that by swapping out the block-class for our projects:

    /**
     * Implements hook_block_alter().
     *
     * Alter the block plugin definitions for Layout builder's inline blocks.
     */
    function ldp_layout_builder_block_alter(&$definitions) {
      foreach ($definitions as $id => $definition) {
        if (strpos($id, 'inline_block:') === 0) {
          $definitions[$id]['class'] = LdpInlineBlock::class;
        }
        if (strpos($id, 'block_content:') === 0) {
          $definitions[$id]['class'] = LdpContentBlock::class;
        }
      }
    }
    

    and then this build method:

       * {@inheritdoc}
       */
      public function build() {
        if ($this->getCurrentRequest()->getRequestFormat() == 'custom_elements') {
          $block = $this->getEntity();
          $custom_element = $this->getCustomElementGenerator()
            ->generate($block, $this->configuration['view_mode']);
          return $custom_element->toRenderArray();
        }
        return parent::build();
      }

    let's discuss how to do that best!

Production build 0.71.5 2024