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
Comment | File | Size | Author |
---|---|---|---|
#42 | drupal-PageTitle_block-2938129-42.patch | 9.23 KB | joseph.olstad |
#42 | 2938129-interdiff_41_to_42.txt | 791 bytes | joseph.olstad |
#41 | drupal-PageTitle_block-2938129-41.patch | 9.19 KB | sylus |
#36 | 2938129-36.patch | 10.08 KB | smustgrave |
#36 | interdiff-35-36.txt | 1.74 KB | smustgrave |
Issue fork drupal-2938129
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
tim.plunkettComment #4
dmsmidtFirstly, 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.
Comment #7
tim.plunkettHere's a proposed fix. Though @tedbow and I really are feeling a lot of deja vu around this issue...
Comment #8
tim.plunkettPaired on this with @bnjmnm to write a test.
The FAIL patch is equivalent to an interdiff.
Comment #9
Wim Leers(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
… 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…
Comment #11
tim.plunkettPrecisely. 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.
Comment #12
Kristen PolThanks 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.
Nitpick: Remove extra space before https?
Comment #13
Kristen PolI 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.
Comment #14
tim.plunkettWhile 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
Comment #15
xjmI 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.
Comment #17
Morbus IffThe current patch in #11 doesn't appear to be working against 8.7.
Comment #20
joseph.olstad1) Patch has tests
2) Patch applies cleanly to 9.1.x branch
3) Patch applies cleanly to 9.0.x branch
what is left?
Comment #21
joseph.olstadah 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.
Comment #22
andrewsuth CreditAttribution: andrewsuth at World Food Programme commentedI 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
Comment #25
liquidcms CreditAttribution: liquidcms commentedI 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?
Comment #29
Liam MorlandThis patch re-roll applies to Drupal 10.1 and 9.3 (and perhaps others too). It replaces
core
withcore_version_requirement
.Comment #30
Akram Khanadded default theme and Fixing CCF #29
Comment #31
Liam MorlandIt looks like the new files are missing from the patch in #30.
Comment #32
Akram Khandon'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.
Comment #34
smustgrave CreditAttribution: smustgrave at Mobomo commentedComment #35
smustgrave CreditAttribution: smustgrave at Mobomo commentedStarted 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
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.
Comment #36
smustgrave CreditAttribution: smustgrave at Mobomo commentedFixing 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.
Comment #37
tim.plunkettI think this should be postponed on #3370946: Page title should contextualize the local navigation
Comment #39
smustgrave CreditAttribution: smustgrave at Mobomo commented@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.
Comment #40
sylus CreditAttribution: sylus commentedRe-roll from #29 against D10.2.x as the interdiffs it seemed I had but didn't have time to look further.
Comment #41
sylus CreditAttribution: sylus commentedOoops wrong patch.
Re-roll from #29 against D10.2.x.
Comment #42
joseph.olstadOk, 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.