22 August 2012

Programmatically create menu link language relation in Drupal 7. This example takes a certain taxonomy vocabulary by id and creates language relations to menu links by terms relations. You might need it when you create custom view that needs menu link language relations.

Source code viewer
  1. // language relation
  2. $terms = taxonomy_get_tree(1);
  3. $translate_array = array();
  4. $mlids = db_query('SELECT tid, mlid FROM {taxonomy_menu} WHERE vid = 1')->fetchAllKeyed();
  5. foreach($terms as $term) {
  6. // get all engilsh terms
  7. if($term->language == 'en') {
  8. //get other languages
  9. $lang_terms = array('en' => $term->tid);
  10. foreach($terms as $term2) {
  11. if($term2->i18n_tsid == $term->i18n_tsid)
  12. $lang_terms[$term2->language] = $term2->tid;
  13. }
  14. //create language relations
  15. $translation_set = i18n_translation_set_create('menu_link');
  16. foreach ($lang_terms as $lang => $tid) {
  17. $item = menu_link_load($mlids[$tid]);
  18. $translation_set->add_item($item, $lang);
  19. $item['i18n_tsid'] = $translation_set->tsid;
  20. menu_link_save($item);
  21. }
  22. $translation_set->save(TRUE);
  23. }
  24. }
  25. menu_rebuild();
Programming Language: PHP