Problem/Motivation
Steps to reproduce
Proposed resolution
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
Problem/Motivation
The following error is generated when rendering an extra field block on a layout builder enabled page, when the extra field renders a block content entity.
Undefined array key "view_mode" in block_content_theme_suggestions_block_alter() (line 201 of core/modules/block_content/block_content.module)
Steps to reproduce
Add an extra field block to a layout builder enabled page for an extra field that renders a block content entity. Then view the page.
Proposed resolution
Change the code to extract the view mode from the content, instead of configuration. The view mode is present in content for both regular block plugins and for an extra field rendering a block content, while it is present in configuration for regular block plugins only.
Remaining tasks
MR for backport to Drupal 10
User interface changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #64 | drupal_core_block_content_theme_suggestions_block_alter.patch | 1.79 KB | attraktive |
Issue fork drupal-3468180
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
john.oltman commentedThe attached patch currently applies to the 11.0.x and 10.3.x and 10.4.x branches.
Comment #3
cilefen commentedComment #5
john.oltman commentedThe patch attached to this comment is a re-roll based on the MR. The original patch in #2 does not pass tests because it reorders the suggestions.
Comment #6
john.oltman commentedComment #7
smustgrave commentedPossible to get test coverage around this issue?
Comment #8
john.oltman commentedAdded a test. The test passes with the MR and fails when run using the "test only" pipeline, as desired.
Comment #9
smustgrave commentedMade a small change directly. Don't think we need to mention the ticket in the comment since git blame can show that. But did leave the comment that this is solving a php warning.
Test-only failure does indeed show coverage and rest of the changes appear fine.
Comment #10
john.oltman commentedMakes sense, thank you.
Comment #11
catchThe steps to reproduce say:
But the function does this:
So it shouldn't run for field or extra field blocks at all, and I don't see how a block content block can be rendered without a view mode set.
The test coverage is quite low-level so doesn't confirm the steps to reproduce either. It's not clear to me how we end up in this situation, and if so, whether the test is fixing the right thing or not - if this alter is running for the wrong blocks, then that's the thing that should be changed.
Comment #12
john.oltman commentedThanks for that analysis which is spot on. I've updated the 'steps to reproduce' to a much narrower use case, which is when the extra field builds a render array containing a block content entity. The
layout_builder_entity_view_alter()function callsExtraFieldBlock::replaceFieldPlaceholder()when it finds an extra field in its layout, and that function does a simple replacement of the content for the plugin. If that content contains a render array generated by a call like\Drupal::entityTypeManager()->getViewBuilder('block_content')->view($block_content_entiy, 'default'), the block content suggestions logic will trigger this issue.Creating a test that instantiates the above would be doable but non-trivial, and more work than I have time for today. I'll put this back into "needs review" to get your thoughts on whether that complicated test is worth it. My thought is that the suggestions logic should be defensive in its approach, and the existing test covers that, but put this back into "needs work" if you feel the complicated test is a requirement to proceed.
Comment #15
sonfdI am also seeing this issue when using the Fixed Block Content contrib module.
The module renders a block_content entity directly as its only content, the same situation as described by OP. This situation does seem perfectly reasonable to me. There's any number of reasons why a block, e.g. a custom block, might only directly render a block_content entity as its content.
MR !9426 attempts to resolve this issue by looking to
$variables['elements']['content']['#view_mode']to determine the view mode of the block_content entity rather than$variables['elements']['#configuration']['view_mode'].Comment #16
sonfdHide patches since we are now using MRs.
Comment #17
smustgrave commentedThere are now 2 MRs up with different approaches and 1 not having test coverage.
Comment #19
john.oltman commentedI can confirm that the approach in MR 9426 from @sonfd handles my use case as well as regular block plugins, so I closed my MR and updated the existing test so MR 9426 passes.
The existing test is low level as @catch points out, and ideally there would be a more complex test that instantiates various blocks in a more realistic way, and then checks the suggestions. In this way one could see that the suggestions for the standard block plugin case are the same as they were and no regression is introduced by the MR, and the extra field case is handled in the MR but wasn't previously. I'll try and work on this additional test, so for now leaving this issue in "needs work".
Comment #20
john.oltman commentedAdded tests and moving to "needs review". The TEST ONLY failed, as expected, while the MR passes. Portions of the new tests are modeled after the suggestions testing in the core layout_builder module.
Comment #21
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #22
smustgrave commentedFalse positive
Comment #23
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #25
tobiasbMR updated + missing type hints.
Comment #26
smustgrave commentedBelieve feedback on this one has been addressed.
Comment #27
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #28
john.oltman commented@smustgrave should the hooks in the test module be converted to the new Hook system as well - I can do that if needed
Comment #29
nicxvan commentedYes, they should be, but wow do you guys get an award for confusing module / hook names.
At first glance I thought block_content_theme_suggestions_test_entity_extra_field_info was theme suggestion until I saw the end.
Theme suggestions are not converted yet, but to be clear all three hooks in this module should be able to be converted.
Comment #30
nicxvan commentedComment #31
john.oltman commentedThe MR has been updated, moving hooks in the test module from procedural to OOP. Ready for review. As before, the "test only" pipeline fails with the error from the issue title, but the fix passes all tests.
Comment #32
smustgrave commentedRefactor for move to hooks seems good.
Comment #33
larowlanLeft some questions on the MR - thanks
Comment #34
john.oltman commentedAdded commits to address printing something about the block in the template and creating the block content in the test setup with a known UUID.
No, if anything, it fixes the same use case for inline blocks as it does for ExtraFieldBlock. However, there are no test cases in the layout builder module regarding theme suggestions for inline blocks specifically, so I can't prove it yet. Will add tests to cover that - one in layout builder for a base case and one in block content module for the specific case of an inline block referencing a block content entity.
Keeping in needs work.
Comment #36
acbramley commentedThink this is ready for a review again.
Comment #37
smustgrave commentedBelieve #34 is still needed no?
Comment #38
acbramley commentedFor theme suggestions for inline blocks? Is that in scope of this issue? That was a response to "Does this break Inline blocks?" Which I've manually confirmed it doesn't. I think if we want additional test coverage there it should be a separate issue?
Comment #39
smustgrave commentedThink I would agree with that. Not opening up the follow up unless we want the additional coverage.
Comment #40
sonfdHere's a code-only patch for use with composer and 10.5.
Comment #41
piotr pakulskipatch for 10.3.x
Comment #42
needs-review-queue-bot commentedThe Needs Review Queue Bot tested this issue. It no longer applies to Drupal core. Therefore, this issue status is now "Needs work".
This does not mean that the patch necessarily needs to be re-rolled or the MR rebased. Read the Issue Summary, the issue tags and the latest discussion here to determine what needs to be done.
Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.
Comment #43
acbramley commentedComment #44
nod_Comment #45
nod_Committed 5de749b and pushed to 11.x. Thanks!
Comment #47
nod_Comment #50
thefancywizard commentedadding a patch for 11.1.x since this has not made it into a release yet.
Comment #51
thefancywizard commentedComment #52
cilefen commentedComment #53
quietone commentedAsked the other release managers about the duplicate isssue, #3551116: strtr(): Passing null to parameter #1 ($string) of type string is deprecated in block_content_theme_suggestions_block_alter(). longwave said that this should be backported.
The diff here still applies to 11.1.x. Re-opening for am MR for Drupal 10.
Comment #54
quietone commentedThis needs an MR for Drupal 10.
Comment #55
acbramley commentedComment #57
acbramley commentedBackported https://git.drupalcode.org/project/drupal/-/commit/5de749b83a9ee2d2af0c1... to 10.6.x, swapping all OOP hooks in block_content_theme_suggestions_test to the procedural equivalent.
Comment #58
smustgrave commentedSeems like a good backport.
Comment #60
longwaveCommitted and pushed 9839653ed9a to 10.6.x. Thanks!
Comment #64
attraktive commentedHere is a patch that is 10.6 compatible :