0

What i want: Active terms of vocabulary in main menu.

What i have done so far:

  1. I have used taxonomy_menu module.
  2. Created taxonomy(Category).
  3. Added "Status" field having boolean values (0 = Inactive, 1 = Active).
  4. Added two terms in Category(Cat1 and Cat2).
  5. Cat1 = Active, Cat2 = Inactive.
  6. Configured menu from "Edit vocabulary" section.

Problem: All terms of vocabulary are included in main menu. I want that only those terms whose Status = Active should be displayed in main menu.

Community, please help. Any hint would be helpful.

  • You can instead disable the terms from add terms – harshal Dec 27 '13 at 13:10
  • @harshal: by disable, do you mean that i can delete them? or something else – Bhavik Shah Dec 27 '13 at 13:19
1

You can do this by using hook on taxonomy insert and update inside a custom module.

Here is a sample of code (needs refactoring/cleaning/checking but that's a start and it works for me) :

function custom_module_taxonomy_menu_insert(&$item) {
  //load taxonomy to access fields
  $tax = taxonomy_term_load($item['tid']);
  //get your field display (here i called it field_status)
  $display = isset($tax->field_status['und'][0]['value'])?$tax->field_status['und'][0]['value']:'';
  //check your value
  if($display == 0) {
    //I don't want to display this element
    $item['remove'] = TRUE;
    return $item;
  }
}

function custom_module_taxonomy_menu_update(&$item) {
  //IDEM without comments
  $tax = taxonomy_term_load($item['tid']);
  $display = isset($tax->field_status['und'][0]['value'])?$tax->field_status['und'][0]['value']:'';
  if($display == 0) {
    $item['remove'] = TRUE;
    return $item;
  }
}

[Edit to add some details]

The menu will not update automatically, you'll have to save/update your terms to update the menu (hook_taxonomy_menu_update). For new terms, it's going to work automatically (hook_taxonomy_menu_insert).

  • +1 on the use of the hooks, not so sure on the logic involved if an item is sent in that doesn't contain the field in question, but definitely the way to go from an api standpoint. – Jimajamma Dec 27 '13 at 14:05

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.