Filter menu overview based on current language

Public

Get raw version
php
  1. /**
  2.  * Implements hook_form_FORM_ID_alter().
  3.  */
  4. function hook_form_menu_overview_form_alter(&$form, &$form_state) {
  5. global $language;
  6. // The goal here is to hide unwanted languages, so we will keep only menu
  7. // links which language is LANGUAGE_NONE or current language and also menu
  8. // links not translated in the current language.
  9.  
  10. foreach (element_children($form) as $element) {
  11. if (substr($element, 0, 5) == 'mlid:') {
  12. $item = $form[$element]['#item'];
  13.  
  14. if ($item['language'] != LANGUAGE_NONE && $item['language'] != $language->language && $item['i18n_tsid']) {
  15. // This menu link is not in our language, we check if we have a
  16. // translation in the current language
  17. $translations = i18n_translation_set_load($item['i18n_tsid'], 'menu_link')->load_translations();
  18. if ($translations && $translations[$language->language]) {
  19. // We have a translation, so we hide this menu link
  20. $form['disabled_languages'][$element] = $form[$element] + array('#access' => FALSE);
  21.  
  22. // On submit, synchronize weights with original language
  23. if (!empty($form_state['input'])) {
  24. $original_mlid = $translations[$language->language]['mlid'];
  25. $form_state['input'][$element]['weight'] = $form_state['input']['mlid:' . $original_mlid]['weight'];
  26. }
  27. }
  28. else {
  29. $form[$element]['#attributes']['class'][] = 'menu-language-unavailable';
  30. }
  31. unset($form[$element]);
  32. }
  33. // We add some classes to make this more visual for the contributor
  34. elseif ($item['language'] == LANGUAGE_NONE) {
  35. $form[$element]['#attributes']['class'][] = 'menu-language-none';
  36. }
  37. elseif ($item['language'] != $language->language || !$item['i18n_tsid']) {
  38. $form[$element]['#attributes']['class'][] = 'menu-language-unavailable';
  39. }
  40. else {
  41. $form[$element]['#attributes']['class'][] = 'menu-language-current';
  42. }
  43. }
  44. }
  45. $form['#prefix'] = t('<p>This form is filtered and shows only links in the <a class="menu-language-current">current language</a>, or having <a class="menu-language-none">no language</a> or having <a class="menu-language-unavailable">no translation in the current language</a>.</p>');
  46. }