Problem/Motivation

On a panelized node, if a block is empty in a region (ie. view block with 0 results), the Panels StandardDisplayBuilder class incorrectly sets cacheable metadata, resulting in multiple 'User error' messages that look like the following: User error: "max-age" is an invalid render array key

To reproduce on clean drupal install (8.1.10) w/ panels 8.x-3.0-beta5, panelizer 8.x-3.0-alpha2 (and all dependencies)

  1. Create a 'view block' with a filter that will return 0 results (ie. add filter for taxonomy term that no nodes are tagged for
  2. On the 'block' tab of the view, set 'Hide block if the view output is empty' to 'Yes'
  3. Place the view block you created with panelizer on a panelized node, and save it
  4. Clear cache, and then view the node with the empty block
  5. View DB log to see 'User error: "0" is an invalid render array key', 'User error: "1" is an invalid render array key', etc etc

Proposed resolution

Basically, in panels/src/Plugin/DisplayBuilder/StandardDisplayBuilder.php::136 there is a check for empty blocks, and if they are empty it attempts to set the #cache in the render array for an empty block. However, it incorrectly sets the $block_render_array var, and instead of updating the #cache element in the array, it creates sibling elements with the contents of $content['#cache'], leaving the initial element in $block_render_array blank. The code below is the culprit:

if (isset($content['#cache'])) {
  $block_render_array += $content['#cache'];
}

Should be:

if (isset($content['#cache'])) {
  $block_render_array['#cache'] += $content['#cache'];
}

Or, alternatively as I've handled in this patch:

$content_cache = $content['#cache'];
if (isset($content_cache)) {
  foreach ($content_cache as $key => $cache_item) {
    $block_render_array['#cache'][$key] = $cache_item;
  }
}

After applying the patch, the array is returned correctly and the 'User Errors' disappear. I'm not sure which approach of the above is preferred.

Remaining tasks

Review/feedback needed

User interface changes

None

API changes

None

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

trwill created an issue. See original summary.

trwill’s picture

Issue summary: View changes
dobrzyns’s picture

I can verify that the errors go away after applying the patch.

I'm not sure which of the approaches is preferred though.

trwill’s picture

Issue summary: View changes
trwill’s picture

I updated the steps to reproduce after realizing I added a lot complexity with contextual filters and that any 'empty' block (ie view block with 0 results) is enough to reproduce the errors.

lauriii’s picture