Warning: Undefined array key "content" in template_preprocess_block() (line 251 of core/modules/block/block.module).

Created on 6 June 2023, over 1 year ago
Updated 17 June 2023, over 1 year ago

Problem/Motivation

When placing views blocks in layout builder, that rely on a content ID contextual filter, and which also don't render when empty, get this error

Warning: Undefined array key "content" in template_preprocess_block() (line 251 of core/modules/block/block.module). 

Steps to reproduce

Create a view with a block display, add a contextual filter "Content: ID", set Validation criteria "Content", add setting on view to hide the view if empty, place the view block using layout builder, select "Content" for "Content:ID" to pass the content ID of the page to the view block.
Visit a page where this block is placed, but where the view would not return results, and would therefore not display.
Error appears

Proposed resolution

Check that $variables['elements']['content'] is set before querying it

  if (isset($variables['elements']['content'])){
    $variables['content'] = $variables['elements']['content'];
  }
🐛 Bug report
Status

Needs work

Version

9.5

Component
Block 

Last updated 2 days ago

Created by

🇳🇿New Zealand roxflame

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024