5

How do I create a year-only drop-down field?

I want to create a Drupal 8 field that shows a year drop-down with 100 years to the current year.

3 Answers 3

4

The Year Only module provides this feature by defining a new field type.

Year only field type.

It comes with a range configuration.

Year only range configuration

It produces the following result in the entity (node, ...) edit form. Configured with unlimited number of values in this example.

Year only values

-1

I would personally recommend following https://drupal.stackexchange.com/a/242011/5438. It's a little more work with some coding but it doesn't mean you need to add another module that needs to be supported in relation to many drupal areas. You can use a List (integer) field and add things there.

You can can also use a field_integer textfield and use a form alterer.

2
  • The link suggests creating a custom module so I don't see the benefit here? Aug 12, 2020 at 17:44
  • 1
    @DanielHarper it's a helper module that uses a core API hook, this means you will hardly have to update it and it uses core's field types instead of one provided by a module. The level of maintainability is considerable less IMO. I did say it was a personal recommendation, this is the reasoning behind it.
    – Mescalito
    Aug 13, 2020 at 21:39
-2

You can use Date module.

This package contains both a flexible date/time field type Date field and a Date API that other modules can use.

When you add field you can choose what you want to show (day, month, year) and + and - from current year.

1
  • 1
    This was only the case for the D7 module. Date in core does not have these options.
    – tanc
    May 25, 2017 at 13:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.