Skip to content

Instantly share code, notes, and snippets.

@init90
Last active May 4, 2018 19:49
Show Gist options
  • Save init90/1c9c36607928e350a665aaa740e4e8ba to your computer and use it in GitHub Desktop.
Save init90/1c9c36607928e350a665aaa740e4e8ba to your computer and use it in GitHub Desktop.
Drupal 7, Prepared options in views explose filter. Display options only with content. Module for the same - Views Selective Filters.
/**
* Implements hook_form_alter().
*/
function MODULE_form_alter(&$form, &$form_state, $form_id) {
// We prepared product type filter, code below must display only node types
// which have added content.
if (isset($form['#id']) && $form['#id'] == 'EXPLOSE_FILTER_FORM_ID') {
$selected_brand = menu_get_object('taxonomy_term', 2);
// If isset product type filter.
if (isset($form['type']) && !empty($form['type']['#options']) && is_object($selected_brand)) {
$brand_id = $selected_brand->tid;
// Get node types with added content.
$q = db_select('field_data_field_brands', 'brands_fields');
$q->leftJoin('node', 'node', 'node.nid = brands_fields.entity_id');
$q->innerJoin('node_type', 'node_type', 'node_type.type = node.type');
$q->fields('node_type', array('type', 'name'));
$q->condition('brands_fields.entity_type', 'node');
$q->condition('brands_fields.field_brands_tid', $brand_id);
$q->condition('node.status', NODE_PUBLISHED);
$available_types = $q->execute()->fetchAllKeyed();
if (!empty($available_types)) {
$form['type']['#options'] = $available_types;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment