10

I have a couple of exposed filters to filter the content in my view. Most of them are taxonomy terms, now i would like to show how many nodes are tagged with that term behind the taxonomy term.

example:

London (5)
Paris (3)
Madrid (2)
...

Any idea how i can do this?

4

Here is some sample code to do this:

function hook_form_alter(&$form, &$form_state, $form_id) {
    switch($form_id) {
        case 'views_exposed_form':

            $allowed_categories = db_query("SELECT 1 as tid, "Term Name (2)" as `name`")->fetchAllKeyed();
            $allowed_categories = array_reverse($allowed_categories, TRUE);
            $allowed_categories['All'] = '- Any -';
            $allowed_categories = array_reverse($allowed_categories, TRUE);

            $form['field_category_tid']['#options'] = $allowed_categories;

            break;
    }
}

Just change the query to return 2 columns - the tid and the name with a count.

1

i created a module with a hook_form_alter().

In that function i created a query that counted the nodes attached to a certain taxonomy term and added this number at the end of every term.

  • Any chance to see the code you've used for this? THX in advance! – Volker Jul 7 '12 at 10:41
  • @Volker: I've just added an answer with some sample code... – Felix Eve Oct 11 '13 at 11:43
1
function hook_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id){
    case 'views_exposed_form':
      if ($form_state['view']->name == 'viewname') {
    //must add some bool so this doesn't get into infinite loop
    if(!isset($form_state['view']->gg)){
      $form_state['view']->gg = TRUE;
      $form_state['view']->execute();
    }

    $form['results_count'] = array(
      '#markup' => t('!count results match your criteria', array('!count' => '<b>'.$form_state['view']->total_rows.'</b>')),
      '#weight' => -99,
    );
      }

      break;
  }
}
  • Hello and welcome. While this code might answer the question, it would be in a good tone to explain why and how it is supposed to work, so the OP could learn from it, not only rely on others to write pieces for him. It looks like ideas and knowledge are what he is after, and that's a good thing that should be encouraged :) – Mołot Oct 29 '13 at 8:12
  • the loop thing was a very useful advice ! thks – Kojo May 19 '16 at 18:49
0

Couple of links (may be useful)

  1. Module Taxonomy Menu, that has count option.
  2. Question about these things, but without trusted answer.
0

This is an example from my real site. field_marka_prochnosti is a field of a node represented by a taxonomy vocabulary. I used phpMyAdmin to learn the name of a table and the name of the field of that table to make a query. bricksale_omega is the name of my theme.

function bricksale_omega_form_alter(&$form, &$form_state, $form_id) {
switch($form_id){
case 'views_exposed_form':

  foreach ($form['field_marka_prochnosti_tid']['#options'] as $tid => &$value) {
    $query = db_select('field_data_field_marka_prochnosti', 'f')
            ->condition('f.field_marka_prochnosti_tid', $tid);
    $query->addExpression('COUNT(*)');
    $count = $query->execute()->fetchField();
    $value = $value . ' (' . $count . ')';
  }

  break;
  }
}

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.