Undefined array key "element" in template_preprocess_input warning

Created on 5 August 2024, 3 months ago
Updated 19 September 2024, about 2 months ago

Problem/Motivation

Hello project maintainers,

I got warnings in the logs.

Warning: Undefined array key "element" in template_preprocess_input() (line 345 of /var/www/docroot/core/includes/form.inc)
#0 /var/www/docroot/core/includes/bootstrap.inc(164): _drupal_error_handler_real(2, 'Undefined array...', '/var/www/docroo...', 345)
#1 /var/www/docroot/core/includes/form.inc(345): _drupal_error_handler(2, 'Undefined array...', '/var/www/docroo...', 345)
#2 [internal function]: template_preprocess_input(Array, 'input__tree', Array)
Warning: Trying to access array offset on value of type null in template_preprocess_input() (line 350 of /var/www/docroot/core/includes/form.inc)
#0 /var/www/docroot/core/includes/bootstrap.inc(164): _drupal_error_handler_real(2, 'Trying to acces...', '/var/www/docroo...', 350)
#1 /var/www/docroot/core/includes/form.inc(350): _drupal_error_handler(2, 'Trying to acces...', '/var/www/docroo...', 350)
#2 [internal function]: template_preprocess_input(Array, 'input__tree', Array)

and next one (related to the previos one).

I did investifate it and see that it happens in form.inc (core/includes/form.inc)

function template_preprocess_input(&$variables) {
  $element = $variables['element'];
  // Remove name attribute if empty, for W3C compliance.
  if (isset($variables['attributes']['name']) && empty((string) $variables['attributes']['name'])) {
    unset($variables['attributes']['name']);
  }
  $variables['children'] = $element['#children'];
}

It happens because array keys 'element' and '#children' wasnt created.

Steps to reproduce

Core version 10.2.7
PHP 8.1.14
Web Apache/2.4.53 (Unix) OpenSSL/1.1.1n

1. Enable warnings visible (by default it enabled).
2. Install module. Version 1.0.0 or 1.0.1.
3. Copy template input--tree.html.twig to the theme and add changes (in my case I added title attr into 'label' + added trim for long menu item title)
5. Open node create/edit form.
5. Check internal drupal logs.

Proposed resolution

I can fix it via hard way and patch form.inc file
like this one

function template_preprocess_input(&$variables) {
  $element = $variables['element'] ?? [];
  // Remove name attribute if empty, for W3C compliance.
  if (isset($variables['attributes']['name']) && empty((string) $variables['attributes']['name'])) {
    unset($variables['attributes']['name']);
  }
  $variables['children'] = $element['#children'] ?? [];
}

But seems need to find the way to fix in on module level.

Second way
Change menu_tree_theme function from

function menu_tree_theme($existing, $type, $theme, $path) {
  $module_path = \Drupal::service('extension.list.module')->getPath('menu_tree');
  return [
    'input__tree' => [
      'variables' => [
        'id' => NULL,
        'menus' => NULL,
        'exclude' => NULL,
      ],
      'template' => 'input--tree',
      'path' => $module_path . '/templates',
    ],
  ];
}

to

function menu_tree_theme($existing, $type, $theme, $path) {
  $module_path = \Drupal::service('extension.list.module')->getPath('menu_tree');
  return [
    'input__tree' => [
      'variables' => [
        'id' => NULL,
        'menus' => NULL,
        'exclude' => NULL,
        'element' => [
          '#children' => [],
        ],
      ],
      'template' => 'input--tree',
      'path' => $module_path . '/templates',
    ],
  ];
}

In shoer, I added missed default variables into theme hook.

Any ideas are welcome. Thanks!

🐛 Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

🇺🇦Ukraine t1mm1 Kiev

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

Comments & Activities

Production build 0.71.5 2024