hook_form_views_exposed_form_alter

/**
 * Implements hook_form_views_exposed_form_alter().
 * Set the default exposed filter value.
 * @param $form
 * @param $form_state
 */
function hook_form_views_exposed_form_alter(&$form, &$form_state) {
  if (!isset($form_state['input']['field_date_value_1']['value']['date'])) {
    $form_state['input']['field_date_value_1']['value']['date'] = date("Y-m", strtotime("+1 month"));
  }
}

hook_form_views_exposed_form_alter() is run before the form is rendered. It’s hook_form_alter(), but with the form_id of view_exposed_form.

In the example I provided, I have a date filter with the name of: field_date_value_1.

By default, the value of the filter is empty. I want to change that. The code checks whether the filter value is set. If not, it sets the filter date value to that of next month.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s