0

I'm working on a website which uses a single vocabulary for three different content types. I'm looking for a way to group list items by content type.

To illustrate, I got three content types:

  • Manual
  • News
  • Blog

And I got lots of vocabulary terms which might be used by all three content types:

  • Rabbits
  • Cats
  • Dogs
  • Frogs

When I select a vocabulary term it will show all the relevant content, sorted by the publication date, like so:

Rabbits

  • Rabbit escaped from show-ground
  • Feeding your rabbit
  • Why do rabbits have such big ears?

However, I'd like to group the items by content type:

Rabbits

Manual

  • Feeding your rabbit

News

  • Rabbit escaped from show-ground

Blog

  • Why do rabbits have such big ears?

I've tried the default 'Taxonomy term' view but found it wouldn't quite work because it affect other vocabularies as well. I also tried the Taxonomy Display module but didn't really understand how it works (I got some configuration options but no view I could change).

Is there a function that could check the vocabulary and the list items' content types and then group them by content type?

1

I assume you know how to set up the view and have difficulty only with applying the view to the specific taxonomy and letting other taxonomies render by default.

Create a custom module and put this is the main module file:

function MODULE_menu_alter(&$items){
  // change taxonomy view path for certain vocabularies
  $items['taxonomy/term/%taxonomy_term']['page arguments'][] = $items['taxonomy/term/%taxonomy_term']['page callback'];
  $items['taxonomy/term/%taxonomy_term']['page callback'] = 'custom_taxonomy_term_callback';
}

function custom_taxonomy_term_callback($term, $callback) {
  if ($term->vocabulary_machine_name == "SOME_TAXONOMY") {
    $view = views_get_view('CUSTOM_TERM_VIEW');
    $view->set_display('default');
    $view->set_arguments(array($term->tid));
    $view->pre_execute();
    $view->execute();
    return $view->render();
  }
  else {
    module_load_include('inc', 'taxonomy', 'taxonomy.pages');
    return $callback($term);
  }  
}

This is what the code does:

It alters the path taxonomy is called on, then checks if the path is on your taxonomy that you want to special render. If it is it loads and renders the view for that taxonomy, and if not it passes the call back to the default taxonomy callback.

You need to alter the code to suit your taxonomy machine name and view name. And remember to clear the cache.

For your view you can create just the master view (ie no page with path) that accepts the tid as contextual filter. This view is custom rendered and therefore does not need to be on any path.

  • Many thanks - that worked. Maybe for the benefit of others struggling with this, setting up the custom view is as easy as cloning the standard Taxonomy term view (disabled by default) and then changing the Path to the page you use to display vocabulary terms (in my case '/labels/%') and selecting the vocabulary you want to display under Advanced, Content: Has taxonomy term ID (with depth). After that you can choose which fields you want to display, group them etc. – rkhff Jun 17 '15 at 20:24

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.