Change taxonomy term path.

Public

Each term has the same path: taxonomy / term / tid. If you want to use different views, you have to use additional modules to change the path, or insert a few lines of code to your module or theme:

</> CopyGet raw version
php
  1. function MY_MODULE_entity_info_alter(&$entity_info) {
  2. $entity_info['taxonomy_term']['uri callback'] = 'MY_MODULE_taxonomy_term_uri';
  3. }
  4.  
  5. function MY_MODULE_taxonomy_term_uri($term) {
  6. switch ($term->vocabulary_machine_name) {
  7.  
  8. case 'vocabulary_one':
  9. return array(
  10. 'path' => 'vocabulary-one/' . $term->tid,
  11. );
  12. break;
  13.  
  14. case 'vocabulary_two':
  15. return array(
  16. 'path' => 'vocabulary-two/' . $term->tid,
  17. );
  18. break;
  19.  
  20. default:
  21. return array(
  22. 'path' => 'taxonomy/term/' . $term->tid,
  23. );
  24. break;
  25. }
  26. }

Comments

timonweb's picture

Cool, but shouldn't you assign your callback in hook_entity_info_alter() at first?
Like this:

  1. function MODULE_entity_info_alter(&$entity_info) {
  2. $entity_info['taxonomy_term']['uri callback'] = 'MODULE_taxonomy_term_uri';
  3. }
malcolm's picture

Hi timonweb.
No, it is not necessary. See for yourself :P

malcolm's picture

timonweb, you are absolutely right! I apologize for this mistake, I updated the snippet, thanks.

caschbre's picture

If we have a module that only wants to modify a single vocabulary path, does this solution work? It looks like this completely overtakes the taxonomy_term callback.

malcolm's picture

Yes, I am using this to my custom forum. TVI module do the same. You need this only if you want different views for different vocabulary, or have a custom menu callback and you want to pass to it the term arguments (tid).