Created
November 5, 2012 15:05
-
-
Save brockboland/4017640 to your computer and use it in GitHub Desktop.
Combine month and year dropdowns for Views date filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Implements hook_date_select_process_alter(). | |
* Alter the month/year dropdown filter for views to use a single dropdown | |
* instead of using separate year and month dropdowns. | |
* | |
* This is ONLY done on elements where #combine_dropdowns is TRUE on the | |
* element. This is to prevent the process from being used on form elements | |
* where it is not intended. Use form_alter to set #combine_dropdowns on form | |
* elements that should use the combined dropdown. | |
* | |
* For more info, see hook_date_select_process_alter() documentation in | |
* date.api.php of the contrib date module. | |
*/ | |
function mymodule_date_select_process_alter(&$element, &$form_state, $context) { | |
// Check the field name (usually date_filter, on Views) | |
$field_name = $element['#parents'][0]; | |
$element_from_formvar = $context['form'][$field_name]; | |
// Check if this element should use combined Year/Month dropdown | |
if (isset($element_from_formvar['#combine_dropdowns']) && $element_from_formvar['#combine_dropdowns']) { | |
// Year/month dropdowns | |
if ($element['#date_format'] == 'Y-m') { | |
// Init the year and month values | |
$year = $month = 0; | |
// If the field already has values, use them | |
if (isset($form_state['values'][$field_name]['value']['year_and_month'])) { | |
$val = $form_state['values'][$field_name]['value']['year_and_month']; | |
if (!empty($val)) { | |
// Split the year and month | |
list($year, $month) = explode('-', $val); | |
if ($year > 0 && $month > 0) { | |
// Save the values into the actual fields in form_state so it's used | |
$form_state['values'][$field_name]['value']['year'] = $year; | |
$form_state['values'][$field_name]['value']['month'] = $month; | |
} | |
} | |
} | |
// Build a combined year/month dropdown | |
$values = array('' => t('<Any>')); | |
foreach ($element['year']['#options'] as $y) { | |
if ($y > 0) { | |
foreach ($element['month']['#options'] as $mon_no => $mon_name) { | |
if ($mon_no > 0) { | |
if ($mon_no < 10) { | |
$mon_no = '0' . $mon_no; | |
} | |
$values[$y . '-' . $mon_no] = $mon_name . ' ' . $y; | |
} | |
} | |
} | |
} | |
// Add the new combined select | |
$element['year_and_month'] = array( | |
'#type' => 'select', | |
'#options' => $values, | |
'#default_value' => '', | |
'#required' => FALSE, | |
); | |
// Nuke the Year and Month dropdowns, replacing with the actual values if | |
// the filter has been submitted already | |
$element['year'] = array( | |
'#type' => 'value', | |
'#value' => $year, | |
); | |
$element['month'] = array( | |
'#type' => 'value', | |
'#value' => $month, | |
); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment