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

Issue fork drupal-3468180

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:

Comments

john.oltman created an issue. See original summary.

john.oltman’s picture

StatusFileSize
new1.52 KB

The attached patch currently applies to the 11.0.x and 10.3.x and 10.4.x branches.

cilefen’s picture

Version: 10.3.x-dev » 11.x-dev
Status: Active » Needs work
Issue tags: +Needs merge request

john.oltman’s picture

StatusFileSize
new1.78 KB

The 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.

john.oltman’s picture

Issue summary: View changes
Status: Needs work » Needs review
Issue tags: -Needs merge request
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Possible to get test coverage around this issue?

john.oltman’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests

Added a test. The test passes with the MR and fails when run using the "test only" pipeline, as desired.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Made 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.

john.oltman’s picture

Makes sense, thank you.

catch’s picture

Status: Reviewed & tested by the community » Needs work

The steps to reproduce say:

Add a field block or extra field block to a layout builder enabled page. Then view the page.

But the function does this:

function block_content_theme_suggestions_block_alter(array &$suggestions, array $variables) {
  $suggestions_new = [];
  $content = $variables['elements']['content'];

  $block_content = $variables['elements']['content']['#block_content'] ?? NULL;

  if ($block_content instanceof BlockContentInterface) {

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.

john.oltman’s picture

Issue summary: View changes
Status: Needs work » Needs review

Thanks 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 calls ExtraFieldBlock::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.

sonfd made their first commit to this issue’s fork.

sonfd’s picture

I 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'].

sonfd’s picture

Hide patches since we are now using MRs.

smustgrave’s picture

Status: Needs review » Needs work

There are now 2 MRs up with different approaches and 1 not having test coverage.

john.oltman’s picture

Issue summary: View changes

I 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".

john.oltman’s picture

Status: Needs work » Needs review

Added 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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new4.15 KB

The 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.

smustgrave’s picture

Status: Needs work » Needs review

False positive

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new4.16 KB

The 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.

tobiasb made their first commit to this issue’s fork.

tobiasb’s picture

Status: Needs work » Needs review

MR updated + missing type hints.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Believe feedback on this one has been addressed.

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new90 bytes

The 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.

john.oltman’s picture

Status: Needs work » Needs review

@smustgrave should the hooks in the test module be converted to the new Hook system as well - I can do that if needed

nicxvan’s picture

Yes, 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.

nicxvan’s picture

Status: Needs review » Needs work
john.oltman’s picture

Status: Needs work » Needs review

The 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.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Refactor for move to hooks seems good.

larowlan’s picture

Status: Reviewed & tested by the community » Needs work

Left some questions on the MR - thanks

john.oltman’s picture

Added commits to address printing something about the block in the template and creating the block content in the test setup with a known UUID.

Does this break theme suggestions for inline blocks? (see InlineBlock)

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.

acbramley made their first commit to this issue’s fork.

acbramley’s picture

Status: Needs work » Needs review

Think this is ready for a review again.

smustgrave’s picture

Status: Needs review » Needs work

Believe #34 is still needed no?

acbramley’s picture

Status: Needs work » Needs review

For 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?

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Think I would agree with that. Not opening up the follow up unless we want the additional coverage.

sonfd’s picture

StatusFileSize
new785 bytes

Here's a code-only patch for use with composer and 10.5.

piotr pakulski’s picture

StatusFileSize
new842 bytes

patch for 10.3.x

needs-review-queue-bot’s picture

Status: Reviewed & tested by the community » Needs work
StatusFileSize
new92 bytes

The 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.

acbramley’s picture

Status: Needs work » Reviewed & tested by the community
nod_’s picture

Issue tags: +no-needs-review-bot
nod_’s picture

Status: Reviewed & tested by the community » Fixed

Committed 5de749b and pushed to 11.x. Thanks!

  • nod_ committed 5de749b8 on 11.x
    Issue #3468180 by john.oltman, sonfd, acbramley, smustgrave, tobiasb,...
nod_’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

thefancywizard’s picture

StatusFileSize
new1006 bytes

adding a patch for 11.1.x since this has not made it into a release yet.

thefancywizard’s picture

quietone’s picture

Issue summary: View changes
Status: Closed (fixed) » Needs work

Asked 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.

quietone’s picture

Status: Needs work » Patch (to be ported)

This needs an MR for Drupal 10.

acbramley’s picture

Version: 11.x-dev » 10.6.x-dev

acbramley’s picture

Status: Patch (to be ported) » Needs review

Backported 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.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community

Seems like a good backport.

  • longwave committed 9839653e on 10.6.x
    fix: #3468180 Undefined array key "view_mode" in...
longwave’s picture

Status: Reviewed & tested by the community » Fixed

Committed and pushed 9839653ed9a to 10.6.x. Thanks!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

attraktive’s picture

Here is a patch that is 10.6 compatible :

diff --git a/core/modules/block_content/block_content.module b/core/modules/block_content/block_content.module
--- a/core/modules/block_content/block_content.module
+++ b/core/modules/block_content/block_content.module	(date 1779354874139)
@@ -192,21 +192,25 @@
  */
 function block_content_theme_suggestions_block_alter(array &$suggestions, array $variables) {
   $suggestions_new = [];
-  $content = $variables['elements']['content'];
+  $content = $variables['elements']['content'] ?? [];

-  $block_content = $variables['elements']['content']['#block_content'] ?? NULL;
+  $block_content = $content['#block_content'] ?? NULL;

   if ($block_content instanceof BlockContentInterface) {
-    $bundle = $content['#block_content']->bundle();
-    $view_mode = strtr($variables['elements']['content']['#view_mode'], '.', '_');
-
-    $suggestions_new[] = 'block__block_content__view__' . $view_mode;
+    $bundle = $block_content->bundle();
     $suggestions_new[] = 'block__block_content__type__' . $bundle;
-    $suggestions_new[] = 'block__block_content__view_type__' . $bundle . '__' . $view_mode;
+
+    $view_mode = strtr($content['#view_mode'] ?? '', '.', '_');
+    if ($view_mode) {
+      $suggestions_new[] = 'block__block_content__view__' . $view_mode;
+      $suggestions_new[] = 'block__block_content__view_type__' . $bundle . '__' . $view_mode;
+    }

     if (!empty($variables['elements']['#id'])) {
       $suggestions_new[] = 'block__block_content__id__' . $variables['elements']['#id'];
-      $suggestions_new[] = 'block__block_content__id_view__' . $variables['elements']['#id'] . '__' . $view_mode;
+      if ($view_mode) {
+        $suggestions_new[] = 'block__block_content__id_view__' . $variables['elements']['#id'] . '__' . $view_mode;
+      }
     }

     // Remove duplicate block__block_content.