I use better exposed filters with views. I chose to show field "term reference" as hierarchical checkboxes on my exposed filter. It works great.

But now I need to add quantity of nodes having certain term near each checkbox inside filter. What is the best way to do it? Thank you!


I ended up with editing \sites\all\modules\better_exposed_filters\better_exposed_filters.theme

Inside it I made a query to field_data_, got all terms with nodes count:

function theme_select_as_checkboxes:

$res = db_query('SELECT field_dance_styleee_tid, COUNT(*) as count FROM field_data_field_dance_styleee GROUP BY field_dance_styleee_tid'); $allNodes = array(); while($record = $res->fetchAssoc()){ $allNodes[$record['field_dance_styleee_tid']] = $record; }

Then passed $allNodes to

  $output .= bef_checkbox($element, $key, $value, array_search($key, $selected_options) !== FALSE, $allNodes);

function bef_checkbox($element, $value, $label, $selected, $allNodes=array()) {

$value = check_plain($value); $label = check_plain($label); $id = drupal_html_id($element['#id'] . '-' . $value); // Custom ID for each checkbox based on the 's original ID $properties = array( '#required' => FALSE, '#id' => $id, '#type' => 'bef-checkbox', '#name' => $id, );

// Prevent the select-all-none class from cascading to all checkboxes if (!empty($element['#attributes']['class']) && FALSE !== ($key = array_search('bef-select-all-none', $element['#attributes']['class']))) { unset($element['#attributes']['class'][$key]); }

// Unset the name attribute as we are setting it manually. unset($element['#attributes']['name']);

$checkbox = '';

$properties['#children'] = "$checkbox $label". (( $element['#name'] == 'field_dance_styleee_tid' && isset($allNodes[$value]) )? "(".$allNodes[$value]['count'].")" : '')."";

$output = theme('form_element', array('element' => $properties)); return $output; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.