2

Note: The solution should not change the main Views properties, mainly that it is a View of content nodes.

I have a content type (books) which are under a taxonomy (Category). I have created a View to show the titles. By using "Grouping field" with the category (see picture #1), I can get the title of the category and order by that.

However, when I have books which are classified under several Categories (in this example, the book about Chess is under Sports and Science), I get a mixture of taxonomies in the Grouping field displayed, and the book does not appear in the other categories. I would like it to appear under Sports and under Sciende, and get rid of this "third" combined taxonomy "Science, Sports". (see picture #2).

Image 1

Picture #2

Image 2

Picture #2

2 Answers 2

5
+50

You have many solutions available.

Solution 1:

Enable "Use aggregation" option with the default settings, it should be enough.

Solution 2:

You can create a view of terms and add a "Relationships" with "Taxonomy term: Content using Category". As fields you have to add the content fields (with the new relation) and the term name (used for the grouping).

This is an example of the view assuming the content type is "article", the category field is called "field_category" and the vocabulary is "category":

$view = new view();
$view->name = 'nestedexample';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'taxonomy_term_data';
$view->human_name = 'nestedexample';
$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'] = 'nestedexample';
$handler->display->display_options['use_more_always'] = FALSE;
$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'] = '10';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['style_options']['grouping'] = array(
  0 => array(
    'field' => 'name',
    'rendered' => 1,
    'rendered_strip' => 0,
  ),
);
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: Taxonomy term: Content using Category */
$handler->display->display_options['relationships']['reverse_field_category_node']['id'] = 'reverse_field_category_node';
$handler->display->display_options['relationships']['reverse_field_category_node']['table'] = 'taxonomy_term_data';
$handler->display->display_options['relationships']['reverse_field_category_node']['field'] = 'reverse_field_category_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']['label'] = '';
$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: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['relationship'] = 'reverse_field_category_node';
/* Field: Taxonomy term: Name */
$handler->display->display_options['fields']['name_1']['id'] = 'name_1';
$handler->display->display_options['fields']['name_1']['table'] = 'taxonomy_term_data';
$handler->display->display_options['fields']['name_1']['field'] = 'name';
/* Sort criterion: Content: Title */
$handler->display->display_options['sorts']['title']['id'] = 'title';
$handler->display->display_options['sorts']['title']['table'] = 'node';
$handler->display->display_options['sorts']['title']['field'] = 'title';
$handler->display->display_options['sorts']['title']['relationship'] = 'reverse_field_category_node';
/* Sort criterion: Taxonomy term: Name */
$handler->display->display_options['sorts']['name']['id'] = 'name';
$handler->display->display_options['sorts']['name']['table'] = 'taxonomy_term_data';
$handler->display->display_options['sorts']['name']['field'] = 'name';
/* 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(
  'category' => 'category',
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'nestedexample';

Solution 3: Another solution is adding a relationshipt with the taxonomy terms "Content: Taxonomy terms on node" and use the taxonomy term from the relationship as group by field. I hope this will help you.

12
  • See the note at the beginning of the question. Also the aggregation (solution 1) is being used and does not work as expected.
    – Cesar
    Aug 18, 2016 at 9:22
  • Would you like to export your view? I've just created a demo D7 website and the aggregation works as expected.
    – FiNeX
    Aug 18, 2016 at 9:56
  • I have just realized the problem. I have created the View from scratch and it works. Just see with attention the first image. If you realize what the problem is, you can take the bounty if you provide the correct answer.
    – Cesar
    Aug 18, 2016 at 9:58
  • Well, the sort criteria would interfere with the grouping and ordering. If you don't use the aggregation the view displays the same node duplicated on the same group of categories. Without ordering you see the group with the two terms but the node is not duplicated. When you enable the aggregation the problem is solved.
    – FiNeX
    Aug 18, 2016 at 12:29
  • It has nothing to do with it. It is much more evident. It is even highlighted in a photosoped red box. I cannot believe I didn't see by myself from the beginning. :)
    – Cesar
    Aug 18, 2016 at 12:32
1

Since the base table of view should not be changed, the required grouping can be achieved by disabling the Multiple field settings value.

  • For the taxonomy reference field, disable "display all values in same row" enter image description here

Now when grouped by taxonomy term, it should list the items in expected order enter image description here

3
  • From the bottom yellow box: "The solution should not change the main Views properties, mainly that it is a View of content nodes."
    – Raf
    Aug 18, 2016 at 9:21
  • Please see the note at the beginning of the question. Exactly, #Raf.
    – Cesar
    Aug 18, 2016 at 9:23
  • Updated the solution. Hope this helps
    – ajp
    Aug 18, 2016 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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