0

I have a View that uses aggregation to count the number of nodes of a custom color type. For example, there are 10 red nodes, 10 green nodes and 20 blue nodes. For each color, I would want it's node count and the total count to show up on each row, like this:

Color    Count   Total
Red       10      40
Green     10      40
Blue      20      40

Ultimately, I do want to calculate the percentage in each row as well, but for now I'd just like the total.

What's the most efficient "drupal" way of doing this?

0

A no-custom-code solution could be something like this (not the most performant way though but you could perhaps do something with views caching).

Explanation:
Create one view for the total count that with the use of aggregration and a content type filter returns the total number of nodes (the Total column) In another Taxonomy Terms view you attach the total count view by adding a Global: View field that is provided by the Views Field View module

Steps
- Create a content type e.g. "Colornode"
- Create a taxonomy vocabulary called "Colors"
- Add three terms Blue,Green and Red
- Add a term reference field and select Colors as the vocabulary - Install and enable the Views Field View module

Then import these two views in this order:

$view = new view();
$view->name = 'nodecounts';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Node counts';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Node counts';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['style_options']['default_row_class'] = FALSE;
$handler->display->display_options['style_options']['row_class_special'] = FALSE;
$handler->display->display_options['row_plugin'] = 'fields';
$handler->display->display_options['row_options']['inline'] = array(
  'type_1' => 'type_1',
  'type' => 'type',
);
$handler->display->display_options['row_options']['separator'] = ': ';
/* Field: Content: Type */
$handler->display->display_options['fields']['type_1']['id'] = 'type_1';
$handler->display->display_options['fields']['type_1']['table'] = 'node';
$handler->display->display_options['fields']['type_1']['field'] = 'type';
$handler->display->display_options['fields']['type_1']['label'] = '';
$handler->display->display_options['fields']['type_1']['exclude'] = TRUE;
$handler->display->display_options['fields']['type_1']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['type_1']['element_default_classes'] = FALSE;
/* Field: COUNT(Content: Type) */
$handler->display->display_options['fields']['type']['id'] = 'type';
$handler->display->display_options['fields']['type']['table'] = 'node';
$handler->display->display_options['fields']['type']['field'] = 'type';
$handler->display->display_options['fields']['type']['group_type'] = 'count';
$handler->display->display_options['fields']['type']['label'] = '';
$handler->display->display_options['fields']['type']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['type']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['type']['separator'] = '';
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'colornode' => 'colornode',
);

/* Display: Block */
$handler = $view->new_display('block', 'Block', 'block');

And the second one:

$view = new view();
$view->name = 'colorterms';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'taxonomy_term_data';
$view->human_name = 'Color terms';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Color terms';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['group_by'] = TRUE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '100';
$handler->display->display_options['style_plugin'] = 'table';
/* Relationship: Taxonomy term: Content using Color */
$handler->display->display_options['relationships']['reverse_field_color_node']['id'] = 'reverse_field_color_node';
$handler->display->display_options['relationships']['reverse_field_color_node']['table'] = 'taxonomy_term_data';
$handler->display->display_options['relationships']['reverse_field_color_node']['field'] = 'reverse_field_color_node';
/* Relationship: Content: Color (field_color) */
$handler->display->display_options['relationships']['field_color_tid']['id'] = 'field_color_tid';
$handler->display->display_options['relationships']['field_color_tid']['table'] = 'field_data_field_color';
$handler->display->display_options['relationships']['field_color_tid']['field'] = 'field_color_tid';
$handler->display->display_options['relationships']['field_color_tid']['relationship'] = 'reverse_field_color_node';
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'taxonomy_term_data';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
$handler->display->display_options['fields']['name']['link_to_taxonomy'] = TRUE;
/* Field: COUNT(Content: Color) */
$handler->display->display_options['fields']['field_color']['id'] = 'field_color';
$handler->display->display_options['fields']['field_color']['table'] = 'field_data_field_color';
$handler->display->display_options['fields']['field_color']['field'] = 'field_color';
$handler->display->display_options['fields']['field_color']['relationship'] = 'reverse_field_color_node';
$handler->display->display_options['fields']['field_color']['group_type'] = 'count';
$handler->display->display_options['fields']['field_color']['label'] = 'Count';
$handler->display->display_options['fields']['field_color']['separator'] = ', ';
/* Field: Global: View */
$handler->display->display_options['fields']['view']['id'] = 'view';
$handler->display->display_options['fields']['view']['table'] = 'views';
$handler->display->display_options['fields']['view']['field'] = 'view';
$handler->display->display_options['fields']['view']['label'] = 'Total';
$handler->display->display_options['fields']['view']['view'] = 'nodecounts';
$handler->display->display_options['fields']['view']['display'] = 'block';
/* Filter criterion: Taxonomy vocabulary: Machine name */
$handler->display->display_options['filters']['machine_name']['id'] = 'machine_name';
$handler->display->display_options['filters']['machine_name']['table'] = 'taxonomy_vocabulary';
$handler->display->display_options['filters']['machine_name']['field'] = 'machine_name';
$handler->display->display_options['filters']['machine_name']['value'] = array(
  'colors' => 'colors',
);
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['relationship'] = 'reverse_field_color_node';
$handler->display->display_options['filters']['type']['value'] = array(
  'colornode' => 'colornode',
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'color-terms';
$handler->display->display_options['menu']['type'] = 'normal';
$handler->display->display_options['menu']['title'] = 'Color terms';
$handler->display->display_options['menu']['name'] = 'main-menu';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page_1');
$handler->display->display_options['path'] = 'color-terms';
$handler->display->display_options['menu']['type'] = 'normal';
$handler->display->display_options['menu']['title'] = 'Color terms';
$handler->display->display_options['menu']['name'] = 'main-menu';

Result enter image description here

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.