Problem/Motivation

\Drupal\Core\Block\BlockPluginInterface specifies a ::build() method for each plugin to implement.

In \Drupal\block\BlockViewBuilder, the result is wrapped in a very useful render array for the block theme hook.

This logic is duplicated in other modules that want to render blocks using the active theme's block templates: Page Manager, Display Suite, Drupal Canvas, and the core Layout Builder module.

In #3494483: Blocks are missing their block.html.twig wrapper, Experience Builder (now Drupal Canvas) adopted Layout Builder's approach after menu blocks rendered without their outer nav element.

#3644100: [PP-2] Display local tasks and local actions without Block module being installed needs the same wrapper to render the local tasks and local actions blocks in SimplePageVariant.

Background

#1927608: Remove the tight coupling between Block Plugins and Block Entities allowed block plugin configuration to be stored directly in a layout. Its stated goal was that the block theme hook should work regardless of where that configuration is stored.

During review of #2926914-52: Rewrite \Drupal\layout_builder\Section to represent the entire section, not just the block info, @tim.plunkett opened this issue as a follow-up to give callers a common way to render a complete, themed block without a block configuration entity. In #14, however, he raised BC concerns about moving the existing rendering logic from BlockViewBuilder into BlockBase.

Proposed resolution

Provide a wrapper method for ::build() called ::toRenderArray(), and move the wrapping logic shared by BlockViewBuilder and Layout Builder into BlockBuilderBase.

For BC, keep both modules' existing entry points. In BlockViewBuilder, modules must still be able to alter the wrapper before ::preRender() builds the plugin content.

Preserve each module's handling of attributes, contextual links, and empty blocks, including the fallback for empty blocks in Layout Builder previews. Cover these behaviors with regression tests before extracting the shared code.

Have the BlockBase implementation delegate to \Drupal\Core\Block\BlockBuilder::build($this). Callers can also use this builder directly with any block plugin.

Either way, callers are responsible for checking access and supplying any required plugin contexts.

Remaining tasks

Postponed on #3644069: Move the block theme hook and its default Twig template from Block to System.

Review.

User interface changes

None.

API changes

BlockPluginTrait now provides ::toRenderArray().

BlockPluginInterface announces it with @method, so existing plugins that implement the interface directly remain compatible.

Data model changes

n/a

Release notes snippet

Modules can now render a block plugin using the active theme's block templates by calling BlockBuilder::build($block).

Issue fork drupal-2931040

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tim.plunkett created an issue. See original summary.

larowlan’s picture

I would argue this should be a standalone interface implemented by BlockPluginBase

That way we can use it for other things..which would allow layout builder to work with said other things.

jibran’s picture

        // @todo Move this to BlockBase in https://www.drupal.org/node/2931040.
        $output = [
          '#theme' => 'block',
          '#configuration' => $plugin->getConfiguration(),
          '#plugin_id' => $plugin->getPluginId(),
          '#base_plugin_id' => $plugin->getBaseId(),
          '#derivative_plugin_id' => $plugin->getDerivativeId(),
          '#weight' => $this->getWeight(),
          'content' => $plugin->build(),
        ];

How can we move this hunk to \Drupal\Core\Block\BlockBase? The theme function is defined in block_theme.

tim.plunkett’s picture

We have \Drupal\Core\Render\RenderableInterface::toRenderable(), maybe use that more.

@jibran good point, that's a bug IMO

jibran’s picture

We have \Drupal\views\Element\View why not add \Drupal\Core\Render\Element\Block?

jibran’s picture

avpaderno’s picture

I don't buy either there should be such method. It seems an anti-pattern, and I don't see why blocks should be different from other entities.

tim.plunkett’s picture

This is block plugins we're discussing, not entities

avpaderno’s picture

Actually, BlockViewBuilder is a class associated to the Block entity. So, this is about removing code from the view builder an entity uses.
The fact the Block entity also implements EntityWithPluginCollectionInterface doesn't change the relation between the Block entity and the BlockViewBuilder class, which is the same relation between the Node entity and the NodeViewBuilder class.

tim.plunkett’s picture

@kiamlaluno what is your point? Block plugins are incorrectly coupled to Block entities, this is a known problem that was not completely resolved by #1927608: Remove the tight coupling between Block Plugins and Block Entities.

Block plugins need to be independently and reliably renderable outside of the Block entity system (remember, block plugins are part of the Drupal\Core\Block subsystem and block entities are in an optional module).

Even if we leave BlockViewBuilder as-is, we need to have a canonical and defined way to render a complete block that is in line with the expectations of a themer. Simply returning the result of BPI::build() without any wrapper has been pushed back on as incomplete.

avpaderno’s picture

The point is that's moving code from a side of the dependency graph to the other side. Then, if it needs to be done for an entity using plugins, it should be done for every entity using plugins.

Adding an helper method in the view builder class is fine, but still the changes should be considered for a generic view builder class, not for just a concrete view builder.

tim.plunkett’s picture

You are misunderstanding. I'm not sure how to better explain it to you, maybe we could discuss in realtime and summarize our discussion here?

avpaderno’s picture

I am just commenting on the move the code currently in BlockViewBuilder into the BlockBase implementation part. To me it would be like moving code in NodeViewBuilder back into the Node entity. Then, it depends from what exactly move the code currently in BlockViewBuilder into the BlockBase implementation means.

tim.plunkett’s picture

There is code in the entity's view builder that actually pertains directly to a plugin.
In core this plugin is currently only used by that entity.

The plugin is being used in contrib, without the wrapper entity.
Anyone (including contrib) attempting to use this plugin must duplicate the misplaced code, in an incorrect way.

This issue is about providing the plugin with its own means of rendering itself.

One proposed solution would be to move the misplaced code.
I personally disagree with that solution, since it will likely cause BC issues.

avpaderno’s picture

Is the misplaced code the code in BlockViewBuilder::buildPreRenderableBlock() and BlockViewBuilder::preRender()? That seems code I would see in BlockBase.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

wim leers’s picture

7 years and 2 days later, this core bug is still biting us: #3494483: Blocks are missing their block.html.twig wrapper.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.