Problem/Motivation

Currently, the Year Only doesn't provide useful default value options. The only settings are '- None -' and '0'.

Current default value options

Default values could provide an improved user experience, e.g in cases where there is a large range of years, or the current year is most-commonly used.

Proposed resolution

Implement YearOnlyFieldItemList to provide useful default options (e.g. current year, relative year, specific year).

Remaining tasks

  • Write the patch
  • Review
  • Commit

User interface changes

Add a select for default value type. Provide dependent fields to collect relative date string (strtotime) and/or specific year value.

Data model changes

Add default_type, default_relative and default_specific to schema.

Issue fork yearonly-3172745

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

justcaldwell created an issue. See original summary.

justcaldwell’s picture

Status: Active » Needs review
StatusFileSize
new40.27 KB
new7.69 KB

Patch attached. Adds options for:

  • Current year
  • Relative year, with dependent input for strtotime value to use in calculating the year
  • Specific year, with dependent input for year value

For example:
Screenshot of example default options added by the patch

pcate’s picture

Patch worked for me. Also, although this is marked as for 9.x, it applied fine with the latest 8.x release.

pcate’s picture

Status: Needs review » Reviewed & tested by the community
joel_osc’s picture

+1 RBTC works great, thank-you!

la558’s picture

Update:
Please disregard.
The problem was that the patch was not being applied correctly.
Now, I'm able to see the 3 appropriate options.

Hello,
Could it be that something has changed.
After applying the patch, all what I see on the default options drop down list, are a list of years.
'Current year', 'Relative year', and 'Specific year' are no set as options.

Thanks!

justcaldwell’s picture

Version: 9.0.0 » 9.0.x-dev
peacog’s picture

Version: 9.0.x-dev » 9.1.0
Status: Reviewed & tested by the community » Needs review
StatusFileSize
new7.73 KB

Re-rolled for 9.1

crutch’s picture

Patch didn't apply to 9.1 but did for dev and works as expected. Thank you

gaurav-mathur’s picture

Assigned: Unassigned » gaurav-mathur
gaurav-mathur’s picture

Assigned: gaurav-mathur » Unassigned

Patch #8 applied successfully on Drupal 9.4.3 and PHP 8.1.6.
The patch work properly for me.
Thanks.

adaragao’s picture

Doesn't work on Drupal 10.3

Unfortunately I'm not a developer, can someone fix it, please?

gab.’s picture

StatusFileSize
new330.16 KB

This is still not fixed on 9.1
Patch #8 did not work for me, couldn't be applied on 9.1
test

justcaldwell’s picture

Here's a patch re-rolled for 9.1.1 and/or 9.x-dev. Please give it a try.

adaragao’s picture

Patch 14 also works on Drupal 10.3.1. Thank you !

pcate’s picture

Version: 9.1.0 » 9.1.2
Status: Needs review » Reviewed & tested by the community
StatusFileSize
new49.71 KB

Patch #14 worked for me with the latest 9.1.2 version of the module.

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

vitaliyb98’s picture

Moved changes from patch #14 into MR

vitaliyb98’s picture

Status: Reviewed & tested by the community » Needs work

PHPCS and PHPSTAN pipelines failed, moved the issue to the Needs work

vitaliyb98’s picture

Status: Needs work » Needs review

Fixed pipelines

justcaldwell’s picture

Version: 9.1.2 » 9.x-dev
Assigned: Unassigned » justcaldwell
justcaldwell’s picture

Assigned: justcaldwell » Unassigned

Cleaned a field description and some error messages, and adjusted validation to ensure any default value is within the min/max year range regardless of type.

I did quite a bit of manual testing and I feel like this is ready. I'll leave it on Needs review for a while in case someone else can test/review.

vitaliyb98’s picture

Hi, I will try to review it and make some manual testing this week.

vitaliyb98’s picture

Status: Needs review » Needs work

Hi @justcaldwell, I tested it with Drupal 10, and it works fine, but I found a problem with #states in Drupal 11. States for the default year selector don't work. (Drupal 11.2.0, admin theme: Claro)

Here the reason:
In Drupal 10, select the element: (id="edit-default-value-input-default-type")

<select data-drupal-selector="edit-default-value-input-default-type" aria-describedby="edit-default-value-input-default-type--description" id="edit-default-value-input-default-type" name="default_value_input[default_type]" class="form-select form-element form-element--type-select">

In Drupal 11 ( id="edit-default-value-input-default-type--M7GELMJWr64"):

<select data-drupal-selector="edit-default-value-input-default-type" aria-describedby="edit-default-value-input-default-type--M7GELMJWr64--description" id="edit-default-value-input-default-type--M7GELMJWr64" name="default_value_input[default_type]" class="form-select form-element form-element--type-select">

Thats why states not worked in Drupal 11:

         '#states' => [
            'visible' => [
              ':input[id="edit-default-value-input-default-type"]' => ['value' => static::DEFAULT_VALUE_RELATIVE],
            ],
            'required' => [
              ':input[id="edit-default-value-input-default-type"]' => ['value' => static::DEFAULT_VALUE_RELATIVE],
            ],
          ],

I suggest we could change it to:

          '#states' => [
            'visible' => [
              ':input[name="default_value_input[default_type]"]' => ['value' => static::DEFAULT_VALUE_RELATIVE],
            ],
            'required' => [
              ':input[name="default_value_input[default_type]"]' => ['value' => static::DEFAULT_VALUE_RELATIVE],
            ],
          ],
justcaldwell’s picture

Status: Needs work » Fixed

Good catch, Vitaliy — thanks.

Updated and confirmed the fix also works with Drupal 10. I'm calling this fixed, and will merge soon.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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