1

I need to override by views my hierarchical taxonomy term paths:

www.example.com/[vocabulary name]
www.example.com/[vocabulary name]/term1
www.example.com/[vocabulary name]/term1/term2 
www.example.com/[vocabulary name]/term1/term2/term3

Where

term1 has depth = 0
term2 has depth = 1
term3 has depth = 2

Vocabulary terms:

+ term1
-- + term2
---- + term3
---- + term3.a
---- + term3.b
-- + term2.a
-- + term2.b

+ term1.a
-- + term2.c
-- + term2.d
-- + term2.e

Paths have been configured by Pathauto

If the path leads to term with depth = 0 I'm using view display "Page".

If the path leads to term with depth = 1 I'm using view display "Page - category".

If the path leads to term with depth = 2 I'm using view display "Page - subcategory".

I tried to use Taxonomy Views Integrator to override the paths by Views, and it's working, but there is one disadvantage in this solution:

If I add a new content and create a new term (with the help of the Simple hierarchical select module ) then I'll be forced to edit this term and select the appropriate view to override this term path. It might be very time consuming with many terms, so I'm looking for another solution.

How to set a view display for a specific term depth?

  • Can't you just clone the builtin Views taxonomy override view? Do you need different Views per taxonomy? Or by hierarchical depth? example.com/admin/structure/views/view/taxonomy_term/edit – Queenvictoria Aug 17 '13 at 7:00
  • The question is how to build different views per vocabulary with hierarchical terms. Not sure what do you mean by builtin Views taxonomy override view – Jack-PL Aug 17 '13 at 10:52
  • You must decide with argument from the path will do the filtering. So, if that path is "www.example.com/[vocabulary name]/term1/term2/term3" you will show results from which term (or vocabulary)? If you decide that an override of the views taxonomy term page is fine as mentioned before. – TheodorosPloumis Aug 18 '13 at 12:55
  • Can't decide which argument do the filtering, because for each view there is only one argument. Have a look at my answer here this question is based on it. – Jack-PL Aug 18 '13 at 19:30
  • Similar question here, drupal.stackexchange.com/questions/56963/… – donquixote Nov 14 '13 at 18:16
1
+100

I think you are making a little mistake that confuses you.

TVI does not allow to define a view for the vocabulary itself so the path www.example.com/[vocabulary name] cannot be handled by the module. The vocabulary settings are the default ones for all terms within the vocabulary. These settings can be overriden for each term and can be inherited with the latest version. If you want a specific view for your entire vocabulary, you will have to create it by yourself because no vocabulary page exists by default in Drupal.

If I get your problem described in this post and on drupal.org (https://drupal.org/node/2066015) I can suggest you the following :

  • Define view1 as a page display and give it the path related to your vocabulary's name
  • Define view2, view3 dans view4 using any path of your choice, it does not matter as TVI will call them directly
  • Define view2 as the vocabulary's default view
  • Define view3 and view4 as term's specific settings if needed

When trying to show a term page, TVI will do the following :

  • If the term has TVI settings, use it
  • Else find its first parent having a TVI setting with the "override" option and use it
  • If no parent is found, use the vocabulary's TVI settings

If I take your drupal.org sample you will have :

Terms

Vocabulary (view2)
T 1 (no settings) 
    T 1.1 (view 3, not inheritable)
        T 1.1.1 (no settings)
    T 1.2 (view 4, inheritable)
        T 1.2.1 (no settings)
    T 1.3 (no settings)
T 2 (no settings) 

Results

T 1 (view 2 from vocabulary)
    T 1.1 (view 3 from itself)
        T 1.1.1 (view 2 from vocabulary)
    T 1.2 (view 4 from itself)
        T 1.2.1 (view 4 from T 1.2)
    T 1.3 (view 2 from vocabulary)
T 2 (view 2 from vocabulary)

I hope this will help you to achieve what you want to do.

  • In your results T1, T2 have the same default view - View 2 - it's ok, this is what I need, but if I would like to set another default view, let's say view 3 for T 1.1, T 1.2 and T 1.3, then I won't be able do that without changeing their parent default view (if I do that, I'll be forced to define each next parent separately, which might be very time consuming). Thank you anyway for your answer, because now, sadly, I'm sure it can't be done with TVI module. – Jack-PL Aug 20 '13 at 0:12
1

I've found the solution by myself. It works without the TVI module.

Useful, but not required modules: Taxonomy Menu Block

This module allows to create a menu out of your taxonomies and comes with its own theme function and a few hooks (see the .api.php file) that allows you to alter the generated data to your needs. One of these hooks will be used by this solution.

In genereal there are four steps of this solution:

  1. Install the Taxonomy Menu block module if needed.
  2. Change the taxonomy term path of specific terms depending on their depth.
  3. Create appropriate aliases or update them if they are already exist.
  4. Set the new taxonomy term path for a specific view display.

Step by step

Install the Taxonomy Menu Block and go to: admin/structure/block/add-taxonomy-menu-block, do some settings and save it.

Change the taxonomy term path of specific terms depending on their depth.

Here I'm looking for parents only. If they're exist, that means the term is a child. Otherwise, it is a parent.

function YOUR_MODULE_entity_info_alter(&$entity_info) {
  $entity_info['taxonomy_term']['uri callback'] = 'YOUR_MODULE_taxonomy_term_uri';
}

function YOUR_MODULE_taxonomy_term_uri($term) {
  switch ($term->vocabulary_machine_name) { 
    case 'YOUR_VOCABULARY_MASHINE_NAME':
      $parents = taxonomy_get_parents($term->tid);
      if (empty($parents)) {
        return array(
          'path' => 'taxonomy/category/' . $term->tid,
        );
      }
      else {
        return array(
          'path' => 'taxonomy/subcategory/' . $term->tid,
        );    
      }
    break;
  }
  // default taxonomy term uri
  return taxonomy_term_uri($term);
}

Create appropriate aliases or update them if they are already exist.

Go to: admin/config/search/path/patterns and set the pattern for all your vocabulary paths: [term:vocabulary]/[term:parents:join-path]/[term:name]

Important: If you already had pathauto running before having this issue you must rebuild the aliases by going to the delete aliases tab and deleting the old aliases for "Taxonomy term paths", then go to the bulk update tab and updating the "Taxonomy term paths".

Alter taxonomy menu block links

Unfortunatelly, if we change the default taxonomy term path, then neither menu created by the Taxonomy menu nor the Taxonomy Menu Block (TMB) module won't be able to replace system paths by their aliases, but luckily, the TMB allows us to alter the links to our needs:

function YOUR_MODULE_taxonomy_menu_block_tree_alter(&$tree, $vid) {

  $vocabulary = taxonomy_vocabulary_load($vid);
  if ($vocabulary->name == 'YOUR_VOCABULARY_MASHINE_NAME') {
    foreach ($tree as $tid => $term) {
      switch ($term['depth']) {
        case 0 :
          $tree[$tid]['path'] = drupal_get_path_alias('taxonomy/category/' . $tid);
          break;
        case 1 :
          $tree[$tid]['path'] = drupal_get_path_alias('taxonomy/subcategory/' . $tid);
          break; 
      }
    }
  }
}

View page settings

In order to use different view display for a specific term depth you need to set the appropriate path under "Page Settings" for each display. For example:

View displays:

Main category page -> path: taxonomy/category/%

Subcategory page -> path: taxonomy/subcategory/%

Notice: you can't use taxonomy/term/% path any more, because now these specific terms have their own paths.

That's it!

In this solution both parent and child terms have their own view displays, so now you can add some new content and associate new terms to it using the simple hierarchical select only.

0

Try these short workaround called Taxonomy Views

Create a module with the following files.

taxonomy_views.info:

name = Taxonomy Views
description = It gives links like taxonomy/[vocabulary machine name]/[term id] to taxonomy term pages, so you can set up many Views for taxonomy pages. You can create two Views with paths "taxonomy/products/%" and "taxonomy/news/%" and enable vocabulary verification for each View, for example.
core = 7.x

dependencies[] = taxonomy
dependencies[] = views

taxonomy_views.module:

<?php

/**
 * @file
 * Views for taxonomy pages.
 */

/**
 * Implements hook_menu().
 */
function taxonomy_views_menu() {
  $items['taxonomy/%taxonomy_vocabulary_machine_name/term/%taxonomy_term'] = array(
    'title' => 'Taxonomy term',
    'title callback' => 'taxonomy_term_title',
    'title arguments' => array(2),
    'page callback' => 'taxonomy_term_page',
    'page arguments' => array(2),
    'access arguments' => array('access content'),
    'file' => 'taxonomy.pages.inc',
    'file path' => drupal_get_path('module', 'taxonomy'),
  );
  return $items;
}

/**
 * Alter the entity info.
 *
 * Modules may implement this hook to alter the information that defines an
 * entity. All properties that are available in hook_entity_info() can be
 * altered here.
 *
 * @param array $entity_info
 *   The entity info array, keyed by entity name.
 *
 * @see hook_entity_info()
 */
function taxonomy_views_entity_info_alter(&$entity_info) {
  if (isset($entity_info['taxonomy_term'])) {
    $entity_info['taxonomy_term']['uri callback'] = 'taxonomy_views_term_uri';
  }
}

/**
 * Entity URI callback.
 */
function taxonomy_views_term_uri($term) {
  return array(
    'path' => 'taxonomy/' . $term->vocabulary_machine_name . '/term/' . $term->tid,
  );
}

Now you can create Views for paths like "taxonomy/[vocabulary-machine-name]/term/%" and set pathauto alias to [term:vocabulary]/[term:parents:join-path]/[term:name].

It's good, because you can set a View for a path taxonomy/products/term/% and enable vocabulary verification (check vocabulary is "products").

In addition, the old taxonomy term url is still available (taxonomy/term/%).

  • TVI module do the same, it solves the issue with the taxonomy/[vocabulary] path, but the problem is with the hierarchical terms (those with depth). – Jack-PL Aug 19 '13 at 23:10
  • What does you mean by issue with hierarchical terms? I use the code above with paths (have just hierarchical terms) I wrote and it works good. – deem Aug 20 '13 at 6:24
  • Each term has the same path. taxonomy/term/tid Your solution only change this path to another and has nothing to do with the depth of taxonomy terms. www.example.com/[vocabulary name]/[term name no matter what depth it has] it's NOT what I'm asking for. – Jack-PL Aug 20 '13 at 11:39
  • Why can't you use pathauto aliases for that? – deem Aug 20 '13 at 12:51
  • "Paths have been configured by Pathauto" read the question please. – Jack-PL Aug 20 '13 at 13:03
0

Creata a general Views for a taxonomy term display (it has nothing to do with the path, since the internal path is always "taxonomy/term/%tid". Pathauto paths have nothing to do with thhis) and use a views Filter to use it only if the term belongs to a Vocabulary or anything you prefer.

Then clone this Views and change the filter for another views display.

  • You have to set up one views for each path depth. So [voc]/%term1, [voc]/%1/%term2 will be 2 different views. Also, do not use the Contextual filters of taxonomy term depth, make it as a simple views. – TheodorosPloumis Aug 21 '13 at 11:40
0

my solution is redirect it to our own views path in hook_init

if my views path is category/% then something like below code help

function hook_init(){
    /// redirect to category
  if(preg_match('/taxonomy\/term\/([0-9]+)$/',current_path(),$matches)){
       drupal_goto('category/'.$matches[1]);
  }
}

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.