On line 65 of core\lib\Drupal\Core\Render\Element\Date.php
I found same issue in function processDate and exactly on line :
if ($element['#attributes']['type'] == 'date' && !empty($element['#date_date_format'])) {
When use custom form with this field bellow :
$form['field_date'] = [
'#type' => 'date',
'#title' => t('Field date'),
'#date_date_format' => 'Y-m-d',
'#required' => TRUE,
'#attributes' => [
'min' => date('Y-m-d', $date_expire),
'max' => date('Y-m-d', strtotime('+2 years', $date_expire)),
],
'#default_value' => $date_value,
];
you will get this error : Undefined index: type
and dump element field date is :
^ array:25 [▼
"#type" => "date"
"#title" => Drupal\Core\StringTranslation\TranslatableMarkup {#8975 ▶}
"#date_date_format" => "Y-m-d"
"#required" => true
"#attributes" => array:3 [▼
"min" => "2020-07-30"
"max" => "2022-07-30"
"data-drupal-selector" => "edit-date-start"
]
"#default_value" => "2020-07-30"
"#input" => true
"#theme" => "input__date"
"#process" => array:2 [▶]
"#pre_render" => array:1 [▶]
"#theme_wrappers" => array:1 [▶]
"#value_callback" => array:2 [▶]
"#defaults_loaded" => true
"#tree" => false
"#parents" => array:1 [▶]
"#array_parents" => array:1 [▶]
"#weight" => 0.006
"#processed" => false
"#title_display" => "before"
"#description_display" => "after"
"#errors" => null
"#id" => "edit-date-start"
"#name" => "date_start"
"#value" => "2020-07-30"
"#ajax_processed" => false
Comments
Comment #2
vlklavanya commentedComment #3
vlklavanya commented@kamalMaroc here is the patch to fix the error.
On the other hand you can also set type to date in #attributes to fix the error. Here the code should look like.
Comment #4
malaynayak commented+1 for patch #3
Comment #5
kamalMaroc commented@vlklavanya that s exactly what i do on custom form to fixe this small issue
however i cannot add this patch until validate support drupal team :)
thank you for answer.
Comment #6
xjmThe issue summary is hard to understand. I retitled the issue based on what I understand from the patch.
Is there a reason that it's not correct to assume
$element['#attributes']['type']is set? What's a valid reason for it to not be?Alternately, should the code be checking
$element['#type']instead?Finally, this issue needs an automated test triggering the bug, so that we can validate our fix.
Comment #13
acbramley commented::processDate was removed entirely in https://git.drupalcode.org/project/drupal/-/commit/2fc40e8642c95abff47e8...
closing this as outdated as it doesn't look like this error exists in this class anymore, the other pre render callback in the element class sets
$element['#attributes']['type'] = 'date';if it's not set.