Undefined array key "render element" in Drupal\Core\Theme\ThemeManager->render()

Created on 14 August 2023, about 1 year ago
Updated 3 April 2024, 7 months ago

Problem/Motivation

Found two warnings in dblog

Warning: Undefined array key "render element" in Drupal\Core\Theme\ThemeManager->render() (line 199 of /drupal-project/public_html/core/lib/Drupal/Core/Theme/ThemeManager.php) #0

Warning: Undefined array key "render element" in Drupal\Core\Theme\ThemeManager->render() (line 201 of drupal-project/public_html/core/lib/Drupal/Core/Theme/ThemeManager.php)

🐛 Bug report
Status

Closed: won't fix

Version

11.0 🔥

Component
Theme 

Last updated about 20 hours ago

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.

  • Issue created by @omar_emailat
  • 🇮🇳India narendraR Jaipur, India

    Did you face this issue in fresh Drupal install? If yes, please update steps to reproduce in issue.
    This might be possible due to some custom code also.

  • Status changed to Postponed: needs info about 1 year ago
  • 🇺🇸United States RustedBucket

    Currently battling this same issue. Trying to override a view template from a custom module.
    Basically I want to override the themes views_view_unformatted.html.twig template

    My custom template resides in modules/custom/custom_app/templates/views-view--on-campus-numbers.html.twig
    In my custom_app.module file I have the following:

    function custom_app_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
      if ($hook == 'views_view_unformatted') {
        $suggestions[] = 'views-view--on-campus-numbers';
      }
    }
    
    
    function custom_app_theme($existing, $type, $theme, $path) {
      return [
        'views_view__on_campus_numbers' => [
          'base_hook' => 'views_view_unformatted',
        ]
      ];
    }
    

    This throws 2 errors:

    Warning: Undefined array key "render element" in Drupal\Core\Theme\ThemeManager->render() (line 199 of core/lib/Drupal/Core/Theme/ThemeManager.php).
    Drupal\Core\Theme\ThemeManager->render('views_view__on_campus_numbers', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array) (Line: 449)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 238)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 583)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 239)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 187)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

    Warning: Undefined array key "render element" in Drupal\Core\Theme\ThemeManager->render() (line 201 of core/lib/Drupal/Core/Theme/ThemeManager.php).
    Drupal\Core\Theme\ThemeManager->render('views_view__on_campus_numbers', Array) (Line: 436)
    Drupal\Core\Render\Renderer->doRender(Array) (Line: 449)
    Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 204)
    Drupal\Core\Render\Renderer->render(Array, ) (Line: 238)
    Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 583)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 239)
    Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
    Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
    Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
    call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
    Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 187)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

    Adding 'render element' => 'elements' like this makes the error go away but no output from the view is rendered

        'views_view__on_campus_numbers' => [
          'render element' => 'elements',
          'base_hook' => 'views_view_unformatted',
        ]
    
  • 🇳🇱Netherlands florisg

    Thank you omar_emailat, this patch helped us,
    it's simply checking the `render element` exists.
    this works in our case at Drupal 9.5.11 and does not add much overhead.
    So we can deal with custom cleaning scheduled and unsurprised.

  • 🇺🇸United States drewcking

    Patch #2 works for me as well.

  • Status changed to Closed: won't fix 7 months ago
  • 🇪🇸Spain eduardo morales alberti Spain, 🇪🇺

    Patch #2 is not right because it mascarade the problem and it does not solve it.
    After some debugging, we saw that the theme function did not have the variables as array.
    Before:

    function mymodule_theme($existing, $type, $theme, $path) {
      return [
        'mymodule_test' => [
          'variables' => NULL,
        ],
      ];
    }
    

    ThemeManager.php sets the 'render element' with the same value as the variables but its expects an array.

        if (isset($variables['#theme']) || isset($variables['#theme_wrappers'])) {
          $element = $variables;
          $variables = [];
          if (isset($info['variables'])) {
            foreach (array_keys($info['variables']) as $name) {
              if (\array_key_exists("#$name", $element)) {
                $variables[$name] = $element["#$name"];
              }
            }
          }
          else {
            $variables[$info['render element']] = $element;
            // Give a hint to render engines to prevent infinite recursion.
            $variables[$info['render element']]['#render_children'] = TRUE;
          }
        }
    

    Solution:

    function mymodule_theme($existing, $type, $theme, $path) {
      return [
        'mymodule_test' => [
          'variables' => [],
        ],
      ];
    }
    
Production build 0.71.5 2024