By dazz


2011-08-16 13:49:22 8 Comments

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?

5 comments

@vadim 2014-10-12 22:11:25

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;
  }
}

@thesame- 2013-10-29 07:49:42

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;
  }
}

@Mołot 2013-10-29 08:12:24

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 :)

@Kojo 2016-05-19 18:49:09

the loop thing was a very useful advice ! thks

@Felix Eve 2013-10-11 11:40:15

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.

@dazz 2011-08-17 14:32:44

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.

@Volker 2012-07-07 10:41:01

Any chance to see the code you've used for this? THX in advance!

@Felix Eve 2013-10-11 11:43:48

@Volker: I've just added an answer with some sample code...

@kalabro 2011-08-16 14:08:44

Couple of links (may be useful)

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

Related Questions

Sponsored Content

1 Answered Questions

How to change values views exposed filter based upon view results?

  • 2015-11-05 17:35:15
  • CR47
  • 719 View
  • 1 Score
  • 1 Answer
  • Tags:   hooks views

1 Answered Questions

How use views exposed filters with taxonomy and panels

1 Answered Questions

[SOLVED] How to add content of nested terms to Taxonomy term View

1 Answered Questions

[SOLVED] View of related nodes by continent or country without taxonomy terms

  • 2016-04-06 17:38:30
  • LTurcios
  • 354 View
  • 1 Score
  • 1 Answer
  • Tags:   nodes views

3 Answered Questions

How to implement Node Access based on a Term in the user's profile?

3 Answered Questions

[SOLVED] Changing the logic for an exposed taxonomy select in a view

  • 2013-10-23 10:54:03
  • njp
  • 202 View
  • 1 Score
  • 3 Answer
  • Tags:   6 views

1 Answered Questions

[SOLVED] Show exposed filter value when returning results

  • 2013-07-27 16:28:25
  • itsututa
  • 829 View
  • 3 Score
  • 1 Answer
  • Tags:   views

1 Answered Questions

[SOLVED] Custom target URL for Views content pane exposed filter (block)

  • 2012-05-20 08:48:11
  • ijujym
  • 971 View
  • 0 Score
  • 1 Answer
  • Tags:   7 views

1 Answered Questions

Sponsored Content