Problem/Motivation

Page titles in Drupal are determined in two different ways.
One is via the title_resolver service, which delegates to the route.
The other is by inspecting the render array of the main content of the page for a top-level #title key.

While the first approach can be used by any code in Drupal, the #title approach is only available at the very end of the rendering process.
The only code that gets access to the real title is whichever @PageDisplayVariant plugin is used to render the page.

The block.module provides a @PageDisplayVariant plugin that passes on the title directly to the page_title_block block.

Without any code setting the title directly on that block, it will always print an empty string.

Since there can only be one @PageDisplayVariant plugin selected at a time, this means that only one module can have access to the true actual title (in the case of a #title being used).

Proposed resolution

There are two approaches to be considered here.

First, the page_title_block block should use the title_resolver service as a fallback when setTitle() is not called.
This still allows the block.module usecase to properly respect the #title if applicable.
All non-block.module usages will not have that title, but the routing-based title.

This is acceptable as using the page title block within Layout Builder is rare, and using a top-level #title is also very rare, and unlikely to occur on a page using Layout Builder.

Furthermore, with #2359901: Discourage $main_content['#title'] in favor of route titles and title callbacks as a major task, eventually the #title approach will no longer be used.

If this approach is not acceptable, then Layout Builder can use hook_plugin_filter_TYPE__CONSUMER_alter() to remove the page title block from being placeable by Layout Builder. Each contrib solution that also has encountered this problem will also have to implement that hook for their own CONSUMER case.

Remaining tasks

Get sign-off on the proposed resolution above, or abandon it for the backup solution.

User interface changes

The page title block will function the same as before for block.module usages.
For alternate usages, the block will no longer be empty. It will contain the title for that route. It will not display the correct title if the top-level render array contains a #title key (aka Views Pages).

API changes

N/A

Data model changes

N/A

CommentFileSizeAuthor
#42 drupal-PageTitle_block-2938129-42.patch9.23 KBjoseph.olstad
#42 2938129-interdiff_41_to_42.txt791 bytesjoseph.olstad
#41 drupal-PageTitle_block-2938129-41.patch9.19 KBsylus
#40 drupal-PageTitle_block-2938129-40.patch14.14 KBsylus
#36 2938129-36.patch10.08 KBsmustgrave
#36 interdiff-35-36.txt1.74 KBsmustgrave
#35 2938129-35.patch9.63 KBsmustgrave
#35 interdiff-29-35.txt1.58 KBsmustgrave
#32 reroll_diff_29-32.txt5.37 KBAkram Khan
#32 2938129-32.patch3.5 KBAkram Khan
#30 2938129-30.patch3.5 KBAkram Khan
#29 drupal-PageTitle_block-2938129-29.patch8.2 KBLiam Morland
#13 Screen Shot 2019-01-18 at 6.51.25 PM.png193.34 KBKristen Pol
#13 Screen Shot 2019-01-18 at 6.50.36 PM.png182.33 KBKristen Pol
#13 Screen Shot 2019-01-18 at 6.50.17 PM.png247.79 KBKristen Pol
#13 Screen Shot 2019-01-18 at 6.49.54 PM.png179.88 KBKristen Pol
#13 Screen Shot 2019-01-18 at 6.49.37 PM.png189.44 KBKristen Pol
#11 2938129-pagetitle-11-interdiff.txt995 bytestim.plunkett
#11 2938129-pagetitle-11.patch8.16 KBtim.plunkett
#8 2938129-pagetitle-8-PASS.patch7.89 KBtim.plunkett
#8 2938129-pagetitle-8-FAIL.patch4.67 KBtim.plunkett
#7 2938129-pagetitle-5.patch3.22 KBtim.plunkett

Issue fork drupal-2938129

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:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

EclipseGc created an issue. See original summary.

tim.plunkett’s picture

Component: layout.module » block.module

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.

dmsmidt’s picture

Firstly, I can confirm this is an issue.

I see use cases where Layout Builder is used to take over the complete rendering of the page. It is most efficient if Front-enders can just style one block type and don't have to use two different approaches when using Layout Builder on some (entity) pages and regular regions on others.
So I'm in favour of fixing this instead removing it from the listing.

Also, when removing this, all the work in #2959962: Page Title block for Layout Builder does not have a place holder would be in vain.

tim.plunkett’s picture

Status: Active » Needs review
Issue tags: +Needs tests
FileSize
3.22 KB

Here's a proposed fix. Though @tedbow and I really are feeling a lot of deja vu around this issue...

tim.plunkett’s picture

Paired on this with @bnjmnm to write a test.
The FAIL patch is equivalent to an interdiff.

Wim Leers’s picture

+++ b/core/lib/Drupal/Core/Block/Plugin/Block/PageTitleBlock.php
@@ -46,7 +109,7 @@ public function defaultConfiguration() {
+      '#title' => !is_null($this->title) ? $this->title : $this->titleResolver->getTitle($this->request, $this->routeMatch->getRouteObject()),

(I know you're updating the IS, but here's already a review of just the patch without having knowing the surrounding motivations.)

We tried very hard to only have the title resolver. But … there were use cases where this was deemed impossible. The canonical example was a page view whose title included the count of matching results. The only way to compute that is to execute the view; the title cannot be resolved independently. This is all from memory, I hope I'm remembering it correctly. Looks like it's still there: \Drupal\views\Plugin\views\display\Page::execute().

If we can do this, then we should be able to simply do

'#title' => $this->titleResolver->getTitle($this->request, $this->routeMatch->getRouteObject()),

… which I think we'd all vastly prefer :) But until #2403359: Use _title and _title_callback where possible is fixed, which blocks #2359901: Discourage $main_content['#title'] in favor of route titles and title callbacks, I don't think we can do this. Unless we're willing to except that on certain pages, the page title may be incorrect if the title block is placed using Layout Builder…

The last submitted patch, 8: 2938129-pagetitle-8-FAIL.patch, failed testing. View results

tim.plunkett’s picture

Unless we're willing to except that on certain pages, the page title may be incorrect if the title block is placed using Layout Builder…

Precisely. I think with #2359901: Discourage $main_content['#title'] in favor of route titles and title callbacks in mind and the complexity of trying to respect #title for an unknown number of consumers, this is an acceptable improvement.

I added @todos pointing to that issue and rewrote the IS.

Kristen Pol’s picture

Thanks for the patch. I have gone through the code a couple times and not seeing anything obviously wrong except perhaps an extra space (below). I assume I just test this by adding a page title block so I'll try that.

+++ b/core/lib/Drupal/Core/Block/Plugin/Block/PageTitleBlock.php
@@ -46,7 +111,9 @@ public function defaultConfiguration() {
+      //   https://www.drupal.org/node/2359901 is resolved.

Nitpick: Remove extra space before https?

Kristen Pol’s picture

I tested adding a page title block and it is adding the block but not the label of the block. I assume there is already an issue for that but I'll move back to "Needs work" just in case.

tim.plunkett’s picture

While this is still very important, I have moved the stable blocker tag to a short-term fix: #3029819: Do not allow Page Title block to be placed in Layout Builder until it works properly

xjm’s picture

Priority: Normal » Major

I signed off on this in Slack also, but explicitly documenting a +1 to #14 here. :)

However, promoting to major which I think is the correct priority here.

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.

Morbus Iff’s picture

The current patch in #11 doesn't appear to be working against 8.7.

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.

joseph.olstad’s picture

Status: Needs work » Reviewed & tested by the community

1) Patch has tests
2) Patch applies cleanly to 9.1.x branch
3) Patch applies cleanly to 9.0.x branch
what is left?

joseph.olstad’s picture

Status: Reviewed & tested by the community » Needs work

ah looks like there was some reason mentioned above for needs work.

good news is, patch still applies cleanly to 9.0.x
and 9.1.x and has tests.

andrewsuth’s picture

I applied patch #14.

However the title field does not seem to respecting the language settings, the default title language is displayed instead. Whereas the other fields being displayed do match the language.

For example:
In English (default):
- Title field is in English
- Body field is in English

In Italian:
- Title field is in English <-- this should be in Italian.
- Body field is in Italian

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.

liquidcms’s picture

I posted this: #3251104: Page Title block is not available in Layout Builder for User profile. earlier today reporting that the Page Title block was not available in Layout Builder for the User profile page. Someone closed that issue stating this one as a duplicate.

I applied this patch to a clean D9.2.7 site and the Page Title block is still not available when using Layout Builder to layout the User profile page.

Would this issue be expected to fix that?

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.

Liam Morland’s picture

This patch re-roll applies to Drupal 10.1 and 9.3 (and perhaps others too). It replaces core with core_version_requirement.

Akram Khan’s picture

added default theme and Fixing CCF #29

Liam Morland’s picture

It looks like the new files are missing from the patch in #30.

Akram Khan’s picture

don't know why its not creating the patch file for where i actually made changes in file. according to CCF (Drupal\Tests\BrowserTestBase::$defaultTheme is required) i added default theme in (core/tests/Drupal/FunctionalTests/Core/Block/PageTitleBlockTest.php) file but after changes in this file when i run (git status) to check which file is actually changed it showing some different file that is (core/lib/Drupal/Core/Block/Plugin/Block/PageTitleBlock.php) and it is not showing the that exact file where i made changes.

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.

smustgrave’s picture

smustgrave’s picture

Started at #29 as #30 and #32 removed the tests.

Also undid the changes from https://git.drupalcode.org/project/drupal/-/commit/ca8a550c056875254ff00...

Am getting this weird phpstan error not sure why

Running PHPStan on changed files.
 ------ --------------------------------------------------- 
  Line   modules/layout_builder/layout_builder.module       
 ------ --------------------------------------------------- 
  40     Function t invoked with 2 parameters, 1 required.  
  52     Function t invoked with 2 parameters, 1 required.  
  53     Function t invoked with 2 parameters, 1 required.  
  57     Function t invoked with 2 parameters, 1 required.  
  61     Function t invoked with 2 parameters, 1 required.  
 ------ --------------------------------------------------- 

Found 1 bug though, moving the block makes it disappear. Refreshing the page I see the block again and it moved so something weird is going on.

smustgrave’s picture

FileSize
1.74 KB
10.08 KB

Fixing test failure and adding fix for moving block failure I mentioned in #35

But imagine tests will need to be updated based on the fix.

tim.plunkett’s picture

smustgrave’s picture

Status: Needs work » Postponed

@tim.plunkett sounds good! If you get time mind reviewing though? Had to put a number of checks for layout builder, moving of blocks, and just regular titles.

Added some additional tests.

sylus’s picture

Re-roll from #29 against D10.2.x as the interdiffs it seemed I had but didn't have time to look further.

sylus’s picture

FileSize
9.19 KB

Ooops wrong patch.

Re-roll from #29 against D10.2.x.

joseph.olstad’s picture

Ok, there's a deprecation on str_contains when using PHP 8.1/PHP 8.2+, in some case came out null, so this is an interdiff and new patch making sure that it's an empty string that is used instead of a null.