Problem/Motivation

When you add a datelist field, the Years available range from 1900 to 2050.

For many uses, that's unnecessarily broad or not broad enough.

It'd be good to be able to configure the range in the manage form display interface.

E.g. to +/- 3 years, or min 2010 and max 2020, etc.

I think Drupal 7 had this feature.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Issue fork drupal-2836054

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Leo Pitt created an issue. See original summary.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

larowlan’s picture

Assigned: Unassigned » larowlan

giddyup

larowlan’s picture

Status: Active » Needs review
StatusFileSize
new10.82 KB

This works for me

jibran’s picture

Status: Needs review » Reviewed & tested by the community

This is an experimental module so no need for the upgrade path and upgrade path tests.

jhedstrom’s picture

Status: Reviewed & tested by the community » Needs review

A few questions/comments here.

Why is this only specific to the daterange field? Both normal datetime and date range fields support select list entry. Also, this is currently applied as a setting regardless of the form widget selected. It is only applicable to select list entry I think (unless it is meant to validate/constrain dates between those years, in which case it needs work since that bit isn't working...).

mpdonadio’s picture

Also, we already have a Datelist form element, which that widget is using. Can't we just expose the setting and then stuff it into #date_year_range key in DateRangeDatelistWidget::formElement() and DateTimeDatelistWidget::formElement().

 /*
   *   - #date_year_range: A description of the range of years to allow, like
   *     '1900:2050', '-3:+3' or '2000:+3', where the first value describes the
   *     earliest year and the second the latest year in the range. A year
   *     in either position means that specific year. A +/- value describes a
   *     dynamic value that is that many years earlier or later than the current
   *     year at the time the form is displayed. Defaults to '1900:2050'.
   */

Since PluginSettingsBase::getSetting() merges in defaults, I don't think either would need an upgrade path and upgrade path tests, just a cache clear.

larowlan’s picture

Also, we already have a Datelist form element, which that widget is using. Can't we just expose the setting and then stuff it into #date_year_range key in DateRangeDatelistWidget::formElement()

That's what the patch does, the YearRange form element is just for the settings form, borrowed from the approach in date_api in D7.

I agree, this can go up one layer further to the single date field, but that'd be in the non-experimental module.

jibran’s picture

Status: Needs review » Needs work

> I agree, this can go up one layer further to the single date field, but that'd be in the non-experimental module.
Let's do that. I believe we can add new features to any module in a minor release.

mpdonadio’s picture

Issue tags: +Usability

I just re-read this. Haven't played with it applied, but I like the better UX of the form element approach rather than a simple text field.

And yeah, this can go into the datetime version, too. As long as the datetime version doesn't depend on the daterange one, they this is a valid new feature for 8.4.x: it adds a new admin feature to an existing form in a non-disruptive way. A select with 150 things in it is a UX WTF anyway.

  1. +++ b/core/modules/datetime_range/src/Element/YearRange.php
    @@ -0,0 +1,104 @@
    +    if (!self::rangeIsValid($year_range)) {
    +      $form_state->setError($element['years_back'], t('Starting year must be in the format -9, or an absolute year such as 1980.'));
    

    static

  2. +++ b/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php
    @@ -164,4 +165,34 @@ protected function createDefaultValue($date, $timezone) {
    +  public static function defaultSettings() {
    +    return parent::defaultSettings() + [
    +        'year_range' => '1900:2050',
    +      ];
    +  }
    +
    

    I don't think the setting should be on the base class, just the list widget.

  3. +++ b/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeWidgetBase.php
    @@ -164,4 +165,34 @@ protected function createDefaultValue($date, $timezone) {
    +    return parent::defaultSettings() + [
    +        'year_range' => '1900:2050',
    +      ];
    +  }
    

    Extra indent.

  4. +++ b/core/modules/datetime_range/tests/src/Functional/DateRangeYearRangeTest.php
    @@ -0,0 +1,123 @@
    +    $storage = FieldStorageConfig::create([
    +      'entity_type' => 'entity_test',
    +      'field_name' => 'range',
    +      'id' => 'entity_test.range',
    +      'type' => 'daterange',
    +      'settings' => [
    +        'target_type' => 'entity_test',
    +      ],
    +    ]);
    +    $storage->save();
    +    $config = FieldConfig::create([
    +      'field_name' => 'range',
    +      'entity_type' => 'entity_test',
    +      'bundle' => 'entity_test',
    +      'id' => 'entity_test.entity_test.range',
    +      'label' => 'Range',
    +    ]);
    +    $config->save();
    

    We can just use the ->save() w/o an intermediate local variable.

  5. +++ b/core/modules/datetime_range/tests/src/Functional/DateRangeYearRangeTest.php
    @@ -0,0 +1,123 @@
    +      ->setComponent('range', array(
    +        'type' => 'daterange_datelist',
    +        'weight' => 10,
    +      ))
    

    Can use [].

Looks like there is no coverage of the new form element on the manage display or did I miss something?

jibran’s picture

Issue tags: +Needs reroll
Munavijayalakshmi’s picture

Munavijayalakshmi’s picture

Assigned: Munavijayalakshmi » Unassigned
Status: Needs work » Needs review
StatusFileSize
new10.8 KB

Rerolled the patch.

jofitz’s picture

Issue tags: -Needs reroll
StatusFileSize
new1.82 KB
new10.72 KB

Fixed minor coding standards issues.
Removed Needs Reroll tag.

mpdonadio’s picture

Status: Needs review » Needs work

Doesn't look like #10 has been addressed.

jofitz’s picture

StatusFileSize
new3.69 KB
new10.78 KB

Sorry, my mistake. I didn't read properly and assumed they had all been address in #13.

  1. Changed self:: to static::
  2. Moved defaultSettings() to DateRangeDatelistWidget
  3. Resolved as part of 2.
  4. Removed intermediate variables
  5. Corrected as part of the re-roll in #13
jofitz’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 16: 2836054-16.patch, failed testing.

jofitz’s picture

Status: Needs work » Needs review
StatusFileSize
new698 bytes
new11.46 KB

Add defaultSettings() to DateRangeDefaultWidget too.

mpdonadio’s picture

Status: Needs review » Needs work

Did some manual testing.

Currently doesn't work on datetime fields. Let's make it work there, too.

Like the way this works on daterange. Big UX win.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jofitz’s picture

Status: Needs work » Needs review
StatusFileSize
new11.47 KB

Patch in #19 no longer applies cleanly. Re-rolled.

jofitz’s picture

StatusFileSize
new5.22 KB
new13.82 KB

Added to datetime fields in the same way it is on daterange fields.

Status: Needs review » Needs work

The last submitted patch, 23: 2836054-23.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

mpdonadio’s picture

  1. +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeDatelistWidget.php
    @@ -26,7 +26,9 @@ public static function defaultSettings() {
    -    ] + parent::defaultSettings();
    +    ] + parent::defaultSettings() + [
    +      'year_range' => '1900:2050',
    +    ];
    

    I think we need to move this to DateTimeWidgetBase.

  2. +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeDatelistWidget.php
    @@ -138,7 +140,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
    -    $summary = [];
    +    $summary = parent::settingsSummary();
    

    Need to look at this applied, but this looks like it could be out of scope? But if we do the #1 bit, then this is probably needed.

  3. +++ b/core/modules/datetime/src/Plugin/Field/FieldWidget/DateTimeDefaultWidget.php
    @@ -98,4 +98,13 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
    +  /**
    +   * {@inheritdoc}
    +   */
    +  public static function defaultSettings() {
    +    return parent::defaultSettings() + [
    +      'year_range' => '1900:2050',
    +    ];
    +  }
    +
    

    This def needs to move to DateTimeWidgetBase() so it is available in all of the Datetime widgets.

  4. +++ b/core/modules/datetime_range/config/schema/datetime_range.schema.yml
    @@ -71,3 +71,7 @@ field.widget.settings.daterange_datelist:
    diff --git a/core/modules/datetime_range/src/Element/YearRange.php b/core/modules/datetime_range/src/Element/YearRange.php
    
    diff --git a/core/modules/datetime_range/src/Element/YearRange.php b/core/modules/datetime_range/src/Element/YearRange.php
    new file mode 100644
    
    new file mode 100644
    index 0000000..e39a262
    
    index 0000000..e39a262
    --- /dev/null
    
    --- /dev/null
    +++ b/core/modules/datetime_range/src/Element/YearRange.php
    
    +++ b/core/modules/datetime_range/src/Element/YearRange.php
    +++ b/core/modules/datetime_range/src/Element/YearRange.php
    @@ -0,0 +1,104 @@
    

    This can't be in datetime_range. At a min, it needs to be in datetime, but possibly in with the core Elements since there could be value outside these modules.

    ?

  5. +++ b/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeDatelistWidget.php
    @@ -27,7 +27,9 @@ public static function defaultSettings() {
    -    ] + parent::defaultSettings();
    +    ] + parent::defaultSettings() + [
    +      'year_range' => '1900:2050',
    +    ];
       }
    

    See above bit about moving to the base class.

  6. +++ b/core/modules/datetime_range/src/Plugin/Field/FieldWidget/DateRangeDefaultWidget.php
    index 0000000..308751d
    --- /dev/null
    
    --- /dev/null
    +++ b/core/modules/datetime_range/tests/src/Functional/DateRangeYearRangeTest.php
    
    +++ b/core/modules/datetime_range/tests/src/Functional/DateRangeYearRangeTest.php
    +++ b/core/modules/datetime_range/tests/src/Functional/DateRangeYearRangeTest.php
    @@ -0,0 +1,121 @@
    
    @@ -0,0 +1,121 @@
    +<?php
    +
    +namespace Drupal\Tests\datetime_range\Functional;
    +
    

    Need to add coverage for the datetime widgets and mirror the test in datetime module for these settings.

jofitz’s picture

Status: Needs work » Needs review
StatusFileSize
new5.11 KB
new16.79 KB
  1. Done
  2. After the change in 1. this is now needed.
  3. Done
  4. Moved into core elements.
  5. Done
  6. Added equivalent test coverage for datetime widgets.

Also fixed test failure and coding standards error.

Status: Needs review » Needs work

The last submitted patch, 26: 2836054-26.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jofitz’s picture

Status: Needs work » Needs review
StatusFileSize
new1.14 KB
new16.74 KB

Corrected error in new test.
Removed unused use statement.

jhedstrom’s picture

  1. +++ b/core/lib/Drupal/Core/Render/Element/YearRange.php
    @@ -0,0 +1,103 @@
    +class YearRange extends FormElement {
    

    Should we add a kernel or unit test specific to this new render element? I was trying to find a specific one for the existing data render element, but could not find one, however many render elements have dedicated unit tests. These public methods seem pretty easy to unit test.

  2. +++ b/core/modules/datetime_range/tests/src/Functional/DateRangeYearRangeTest.php
    @@ -0,0 +1,121 @@
    +    // Test the new 1978:2017 range.
    

    Let's add at least one test that deals with relative dates to ensure those are working (eg, -3:+3).

mpdonadio’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Agree with needing a little more test coverage.

#2 should test 1978:2017, -3:2017, 1978:+3, -3:+3, ie all of the possibilities.

anas_maw’s picture

Not working if you don't give the field default value.

jofitz’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
StatusFileSize
new5.62 KB
new19.61 KB
  1. Added unit test for this render element.
  2. Added more tests to the functional test.
  3. Added condition to handle case when default value is not provided.
anas_maw’s picture

Hello is it possible to make this work if we use 'now' date for example?
Also try to enter start day +1 and the end date +10 for example, this is not working.

jofitz’s picture

@Anas_maw Rather than 'now' you can use '+0' (or perhaps open a follow-up issue).

What is not working with '+1:+10'?

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

jofitz’s picture

StatusFileSize
new1.97 KB
new19.63 KB
  1. Correct form validation that is preventing the starting year being a positive relative value (e.g. +1).
  2. Update the error messages to agree with the form element descriptions.
  3. Correct indentation.
josue2591’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new27.42 KB

Tested in 8.6.x. Works.

mpdonadio’s picture

Status: Reviewed & tested by the community » Needs work

We need to add a check so that the start year <= the end year, and test coverage for that.

jibran’s picture

+++ b/core/modules/datetime/config/schema/datetime.schema.yml
@@ -65,7 +65,7 @@ field.formatter.settings.datetime_time_ago:
 field.widget.settings.datetime_datelist:

@@ -81,3 +81,7 @@ field.widget.settings.datetime_datelist:
 field.widget.settings.datetime_default:

+++ b/core/modules/datetime_range/config/schema/datetime_range.schema.yml
@@ -55,7 +55,7 @@ field.formatter.settings.daterange_custom:
 field.widget.settings.daterange_datelist:

@@ -71,3 +71,7 @@ field.widget.settings.daterange_datelist:
 field.widget.settings.daterange_default:

Now that datetime and datetime_range are not experimental anymore we need an upgrade path and upgrade path test as well.

mpdonadio’s picture

Since these are just widget settings with default values, don’t we just need an empty post update to trigger a rebuild?

jibran’s picture

+++ b/core/modules/datetime/config/schema/datetime.schema.yml
@@ -81,3 +81,7 @@ field.widget.settings.datetime_datelist:
+    year_range:

+++ b/core/modules/datetime_range/config/schema/datetime_range.schema.yml
@@ -71,3 +71,7 @@ field.widget.settings.daterange_datelist:
+    year_range:

As we are adding a new key to the datelist and default formatters we need to update the config for the existing field formatters with the new key and default values.

iyyappan.govind’s picture

Hi Team,

I love this post. I have same issue, I just used the form_alter to restrict the date range. So my date range is 1990 to current year. It works fine but I need the descending date sort order, means 2018, 2017, ...1990. Is this possible via form alter? It would be great if we implement the sort order in UI if it is not in date form api.

Thank you.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

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

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

rob230’s picture

Would love this feature as well. It's not very useful for example to have a date of birth field where you can choose 2050 as the year.

manuel.adan’s picture

StatusFileSize
new19.63 KB

Just 8.8/8.9 re-roll with no additional changes.

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

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now 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.

mpp’s picture

The patch in #47 has an issue with the namespace for t()

MachineNameTest.php also defines Drupal\Core\Render\Element\t, not t() so function_exists will return false (see https://www.php.net/manual/en/function.function-exists.php#117916) but will trigger this error:

PHP Fatal error:  Cannot redeclare Drupal\Core\Render\Element\t() (previously declared in /web/core/tests/Drupal/Tests/Core/Render/Element/MachineNameTest.php:119) in /web/core/tests/Drupal/Tests/Core/Render/Element/YearRangeTest.php on line 70
raman.b’s picture

Status: Needs work » Needs review
StatusFileSize
new19.87 KB
new1.67 KB

Updating Functional tests & addressing #49

raman.b’s picture

StatusFileSize
new20.44 KB
new3.31 KB

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

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

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

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kim.pepper’s picture

Issue tags: +#pnx-sprint

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

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.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.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now 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.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now 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.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new144 bytes

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new19.87 KB

Rerolled, applies cleanly against 9.5.x and 10.1.x.

damienmckenna’s picture

Assigned: Unassigned » damienmckenna

Am working on the fixes.

damienmckenna’s picture

StatusFileSize
new1.1 KB
new20.05 KB

This will hopefully fix the problems; I accidentally lost the t() function change in YearRange.php.

damienmckenna’s picture

Status: Needs review » Needs work

I don't understand why the t() change is required in the first place? But it's causing the test to fail.

Version: 10.1.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, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

damienmckenna’s picture

Assigned: damienmckenna » Unassigned
sethhill’s picture

StatusFileSize
new19.3 KB

Rerolled the patch in #61 against 10.3.x

danielveza made their first commit to this issue’s fork.

danielveza’s picture

Converted the patch from #65 to an MR. Lets see how the pipeline looks

danielveza’s picture

Status: Needs work » Needs review

Passing pipeline, now with update path & tests

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: -Needs upgrade path +Needs issue summary update

Can the IS be updated to use the standard template please.

Thanks

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.

anybody’s picture

Issue summary: View changes

This is not only to broad for certain cases, it's also to limited for others, e.g. if using Drupal for historical data.

See my comment in #2942828: Remove hard-coded #date_year_range making it impossible to enter dates < 1900, > 2050. Instead make them optional and configurable in the UI. I'm unsure if we should better proceed here and postpone the other issue or start from scratch over there. The issue summary here was focused on "datelist" (List!) where such a limit at least makes some sense, but currently the limit also applies to HTML5 datepicker, which is a bug. That's what the other issue is about.

However, we should get this finished.