I'm trying to learn how to use the forms api, for a particular module, but so far I've drawn a blank in terms of "best practice" on this... so here's my question:
When building a form in drupal, I can define a 'date' field, for example in this code snippet:
$form['user_date'] = array(
'#title' => t('Desired date'),
'#type' => 'date',
'#description' => t('Please enter your desired date.'),
);This will produce a nice html form with three elements - day, month and year, including automatic date validation.
But...
- I only want the form to display and select the month and year (the day is not required)
- I want to default the year range to be from 2000 through to (current year)+1
- I might want to use the longer form of month names (eg January instead of Jan)
So far, I have started going down the path of overriding the "expand_date" function which is called by default, but this seems like almost much work as just rewriting a bespoke year/month selector from scratch (albeit I'm building on top of good code).
Have I missed something really obvious? eg does the drupal 'date' form field type take arguments, for example like the PHP date formats, which would make this task easier?
All advice welcomed!
Cheers & thanks,
Pete








A Place To Start
A place to start is with the formapi reference at http://api.drupal.org/api/file....
I'm not sure you can remove the day, but I've never tried it so I really don't know.
Matt Farina
Geeks and God Co-Host
www.innovatingtomorrow.net
www.mattfarina.com
CCK Date Field has varying granularity
Pete,
I don't know if this will solve your problem, but the CCK Date field type lets you specify granularity, so it can be easily restricted to accept only month and year, and will even present them as drop-down selections if you so desire.
If the data you need to collect can be stored in a CCK field, this is probably that quick and obvious answer you were seeking.
Micah
override was the way to go
Thanks Micah, (and Matt) -
in the end I went down the track of providing an override function for the 'date' form field type. Turns out that it wasn't too hard after all.
For the particular area of the site where I am working, using CCK is not an option, but your comments got me thinking further about this, and eventually I worked it out... once I have finished the module, I'll post a link here.
Cheers & thanks
Pete
www.lifesourcetv.com