Fix preprocess function to prevent missing array keys.

Created on 29 January 2025, 4 months ago
Updated 14 April 2025, about 1 month ago

Problem/Motivation

When using the Cookies Addons Views module, a PHP warning occurs during the rendering of a block view. This happens because the theme_hook_original key is missing in the $variables array, leading to an error in the Twig rendering process.

Error Message

Warning: Undefined array key "theme_hook_original" in twig_render_template() 
(line 62 in /core/themes/engines/twig/twig.engine)

Cause of the Issue

The issue is caused by an incorrect initialization of the $variables array in the cookies_addons_views.module file:

$variables = [];

This removes all pre-existing values, including theme_hook_original, which is required by Drupal's theme system. When Twig attempts to access this missing key, a warning is triggered.

Steps to reproduce

  1. Enable the Cookies Addons Views module.
  2. Create a View and place it as a block (e.g., map|block_1|open_street_maps).
  3. Load the page where the block is displayed.
  4. Observe that the map is blocked correctly, but PHP warnings appear in the logs.

Proposed resolution

To fix this issue, the $variables array should be initialized with a default value that includes theme_hook_original:

$variables = ['theme_hook_original' => ''];

This ensures the required key is present and prevents warnings.

Remaining tasks

  • Apply the fix in cookies_addons_views.module.
  • Test with different view configurations to confirm the issue is resolved.
  • Ensure no new warnings appear in the logs.

User interface changes

None.

API changes

None.

Data model changes

None.

🐛 Bug report
Status

Active

Version

1.2

Component

Code

Created by

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • After examining the code more carefully, I've identified the exact cause of the issue and implemented a complete solution.
    The problem occurs in cookies_addons_views_preprocess_views_view() where we completely reset the $variables array:

    $variables = [];
    

    This operation wipes out all existing values, including the critical theme_hook_original key that Drupal's theme system needs. This is what causes the warning.
    My solution preserves and restores this important value:

    $theme_hook_original = $variables['theme_hook_original'] ?? '';
    
    $variables['theme_hook_original'] = $theme_hook_original;
    

    This approach:

    • Saves the theme hook information before it's lost
    • Allows us to still reset the variables array as needed
    • Ensures the theme system has the information it needs at render time
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024