Skip to content

Instantly share code, notes, and snippets.

@init90
Last active May 4, 2018 19:19
Show Gist options
  • Save init90/2bd9a2092ce5863d54d811366ed95896 to your computer and use it in GitHub Desktop.
Save init90/2bd9a2092ce5863d54d811366ed95896 to your computer and use it in GitHub Desktop.
Drupal, if views filter without options we remove it from output on hook_form_alter().
if (isset($form['#id']) && $form['#id'] == 'VIEW_FILTER_ID') {
$filters_list = array();
// Get filters names.
foreach($form['#info'] as $filter_name => $filter_info) {
// Return filter name without 'filter-' prefix.
$filters_list[] = substr($filter_name, 7);
}
foreach($filters_list as $filter) {
// If filter without options, we remove it.
if (isset($form[$filter]) && empty($form[$filter]['#options'])) {
unset($form['#info']["filter-{$filter}"]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment