drupal taxonomy based menu


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. /*Change menu*/
  2.  
  3. /**
  4. * Override menu links output
  5. * NOTE: change function name to match theme name
  6. **/
  7.  
  8.  
  9. /*CHANGED Created body id based on section vocabulary */
  10.  
  11.  
  12. //TODO Add is_front
  13.  
  14. function _phptemplate_variables($hook, $vars) {
  15. switch ($hook) {
  16. case 'page':
  17.  
  18. /*need to check for taxonomy or admin system flips out*/
  19.  
  20. /* Get the taxonomy info from the current node while in the page */
  21. $node_taxonomy_object = $vars['node']->taxonomy;
  22. if ($node_taxonomy_object) {
  23. foreach ($node_taxonomy_object as $tax_object) {
  24. $vid = $tax_object->vid;
  25.  
  26. switch($tax_object->vid){
  27.  
  28. case 14:
  29. #works for primary navigation like mysite.com/about/
  30.  
  31. if (!arg(3) && !arg(2) ) {
  32. $id = $vars['node']->nid;
  33. $terms = taxonomy_node_get_terms_by_vocabulary($id, $vid);
  34.  
  35. foreach ($terms as $term) {
  36. $termID = $term->tid;
  37. $body_id = $term->name;
  38. }
  39.  
  40. $parentCheck = taxonomy_get_parents($termID);
  41. if ($parentCheck == NULL){
  42. $vars['body_class'] = 'noselect';
  43. }
  44.  
  45. $body_id = strtolower($body_id);
  46. $vars['body_id'] = str_replace(" ","-",$body_id);
  47.  
  48.  
  49.  
  50. }
  51.  
  52. #Works for second-level navigation like mysite.com/about/mission/
  53. if (!arg(3)) {
  54. $id = $vars['node']->nid;
  55. $terms = taxonomy_node_get_terms_by_vocabulary($id, $vid);
  56.  
  57. foreach ($terms as $term) {
  58. $termID = $term->tid;
  59. $body_class = $term->name;
  60. }
  61.  
  62.  
  63. //get parents
  64. $parents = taxonomy_get_parents($termID);
  65.  
  66. foreach ($parents as $parent){
  67. $body_id=$parent->name;
  68. }
  69.  
  70. $body_class = strtolower($body_class);
  71. $vars['body_class'] = str_replace(" ","-",$body_class);
  72.  
  73. $body_id = strtolower($body_id);
  74. $vars['body_id'] = str_replace(" ","-",$body_id);
  75.  
  76.  
  77. #account for having no parents, really redundant, needs to be shortened.
  78. $parentCheck = taxonomy_get_parents($termID);
  79. if ($parentCheck == NULL){
  80. $vars['body_class'] = 'noselect';
  81. }
  82.  
  83.  
  84. }
  85.  
  86. # No third-level navigation on static pages
  87.  
  88.  
  89. break;
  90.  
  91. #designed for non-static pages, pages not set as "Pages"
  92.  
  93. default:
  94. #works for primary navigation like mysite.com/about/
  95.  
  96.  
  97. $vocab = taxonomy_get_vocabulary($vid);
  98.  
  99. $body_id = $vocab->name;
  100.  
  101.  
  102. $body_id = strtolower($body_id);
  103. $vars['body_id'] = str_replace(" ","-",$body_id);
  104.  
  105.  
  106. if (!arg(3) && !arg(2)) {
  107. $vars['body_class'] = 'noselect';
  108. }
  109.  
  110.  
  111. # Works for second-level navigation like mysite.com/about/mission/
  112. if (!arg(3)) {
  113. $id = $vars['node']->nid;
  114. $terms = taxonomy_node_get_terms_by_vocabulary($id, $vid);
  115.  
  116. foreach ($terms as $term) {
  117. $termID = $term->tid;
  118. $body_class = $term->name;
  119. }
  120.  
  121. $body_class = strtolower($body_class);
  122. $vars['body_class'] = str_replace(" ","-",$body_class);
  123.  
  124. }
  125.  
  126.  
  127. # Works for third-level navigation like mysite.com/about/mission/article.html
  128. if (arg(3)) {
  129. $id = $vars['node']->nid;
  130. $terms = taxonomy_node_get_terms_by_vocabularies($id, $vid);
  131.  
  132. foreach ($terms as $term) {
  133. $termID = $term->tid;
  134. $body_class = $term->name;
  135. }
  136.  
  137.  
  138. $body_class = strtolower($body_class);
  139. $vars['body_class'] = str_replace(" ","-",$body_class);
  140. //echo "Body Class =" . $body_class;
  141.  
  142.  
  143. }
  144.  
  145. }
  146. }
  147. }
  148. return $vars;
  149. }
  150. }
  151.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.