I am trying to make a single block which takes the place of core's page title & main content blocks on the block layout / region page. However, a minor core implementation detail is preventing me from doing so, and it has a simple fix.
`block/src/Plugin/DisplayVariant/BlockPageVariant.php` populates the Page Title and Main Content blocks when they are placed via Block Layout. It determines whether the block should receive the page title or main content based on whether the block implements the appropriate interface (MainContentBlockPluginInterface / TitleBlockPluginInterface).
Classes can implement many interfaces. I would like to create a single block which implements both interfaces and, therefore, can fulfill the role of both the main content block and the page title block.
Today, this cannot be done because core's code assumes a block will only implement at most one of the interfaces, as shown here:
if ($block_plugin instanceof MainContentBlockPluginInterface) {
$block_plugin->setMainContent($this->mainContent);
$main_content_block_displayed = TRUE;
}
elseif ($block_plugin instanceof TitleBlockPluginInterface) {
$block_plugin->setTitle($this->title);
}
elseif ($block_plugin instanceof MessagesBlockPluginInterface) {
$messages_block_displayed = TRUE;
}
Core assumes a block will implement only one of MainContentBlockPluginInterface, TitleBlockPluginInterface, or MessagesBlockPluginInterface. A custom/contrib block should be allowed to implement all 3.
Create a custom block which implements MainContentBlockPluginInterface & TitleBlockPluginInterface. Drupal will not call $block_plugin->setTitle.
While reproducing the problem is not straightforward (requires a custom module), I hope the behavior can be seen in the code above.
Change both occurrences of `elseif` to `if` in the code sample above.
I'll submit a merge request shortly.
None. This won't impact core and is very unlikely to affect contrib - it simply unlocks an additional capability.
Previously, Drupal core assumed that blocks would implement at most one of MainContentBlockPluginInterface, TitleBlockPluginInterface, or MessagesBlockPluginInterface. This is no longer the case. The implication is that it is now possible to programmatically create a custom block which implements one or more of those interfaces. This change will have no impact to existing or new sites.
Needs review
11.3 π₯
block.module
Enhances developer experience.
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.
No activities found.