I'm sorry if this has been reported or if it is not a bug. Possibly it is a config issue, but D8 documentation is killing me. I have seen numerous conflicting posts on how to set this and none seem to work - so guessing this is a bug.
I have a custom form with a field of type: date.
I have tried various methods to set a default value but none seem to work.
In the date form code i set:
$form['date'] = [
'#type' => 'date',
'#description' => $this->t('Click in box to select date.'),
'#default_value' => DrupalDateTime::createFromTimestamp(strtotime('2018-02-15')),
];
but, in the html this appears to give garbage:
<input type="date" data-drupal-selector="edit-date" aria-describedby="edit-date--description" data-drupal-date-format="Y-m-d" id="edit-date" name="date" 2018-02-15 00:00:00 America/Toronto class="form-date" />
i.e. the time string is outside any attr.
In the browser, for FF it shows a blank textfield and when i open the popup is shows default date as today (not the default date). In Chrome, it shows mm/dd/yyyy in the textfield and also shows today as default in the popup.
I have also tried setting in a form_alter like this:
$form['date']['widget'][0]['value']['#default_value'] = DrupalDateTime::createFromTimestamp(strtotime($date));
and this also does not display correctly.
Oddly enough, I have a custom entity with a date field and it does work and display correctly. In the field.storage yml file for that field i see this:
type: datetime
settings:
datetime_type: date
Going from this i change my custom form to use type = datetime and sure enough that field does work as expected; except of course it now has the time selector part.
This leads me to think a couple things:
- there is a form #attr that can be set for a datetime field to disable the time portion (where is form element documentation?)
- setting form type as date does not work correctly (bug)
Am I the only one that misses the Forms API documentation that existed for D4, D5, D6 and D7 where it listed how to use form elements? Why does this not exist for D8? You can't seriously tell me these pages are supposed to replace this for D8:
- https://api.drupal.org/api/drupal/core%21core.api.php/group/form_api/8.4.x
- https://api.drupal.org/api/drupal/elements/8.4.x
- https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Datetime%...
| Comment | File | Size | Author |
|---|---|---|---|
| #10 | 2944552-10.patch | 913 bytes | mpdonadio |
| #6 | 2944552-6.patch | 966 bytes | bhanuprakashnani |
Comments
Comment #2
tacituseu commentedDoc says:
But this worked for me too (for
'#type' => 'date'):'#default_value' => '2018-02-15'.Comment #3
liquidcms commentedHmm.. perhaps i've been staring at the screen too long.. lol
In form code it works as you say (which i was sure i had tried multiple times).
but doesn't default to anything if not set, so I am using this now:
where $date is in format 'Y-m-d'.
Comment #4
mpdonadioI think we need some (1) test coverage around the Datetime and Date render elements for this (2) adjust the docs to match what really is accepted and happens. There is too much confusion about how this works.
Comment #5
bhanuprakashnani commentedThe code shown here is from the DateTimeFieldTest.php file in DateTime module?. Can I try creating a patch by changing the default value as discussed in the above comments by setting the default value to form['date']['#default_value'] = $date ? $date : date('Y-m-d');? Please let me know if the way I am going is right or not. Thank you.
Comment #6
bhanuprakashnani commentedI have tried to make a change in the default value. Please review whether it's correct or not. If its wrong please let me know what is to be done. Please guide me. Thank you.
Comment #8
sophie.skThis is still an issue on latst 8.6 so updating the version.
The docs quite clearly state than an array of values is the right way to set the default value:
But when I didn't provide a default value, the field defaulted to
dd / mm / yyyy. When I provided a default value as an array, the field defaulted again todd / mm / yyyy.Only when I set
$field['#default_value'] = date('Y-m-d');did it start showing the correct date.Comment #9
mpdonadioLittle housekeeping.
Comment #10
mpdonadioHere is a doc fix.
Comment #11
mpdonadioComment #13
akalata commentedComment #14
alexpottYep this documentation is correct - regardless of the other render setting
#date_date_formatthe expected date format isY-m-dfor the default value. What is also interesting is that for modern browsers#date_date_formatsetting seems meaningless because I think it is only used for a legacy stuff in date.es6.js - maybe we can file a follow up to address that because the current document about it in \Drupal\Core\Render\Element\Date seems misleading.Committed and pushed dee0eb2880 to 8.8.x and 2adbbefe1f to 8.7.x. Thanks!
backported to 8.7.x as a docs fix.
Comment #18
idebr commentedComment #19
idebr commented#14 There was an earlier issue regarding the Date form element document being incomplete. I have repurposed it to update the documentation regarding its other than #default_value. This issue is available at #2836530: Update Date element documentation for #date_date_format and usage for other date types such as time