Disable menu items created by taxonomy_menu module.

Public

Warning message

We recommend you to assign at least one category to this snippet so you could easily navigate it in the future. Edit your snippet here.

Checks a field on taxonomy terms to determine if menu item should be disabled. Requires that you create a boolean field on the taxonomy term called field_enabled and that your menu path is using the default taxonomy/term/tid syntax.

Get raw version
php
  1. /**
  2.  * Implements hook_menu_link_alter().
  3.  */
  4. function MYMODULE_menu_link_alter(&$item) {
  5.  
  6. // Make sure the taxonomy_menu created this item.
  7. if ($item['module'] == 'taxonomy_menu') {
  8.  
  9. // Fetch the term id from the link path value.
  10. preg_match('/([\d]+)$/', $item['link_path'], $matches);
  11.  
  12. if (!empty($matches[0])) {
  13. $term = taxonomy_term_load($matches[0]);
  14. $term_wrapper = entity_metadata_wrapper('taxonomy_term', $matches[0]);
  15.  
  16. // Make sure the field has a value.
  17. if (!empty($term->field_enabled)) {
  18. $enabled = $term_wrapper->field_enabled->value();
  19.  
  20. // Hide/disable the menu item if the taxonomy term is disabled.
  21. if ($enabled == 0) {
  22. $item['hidden'] = TRUE;
  23. }
  24. }
  25. }
  26. }
  27. }