How to change the date year range on a Form API 'date' element
tags:
By default, the range of a date element goes from the year 1900 to 2050. In some cases, we don't want users to enter a year value in the future, so we're going to have to limit the range.
1) Add the '#process' property to your date Form API element, and add a property for the start and end year (or you could hardcode this later if you don't want to make it variable)
5) Replace it by:
6) Now your form element should be within the range you supplied. Alternately you could also not use the #start_year and #end_year properties, but just change the range in your copied function, if you don't want to make them variable.
2) Open the file /includes/form.inc and find the function expand_date().
3) Copy the function, and paste it in your own module. Change the name of the function to the value of your #process property (in our case custom_date_element)
4) Find the following code in the function:
- function custom_date_element($element) {
- (...)
- case 'year':
- break;
- function custom_date_element($element) {
- (...)
- case 'year':
- break;
thanks, was very helpful :)
thanks, was very helpful :)
thanks!
Also, you can put your custom_date_element($element) in template.php instead of the module file.
I did it this way because I have multiple modules calling the function, but I realistically should have all of these modules consolidated, but just an FYI.
Thanks again for the greatness.
Very good tip, thanks Also
Very good tip, thanks
Also note that if you do this as part of a php page within drupal then the overriding function must also exist within that page.
Thanks!
Thanks it worked.
Thanks
It works flawlessly.
Post new comment