5

I'm wondering if there is a way to show a different view (or view part) based on the current depth of the term level?

Example I have a bunch of categories and subcategories. For the categories I want to show a page that is just the name of the category, once I go in to the next level (subcategories) I want the name and the image and description or whatever you have.

Is there anyway to do this? I have a page view that is mapped to category/% so we would get category/% ** CATEGORY */% * SUB CATEGORY **

  • You need to use contextual filter of views for this – subhojit777 Jan 16 '13 at 6:37
  • The code I've posed in drupal.org/node/1446626 for identifying term depth should be of interest here. – Letharion Jan 16 '13 at 9:01
  • I have solved exactly the same problem here. – Jack-PL Nov 9 '13 at 20:04
1

if you know the depth of the category in advance you could create different view page for each depth of the category.

For example for the main category the path would be category/% and for the subcategory the path would be category/%/%

this way when there is 1 argument after category the view page for the main category would be displayed. If there are two arguments the view page for the sub category would be displayed.

1

This is possible with a combination of

The trick is to have a different view mode per taxonomy depth.
You can then use views_content + ds dynamic fields to have dedicated views show up on each view mode. This is covered in depth in other questions on drupal.stackexchange.com and drupal.org, so I'm not going to repeat it here. Instead I am going to focus on the entityaspect stuff.

Custom module code:

/**
 * Implements hook_entityaspect()
 *
 * @param entityaspect_InjectedAPI_hookEntityAspect $api
 */
function e4btcmod_entityaspect($api) {

  $api->type('taxonomy_term')
    // Create dedicated view mode for top-level term pages.
    // (other terms can use the "full" view mode)
    ->viewMode('term_toplevel_page', 'Top-level term page')
    // Let entityaspect control "taxonomy/term/%"
    ->hijackPage('taxonomy/term/%')
  ;
}

/**
 * Implements hook_entityaspect_page__ENTITYTYPE()
 * This is responsible for
 * - Deciding what to show on the page, if it is being visited.
 * - Setting a title for the page and for the link.
 * - Deciding whether this is a tab, the tab weight, etc.
 * - Access checking:
 *   If you tell entityaspect to show something specific on the page, 
 *
 * @param entityaspect_InjectedAPI_hookEntityAspectPage $api
 * @param stdClass $term
 * @param string $route
 */
function MYMODULE_entityaspect_page__taxonomy_term($api, $term, $route) {

  // Only act on taxonomy/term/% route.
  if ('taxonomy/term/%' !== $route) {
    return;
  }

  // Access check.
  // @todo: I don't remember atm if entityaspect already does a basic entity view access check.
  if (!does_the_user_have_access($term)) {
    // In case of no access, we simply return and do nothing.
    return;
  }

  // The term name should be the page and link title.
  $api->title($term->name);

  switch ($term->vocabulary_machine_name) {

    case 'my_vocab':
      // Determine if it is a top-level term.
      $parent_terms = taxonomy_get_parents($term->tid);
      if (empty($parent_terms)) {
        // Use a dedicated view mode for top-level terms in cnet_categories.
        $api->viewMode('term_toplevel_page');
        return;
      }
      // Fall through to "default" case.

    default;
      // Terms of other vocabularies, and deeper-level terms of my_vocab.
      // Let taxonomy_display handle the output.
      $api->pageCallback('taxonomy_display_taxonomy_term_page', array($term));
      return;
  }
}
0

You can use a contextual filter / relationship to achieve this, no coding or additional modules needed.

Only show parent terms in view:

  • Add a contextual filter (Taxonomy term: Parent term)
  • In the contextual filter settings select "Provide default value" and enter "0" for Fixed value

Only show child terms in view:

  • Add a relationship (Taxonomy term: Parent term)
  • Enable "Require this relationship"

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.