4

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.

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

| improve this answer | |
-1

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.

| improve this answer | |
  • This was only the case for the D7 module. Date in core does not have these options. – tanc May 25 '17 at 13:52
-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.

| improve this answer | |
  • The link suggests creating a custom module so I don't see the benefit here? – Daniel Harper Aug 12 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 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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