I'm using a date field on my custom type, if I use "select" widget for node editing, I can choose only between "YMD","MDY" and "DMY" formats.
Is there a way to add custom formats? I need an "only year" format.
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.Sign up to join this community
By default, no. The relevant code is in
What you want is usually called a "partial date", which is a bit of an oxymoron. In Drupal, datetime fields are stored as
DrualDateTime objects, which derive from
\DateTime so have both a full date and a proper time and timezone associated with them.
A partial date (year, year+month), has to make assumptions about the rest of the parts. For example, the date-only field currently sets the time to noon UTC. A year only field would have to do something similar.
You may be able to create your own widget for the datetime field to do this. You could take
DateTimeDatelistWidget as a starting point, and trim it down to be just the year. Then, you would need a masssageFormValues() to set the rest of the parts and then call the parent method (because
DateTimeWidgetBase::masssageFormValues() does stuff, too). This may or may not work, it is just the idea that popped into my head.
There is an open issue about this, https://www.drupal.org/node/2703037, that may or may not be implemeneted.