How to change the date year range on a Form API 'date' element
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.
Click 'read more' to find out how to change the year range.
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)
<span style="color: #000000"><span style="color: #0000BB"><?php<br>$form</span><span style="color: #007700">[</span><span style="color: #DD0000">'date_of_birth'</span><span style="color: #007700">] = array(<br> </span><span style="color: #DD0000">'#type' </span><span style="color: #007700">=> </span><span style="color: #DD0000">'date'</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'#title' </span><span style="color: #007700">=> </span><span style="color: #0000BB">t</span><span style="color: #007700">(</span><span style="color: #DD0000">'Date of birth'</span><span style="color: #007700">),<br> </span><span style="color: #DD0000">'#process' </span><span style="color: #007700">=> array(</span><span style="color: #DD0000">'custom_date_element'</span><span style="color: #007700">), </span><span style="color: #FF8000">// ADD #process property<br> </span><span style="color: #DD0000">'#start_year' </span><span style="color: #007700">=> </span><span style="color: #0000BB">1900</span><span style="color: #007700">,<br> </span><span style="color: #DD0000">'#end_year' </span><span style="color: #007700">=> </span><span style="color: #0000BB">format_date</span><span style="color: #007700">(</span><span style="color: #0000BB">time</span><span style="color: #007700">(), </span><span style="color: #DD0000">'custom'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Y'</span><span style="color: #007700">),<br>);<br></span><span style="color: #0000BB">?></span></span>
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:
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">function </span><span style="color: #0000BB">custom_date_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">) {<br> (...)<br> case </span><span style="color: #DD0000">'year'</span><span style="color: #007700">:<br> </span><span style="color: #0000BB">$options </span><span style="color: #007700">= </span><span style="color: #0000BB">drupal_map_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1900</span><span style="color: #007700">, </span><span style="color: #0000BB">2050</span><span style="color: #007700">));<br> break;<br></span><span style="color: #0000BB">?></span></span>
5) Replace it by:
<span style="color: #000000"><span style="color: #0000BB"><?php<br></span><span style="color: #007700">function </span><span style="color: #0000BB">custom_date_element</span><span style="color: #007700">(</span><span style="color: #0000BB">$element</span><span style="color: #007700">) {<br> (...)<br> case </span><span style="color: #DD0000">'year'</span><span style="color: #007700">:<br> </span><span style="color: #0000BB">$start_year </span><span style="color: #007700">= isset(</span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'#start_year'</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'#start_year'</span><span style="color: #007700">] : </span><span style="color: #0000BB">1900</span><span style="color: #007700">;<br> </span><span style="color: #0000BB">$end_year </span><span style="color: #007700">= isset(</span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'#end_year'</span><span style="color: #007700">]) ? </span><span style="color: #0000BB">$element</span><span style="color: #007700">[</span><span style="color: #DD0000">'#end_year'</span><span style="color: #007700">] : </span><span style="color: #0000BB">format_date</span><span style="color: #007700">(</span><span style="color: #0000BB">time</span><span style="color: #007700">(), </span><span style="color: #DD0000">'custom'</span><span style="color: #007700">, </span><span style="color: #DD0000">'Y'</span><span style="color: #007700">);<br> </span><span style="color: #0000BB">$options </span><span style="color: #007700">= </span><span style="color: #0000BB">drupal_map_assoc</span><span style="color: #007700">(</span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">$start_year</span><span style="color: #007700">, </span><span style="color: #0000BB">$end_year</span><span style="color: #007700">));<br> break;<br></span><span style="color: #0000BB">?></span></span>
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.