Problem/Motivation
Date field provides a dropdown to select the default value to display in entity form. Currently, only allows to leave the value empty or set the current date. Adding an option to set the default value as a relative date entered in a textfield. I know that core Date field provides a simple field and more advanced features should be added by contrib modules. In an IRC conversation with @Berdir, we thought that adding this feature would be easy to implement and the improvement in module's UX would be interesting.
Proposed resolution
Add a new option (Relative date) in the default value dropdown that allows to enter a valid relative date to use as Date field default value.
Remaining tasks
Implement patch
User interface changes
New option in the default value option dropdown and a textfield that will be displayed in case that option is selected.
API changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | interdiff.txt | 1.43 KB | plopesc |
| #12 | relative_date-2302047-12.patch | 11.2 KB | plopesc |
| #11 | interdiff.txt | 3.88 KB | plopesc |
| #11 | relative_date-2302047-10.patch | 11.21 KB | plopesc |
| #8 | relative_date-2302047-8.patch | 10.19 KB | plopesc |
Comments
Comment #1
plopescHello,

attaching patch that give support for relative default dates, here is a screenshot of the changes introduced with this patch
Including test coverage for this feature.
Regards.
Comment #2
penyaskitoLooks a nice improvement, but needs reroll.
Comment #3
penyaskitoComment #4
plopescPatch re-rolled using
FormStateInterfaceinstead of array for$form_state.Regards.
Comment #5
plopescLet's go testbot
Comment #6
penyaskitoCode looks good to me.
Tested it on simplytest.me by editing the article content type, adding a field field_date with relative date, and default to "now". Default date when creating an article is today.
Edited the field, set the default to "+1 month". When creating an article, the default it is correctly set to one month from now.
Comment #7
alexpottAre you sure that in all cases this will generate an invalid relative date? This does not look like a good usage of random string.
Comment #8
plopescHello
New patch replacing
randomString()by'invalid date'string.Regards.
Comment #9
swentel commentedMaybe use 'Current date' and 'Relative date' ?
I'm not sure strtotime needs to be translated.
Comment says 'today' but the value is +90 days ?
Comment #10
swentel commentedAlso, the schema probably needs to be updated as well no ?
Comment #11
plopescPatch addressing comments on #9 and #10.
Regards.
Comment #12
plopescmore nitpicks
Comment #13
swentel commentedGreat, looks good to me now!
Comment #15
alexpottCommitted 4192e7d and pushed to 8.0.x. Thanks!
Comment #18
splash112 commentedRelative dates work very well, many thanks! There seems to be some issues when time is involved.
Please have a look at the issue below:
https://www.drupal.org/project/drupal/issues/3169876