Notice: Undefined index: type in Drupal\Core\Render\Element\Date::processDate()

Created on 21 December 2020, about 4 years ago
Updated 13 November 2023, about 1 year ago

Problem/Motivation

If a form element of '#type' = 'date' has '#attributes' e.g. '#attributes' => ['placeholder' => 'yyyy-mm-dd'], a PHP notice is raised:
Notice: Undefined index: type in Drupal\Core\Render\Element\Date::processDate() (line 65 of /var/www/html/core/lib/Drupal/Core/Render/Element/Date.php)

Steps to reproduce

Create date elements in a form as follows:

    $form['date1'] = array(
      '#type' => 'date',
      '#title' => t('Date'),
    );
    $form['date2'] = array(
      '#type' => 'date',
      '#attributes' => array('placeholder' => 'yyyy-mm-dd'),
      '#title' => t('Date placeholder'),
    );

The undefined index notice is raised for date2, but not date1.

Proposed resolution

Change line 65 of core/lib/Drupal/Core/Render/Element/Date.php from:
if ($element['#attributes']['type'] == 'date' && !empty($element['#date_date_format'])) {
to:
if (isset($element['#attributes']['type']) && $element['#attributes']['type'] == 'date' && !empty($element['#date_date_format'])) {

This prevents the Notice being raised, but I'm not sure if $element['#attributes']['type'] should have been overwritten by lines 1028-1031 in core/lib/Drupal/Core/Form/FormBuilder.php

      if (isset($element[$key]['#type']) && empty($element[$key]['#defaults_loaded']) && ($info = $this->elementInfo->getInfo($element[$key]['#type']))) {
        $element[$key] += $info;
        $element[$key]['#defaults_loaded'] = TRUE;
      }

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

๐Ÿ› Bug report
Status

Closed: outdated

Version

11.0 ๐Ÿ”ฅ

Component
Renderย  โ†’

Last updated 6 days ago

Created by

๐Ÿ‡ฆ๐Ÿ‡บAustralia apiddington

Live updates comments and jobs are added and updated live.

Missing content requested by

๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi
about 1 year ago
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024