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
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
Comment #2
larowlanI 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.
Comment #3
jibranHow can we move this hunk to
\Drupal\Core\Block\BlockBase? The theme function is defined inblock_theme.Comment #4
tim.plunkettWe have \Drupal\Core\Render\RenderableInterface::toRenderable(), maybe use that more.
@jibran good point, that's a bug IMO
Comment #5
jibranWe have
\Drupal\views\Element\Viewwhy not add\Drupal\Core\Render\Element\Block?Comment #6
jibranAdded #2934223: layout_builder module has a hidden dependency on block module for #4.
Comment #7
avpadernoI 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.
Comment #8
tim.plunkettThis is block plugins we're discussing, not entities
Comment #9
avpadernoActually,
BlockViewBuilderis 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
EntityWithPluginCollectionInterfacedoesn't change the relation between the Block entity and theBlockViewBuilderclass, which is the same relation between the Node entity and theNodeViewBuilderclass.Comment #10
tim.plunkett@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.
Comment #11
avpadernoThe 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.
Comment #12
tim.plunkettYou are misunderstanding. I'm not sure how to better explain it to you, maybe we could discuss in realtime and summarize our discussion here?
Comment #13
avpadernoI 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
NodeViewBuilderback into the Node entity. Then, it depends from what exactly move the code currently in BlockViewBuilder into the BlockBase implementation means.Comment #14
tim.plunkettThere 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.
Comment #15
avpadernoIs the misplaced code the code in
BlockViewBuilder::buildPreRenderableBlock()andBlockViewBuilder::preRender()? That seems code I would see inBlockBase.Comment #27
wim leers7 years and 2 days later, this core bug is still biting us: #3494483: Blocks are missing their block.html.twig wrapper.
Comment #29
pancho