I have created views with BEF check boxes as a filter criteria.

I want to get count of each check boxes as mentioned in below image.

enter image description here

Reference link.

My view name is photo_search. So I have renamed below tpl to views-exposed-form--photo-search.tpl

I have copied views-exposed-form.tpl from views/theme to mytheme/templates . But its not working (Cache cleared & Rescanned tpl from views).

  • On a side note: If you would work with facets you would have this functionality out of the box – Fons Vandamme Feb 25 '15 at 6:39
  • @f0ns Is there check box option in facets? – Kalidasan Feb 25 '15 at 6:49

First of all make alter in your view and place the code.

// Set an index if there are multiple terms used.

    $index = 0;
    // Used for the array's string index.
    $string_index = '';
    // Continue for each vocabulary that is used.
    while (isset($form['tid' . $string_index])) {
        // Cycle through each of the terms.
        foreach ($form['tid' . $string_index]['#options'] as $tid => $option) {
            // Build a query to get the nodes for this term that have been
            // specified as filters.
            $query = db_select('node', 'n')
                    ->fields('n', array('title'))
                    ->condition('n.type', $form_state['view']->display_handler->handlers['filter']['type']->value, 'IN')
                    ->condition('fa.field_advertisements_value', 'forsale', '=')
                    ->condition('ti.tid', $tid, '=');
            // A quirk in db_select(). You need to add the join separately.
            $query->innerJoin('taxonomy_index', 'ti', 'ti.nid = n.nid');
            $query->innerJoin('field_data_field_advertisements', 'fa', 'fa.entity_id = n.nid');
            $results = $query->execute();
            $count = $results->rowCount();

// get the count of nodes or watever with your costom query

            $val = $form['tid' . $string_index]['#options'][$tid];
            $form['tid' . $string_index]['#options'][$tid] = str_replace("<span>(" . $count . ")</span><span>(" . $count . ")</span>", "<span>(" . $count . ")</span>", $val . '<span>(' . $count . ')</span>');
        // Increment for the next tid index in the array.
        $index += 1;
        $string_index = '-' . $index;
        //$string_index = '_' . $index;

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.