Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
Datetime element does not support string #default_value.
Ex.:
$form['datetime'] = [
'#type' => 'datetime',
'#default_value' => date('Y-m-d H:i:s'),
];
The date and time value is not set by default, while it does with date element for instance:
$form['date'] = [
'#type' => 'date',
'#default_value' => date('Y-m-d'),
];
NOTE: DrupalDateTime() can be given as default value though.
Comment | File | Size | Author |
---|---|---|---|
#3 | support_string_default_value_in_datetime--2783623-3.patch | 2.07 KB | Dom. |
Comments
Comment #2
Dom. CreditAttribution: Dom. as a volunteer and at ACINO commentedAfter deeper code reading, it seems that #default_value can be added as a DrupalDateTime. This means for DX to first convert his wanted value to a DrupalDateTime before setting #default_value.
Thus issue is not actually a bug but more a new feature. So I updated issue summary consequently.
Comment #3
Dom. CreditAttribution: Dom. as a volunteer and at ACINO commentedThe following patch tries to convert the string via user setted date_format and time_format to apply it to #default_value (jsut as it was with #value)
Comment #6
jhedstromThis would need some tests.