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

Comments

plopesc’s picture

Status: Active » Needs review
StatusFileSize
new32.13 KB
new10.03 KB

Hello,
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.

penyaskito’s picture

Issue tags: +Needs reroll

Looks a nice improvement, but needs reroll.

penyaskito’s picture

Status: Needs review » Needs work
plopesc’s picture

Issue tags: -Needs reroll
StatusFileSize
new10.2 KB

Patch re-rolled using FormStateInterface instead of array for $form_state.

Regards.

plopesc’s picture

Status: Needs work » Needs review

Let's go testbot

penyaskito’s picture

Status: Needs review » Reviewed & tested by the community

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

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
+++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php
@@ -325,15 +326,49 @@ function testDefaultValue() {
+      'default_value_input[default_date]' => $this->randomString(),

Are you sure that in all cases this will generate an invalid relative date? This does not look like a good usage of random string.

plopesc’s picture

Status: Needs work » Needs review
StatusFileSize
new10.19 KB
new780 bytes

Hello

New patch replacing randomString() by 'invalid date' string.

Regards.

swentel’s picture

  1. +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeFieldItemList.php
    @@ -32,13 +37,27 @@ public function defaultValuesForm(array &$form, FormStateInterface $form_state)
    +            static::DEFAULT_VALUE_NOW => t('The current date'),
    +            static::DEFAULT_VALUE_CUSTOM => t('Relative value'),
    

    Maybe use 'Current date' and 'Relative date' ?

  2. +++ b/core/modules/datetime/src/Plugin/Field/FieldType/DateTimeFieldItemList.php
    @@ -32,13 +37,27 @@ public function defaultValuesForm(array &$form, FormStateInterface $form_state)
    +          '#description' => t("Describe a time by reference to the current day, like '+90 days' (90 days from the day the field is created) or '+1 Saturday' (the next Saturday). See !strtotime for more details.", array('!strtotime' => l(t('strtotime'), 'http://www.php.net/manual/en/function.strtotime.php'))),
    

    I'm not sure strtotime needs to be translated.

  3. +++ b/core/modules/datetime/src/Tests/DateTimeFieldTest.php
    @@ -325,15 +326,49 @@ function testDefaultValue() {
    +    // Create a new node to check that datetime field default value is today.
    +    $new_node = entity_create('node', array('type' => 'date_content'));
    +    $expected_date = new DrupalDateTime('+90 days', DATETIME_STORAGE_TIMEZONE);
    +    $this->assertEqual($new_node->get($field_storage->name)->offsetGet(0)->value, $expected_date->format(DATETIME_DATE_STORAGE_FORMAT));
    

    Comment says 'today' but the value is +90 days ?

swentel’s picture

Also, the schema probably needs to be updated as well no ?

plopesc’s picture

StatusFileSize
new11.21 KB
new3.88 KB

Patch addressing comments on #9 and #10.

Regards.

plopesc’s picture

StatusFileSize
new11.2 KB
new1.43 KB

more nitpicks

swentel’s picture

Status: Needs review » Reviewed & tested by the community

Great, looks good to me now!

The last submitted patch, 11: relative_date-2302047-10.patch, failed testing.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed 4192e7d and pushed to 8.0.x. Thanks!

  • alexpott committed 4192e7d on 8.0.x
    Issue #2302047 by plopesc: Added support for relative default dates in...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

splash112’s picture

Relative 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