Share icon

Drupal form api ap usually used to build the custom form to extended the functionality of drupal according to your need.

We can change the year range in date element of form api using following method:

 



function hook_form() {
    $form['date_element'] = array(
        '#type' => 'date',
        '#title' => t('date'),
        '#default_value' => array(
            'day' => format_date(time(), 'custom', 'j'),
            'month' => format_date(time(), 'custom', 'n'),
            'year' => format_date(time(), 'custom', 'Y')),
        '#after_build' => array('__set_year_range'));
}

function __set_year_range($form_element, $form_values) {
$todayYear = date("Y");
    $form_element['year']['#options'] = drupal_map_assoc(range(2000, $todayYear));
    return $form_element;
}

Add new comment

Restricted HTML