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
CommentFileSizeAuthor
#3 3119371-3.patch914 bytesvlklavanya

Comments

kamalMaroc created an issue. See original summary.

vlklavanya’s picture

Assigned: Unassigned » vlklavanya
vlklavanya’s picture

Assigned: vlklavanya » Unassigned
Status: Active » Needs review
StatusFileSize
new914 bytes

@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.

$form['field_date'] = [
      '#type' => 'date',
      '#title' => t('Field date'),
      '#date_date_format' => 'Y-m-d',
      '#required' => TRUE,
      '#attributes' => [
        'type' => 'date',
        'min' => date('Y-m-d', $date_expire),
        'max' => date('Y-m-d', strtotime('+2 years', $date_expire)),
      ],
      '#default_value' => $date_value,
];
malaynayak’s picture

Status: Needs review » Reviewed & tested by the community

+1 for patch #3

kamalMaroc’s picture

@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.

xjm’s picture

Title: Form Field Date » The Date element's processDate() method assumes $element['#attributes']['type'] is set, but it may not be
Priority: Major » Normal
Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs tests

The 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.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

acbramley’s picture

Status: Needs work » Closed (outdated)
Issue tags: +Bug Smash Initiative

::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.

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.