Last active
December 19, 2015 19:29
-
-
Save stborchert/6006774 to your computer and use it in GitHub Desktop.
This file contains 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
<?php | |
/** | |
* Implements hook_form_FORM_ID_alter(). | |
* | |
* Alter views exposed forms for collapsible filters. | |
*/ | |
function MYMODULE_form_views_exposed_form_alter(&$form, &$form_state) { | |
if (empty($form_state['view']) || !in_array($form_state['view']->name, array('NAME_OF_VIEW', 'NAME_OF_VIEWS_DISPLAY'))) { | |
// We alter the exposed form of a single views display, so return if this is | |
// not the expected view. | |
return; | |
} | |
if (isset($form['type'])) { | |
// Add option to select all items (equals to resetting the filter). | |
$options = array( | |
'All' => variable_get('views_exposed_filter_any_label', 'new_any') == 'old_any' ? t('<Any>') : t('- Any -'), | |
); | |
$options += $form['type']['#options']; | |
// Change size of field based on number of options (max: 5 items). | |
$form['type']['#size'] = min(array(count($options), 5)); | |
if (count($options) <= 2) { | |
// Hide filter if there is only one option available (additional | |
// to "All"). | |
$form['type']['#access'] = FALSE; | |
} | |
$form['type']['#options'] = $options; | |
} | |
// Alter multi-value dropdowns. | |
$form_multiple_selects = array(); | |
foreach (element_children($form) as $element_name) { | |
if (isset($form[$element_name]['#type']) && $form[$element_name]['#type'] == 'select' && !empty($form[$element_name]['#multiple'])) { | |
$form_multiple_selects[$element_name] = array( | |
'size' => isset($form[$element_name]['#size']) ? $form[$element_name]['#size'] : 5, | |
); | |
} | |
} | |
if (count($form_multiple_selects)) { | |
$form['#attached'] += array( | |
'js' => array(), | |
'css' => array(), | |
); | |
// Attach custom javascript to the form. | |
$form['#attached']['js'][] = drupal_get_path('module', 'MYMODULE') . '/js/MYMODULE.admin.js'; | |
$form['#attached']['js'][] = array( | |
'data' => array( | |
'collapsibleFilter' => array( | |
'multiple_selects' => $form_multiple_selects, | |
), | |
), | |
'type' => 'setting', | |
); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment