Support the same block being used for page title & main content

Created on 4 December 2025, about 1 month ago

Problem/Motivation

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.

Steps to reproduce

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.

Proposed resolution

Change both occurrences of `elseif` to `if` in the code sample above.

Remaining tasks

I'll submit a merge request shortly.

User interface changes

None. This won't impact core and is very unlikely to affect contrib - it simply unlocks an additional capability.

Release notes snippet

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.

✨ Feature request
Status

Needs review

Version

11.3 πŸ”₯

Component

block.module

Created by

πŸ‡ΊπŸ‡ΈUnited States ashrafabed

Live updates comments and jobs are added and updated live.
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.

No activities found.

Production build 0.71.5 2024