i18n_menu.module 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. <?php
  2. /**
  3. * @file
  4. * Internationalization (i18n) submodule: Menu translation.
  5. *
  6. * @author Jose A. Reyero, 2005
  7. *
  8. */
  9. /**
  10. * Implements hook_menu()
  11. */
  12. function i18n_menu_menu() {
  13. $items['admin/structure/menu/manage/translation'] = array(
  14. 'title' => 'Translation sets',
  15. 'page callback' => 'i18n_translation_set_list_manage',
  16. 'page arguments' => array('menu_link'),
  17. 'access arguments' => array('administer menu'),
  18. 'type' => MENU_LOCAL_TASK,
  19. 'weight' => 10,
  20. );
  21. $items['admin/structure/menu/manage/translation/add'] = array(
  22. 'title' => 'Add translation',
  23. 'page callback' => 'drupal_get_form',
  24. 'page arguments' => array('i18n_menu_translation_form'),
  25. 'access arguments' => array('administer menu'),
  26. 'type' => MENU_LOCAL_ACTION,
  27. 'file' => 'i18n_menu.admin.inc',
  28. );
  29. $items['admin/structure/menu/manage/translation/edit/%i18n_menu_translation'] = array(
  30. 'title' => 'Edit translation',
  31. 'page callback' => 'drupal_get_form',
  32. 'page arguments' => array('i18n_menu_translation_form', 6),
  33. 'access arguments' => array('administer menu'),
  34. 'type' => MENU_CALLBACK,
  35. 'file' => 'i18n_menu.admin.inc',
  36. );
  37. $items['admin/structure/menu/manage/translation/delete/%i18n_menu_translation'] = array(
  38. 'title' => 'Delete translation',
  39. 'page callback' => 'drupal_get_form',
  40. 'page arguments' => array('i18n_translation_set_delete_confirm', 6),
  41. 'access arguments' => array('administer menu'),
  42. 'type' => MENU_CALLBACK,
  43. );
  44. return $items;
  45. }
  46. /**
  47. * Implements hook_menu_alter()
  48. */
  49. function i18n_menu_menu_alter(&$items) {
  50. $items['admin/structure/menu/item/%menu_link'] = $items['admin/structure/menu/item/%menu_link/edit'];
  51. $items['admin/structure/menu/item/%menu_link']['type'] = MENU_CALLBACK;
  52. $items['admin/structure/menu/item/%menu_link/edit']['type'] = MENU_DEFAULT_LOCAL_TASK;
  53. }
  54. /**
  55. * Implements hook_block_view().
  56. */
  57. function i18n_menu_block_view_alter(&$data, $block) {
  58. if (($block->module == 'menu' || $block->module == 'system') && (i18n_menu_mode($block->delta) & I18N_MODE_MULTIPLE)) {
  59. $menus = menu_get_menus();
  60. if (isset($menus[$block->delta])) {
  61. if (empty($block->title)) {
  62. $data['subject'] = i18n_string_plain(
  63. array('menu', 'menu', $block->delta, 'title'),
  64. $menus[$block->delta]
  65. );
  66. }
  67. // Add contextual links for this block.
  68. if (!empty($data['content'])) {
  69. $data['content']['#contextual_links']['menu'] = array('admin/structure/menu/manage', array($block->delta));
  70. }
  71. }
  72. }
  73. }
  74. /**
  75. * Implements hook_i18n_translate_path()
  76. */
  77. function i18n_menu_i18n_translate_path($path) {
  78. $item = i18n_menu_link_load($path, i18n_langcode());
  79. if ($item && ($set = i18n_translation_object('menu_link', $item))) {
  80. $links = array();
  81. foreach ($set->get_translations() as $lang => $link) {
  82. $links[$lang] = array(
  83. 'href' => $link['link_path'],
  84. 'title' => $link['link_title'],
  85. 'i18n_type' => 'menu_link',
  86. 'i18n_object' => $link,
  87. );
  88. }
  89. return $links;
  90. }
  91. }
  92. /**
  93. * Implements hook_menu_insert()
  94. */
  95. function i18n_menu_menu_insert($menu) {
  96. i18n_menu_menu_update($menu);
  97. }
  98. /**
  99. * Implements hook_menu_update()
  100. */
  101. function i18n_menu_menu_update($menu) {
  102. // Stores the fields of menu links which need an update.
  103. $update = array();
  104. if (!isset($menu['i18n_mode'])) {
  105. $menu['i18n_mode'] = I18N_MODE_NONE;
  106. }
  107. if (!($menu['i18n_mode'] & I18N_MODE_LANGUAGE)) {
  108. $menu['language'] = LANGUAGE_NONE;
  109. }
  110. db_update('menu_custom')
  111. ->fields(array('language' => $menu['language'], 'i18n_mode' => $menu['i18n_mode']))
  112. ->condition('menu_name', $menu['menu_name'])
  113. ->execute();
  114. if (!$menu['i18n_mode']) {
  115. $update['language'] = LANGUAGE_NONE;
  116. }
  117. elseif ($menu['i18n_mode'] & I18N_MODE_LANGUAGE) {
  118. $update['language'] = $menu['language'];
  119. }
  120. // Non translatable menu.
  121. if (!($menu['i18n_mode'] & I18N_MODE_TRANSLATE)) {
  122. $tsids = db_select('menu_links')
  123. ->fields('menu_links', array('i18n_tsid'))
  124. ->groupBy('i18n_tsid')
  125. ->condition('menu_name', $menu['menu_name'])
  126. ->condition('customized', 1)
  127. ->condition('i18n_tsid', 0, '<>')
  128. ->execute()
  129. ->fetchCol(0);
  130. if (!empty($tsids)) {
  131. foreach ($tsids as $tsid) {
  132. if ($translation_set = i18n_translation_set_load($tsid)) {
  133. $translation_set->delete();
  134. }
  135. }
  136. }
  137. $update['i18n_tsid'] = 0;
  138. }
  139. if (!empty($update)) {
  140. db_update('menu_links')
  141. ->fields($update)
  142. ->condition('menu_name', $menu['menu_name'])
  143. ->condition('customized', 1)
  144. ->execute();
  145. }
  146. // Update strings, always add translation if no language
  147. if (!i18n_object_langcode($menu)) {
  148. i18n_string_object_update('menu', $menu);
  149. }
  150. // Clear all menu caches.
  151. menu_cache_clear_all();
  152. }
  153. /**
  154. * Implements hook_menu_delete()
  155. */
  156. function i18n_menu_menu_delete($menu) {
  157. i18n_string_object_remove('menu', $menu);
  158. }
  159. /**
  160. * Implements hook_menu_link_alter().
  161. *
  162. * This function is invoked from menu_link_save() before default
  163. * menu link options (menu_name, module, etc.. have been set)
  164. */
  165. function i18n_menu_menu_link_alter(&$item) {
  166. // We just make sure every link has a valid language property.
  167. if (!i18n_object_langcode($item)) {
  168. $item['language'] = LANGUAGE_NONE;
  169. }
  170. }
  171. /**
  172. * Implements hook_menu_link_insert()
  173. */
  174. function i18n_menu_menu_link_insert($link) {
  175. i18n_menu_menu_link_update($link);
  176. }
  177. /**
  178. * Implements hook_menu_link_update().
  179. */
  180. function i18n_menu_menu_link_update($link) {
  181. // Stores the fields to update.
  182. $fields = array();
  183. $menu_mode = i18n_menu_mode($link['menu_name']);
  184. if ($menu_mode & I18N_MODE_TRANSLATE && isset($link['language'])) {
  185. // Multilingual menu links, translatable, it may be part of a
  186. // translation set.
  187. if (i18n_object_langcode($link)) {
  188. if (!empty($link['translation_set'])) {
  189. // Translation set comes as parameter, we may be creating a translation,
  190. // add link to the set.
  191. $translation_set = $link['translation_set'];
  192. $translation_set
  193. ->add_item($link)
  194. ->save(TRUE);
  195. }
  196. }
  197. elseif ($link['language'] === LANGUAGE_NONE && !empty($link['original_item']['i18n_tsid'])) {
  198. if ($translation_set = i18n_translation_set_load($link['original_item']['i18n_tsid'])) {
  199. $translation_set->remove_language($link['original_item']['language']);
  200. // If there are no links left in this translation set, delete the set.
  201. // Otherwise update the set.
  202. $translation_set->update_delete();
  203. }
  204. $fields['i18n_tsid'] = 0;
  205. }
  206. }
  207. // For multilingual menu items, always set a language and mark them for
  208. // 'alter' so they can be processed later by
  209. // hook_translated_link_menu_alter().
  210. if ($menu_mode) {
  211. if (!isset($link['language'])) {
  212. $link['language'] = LANGUAGE_NONE;
  213. }
  214. if (_i18n_menu_link_check_alter($link) && empty($link['options']['alter'])) {
  215. $fields['options'] = $link['options'];
  216. $fields['options']['alter'] = TRUE;
  217. }
  218. // We cannot unmark links for altering because we don't know what other
  219. // modules use it for.
  220. }
  221. // Update language field if the link has a language value.
  222. if (isset($link['language'])) {
  223. $fields['language'] = $link['language'];
  224. }
  225. if (!empty($fields)) {
  226. // If link options are to be updated, they need to be serialized.
  227. if (isset($fields['options'])) {
  228. $fields['options'] = serialize($fields['options']);
  229. }
  230. db_update('menu_links')
  231. ->fields($fields)
  232. ->condition('mlid', $link['mlid'])
  233. ->execute();
  234. }
  235. // Update translatable strings if any for customized links that belong to a
  236. // localizable menu.
  237. if (_i18n_menu_link_is_localizable($link)) {
  238. i18n_string_object_update('menu_link', $link);
  239. }
  240. else {
  241. i18n_string_object_remove('menu_link', $link);
  242. }
  243. }
  244. /**
  245. * Implements hook_menu_delete()
  246. */
  247. function i18n_menu_menu_link_delete($link) {
  248. // If a translation set exists for this link, remove this link from the set.
  249. if (!empty($link['i18n_tsid'])) {
  250. if ($translation_set = i18n_translation_set_load($link['i18n_tsid'])) {
  251. $translation_set->get_translations();
  252. $translation_set->remove_language($link['language']);
  253. // If there are no links left in this translation set, delete the set.
  254. // Otherwise update the set.
  255. $translation_set->update_delete();
  256. }
  257. }
  258. i18n_string_object_remove('menu_link', $link);
  259. }
  260. /**
  261. * Get menu mode or compare with given one
  262. */
  263. function i18n_menu_mode($name, $mode = NULL) {
  264. $menu = menu_load($name);
  265. if (!$menu || !isset($menu['i18n_mode'])) {
  266. return isset($mode) ? FALSE : I18N_MODE_NONE;
  267. }
  268. else {
  269. return isset($mode) ? $menu['i18n_mode'] & $mode : $menu['i18n_mode'];
  270. }
  271. }
  272. /**
  273. * Implements hook_translated_menu_link_alter().
  274. *
  275. * Translate localizable menu links on the fly.
  276. * Filter out items that have a different language from current interface.
  277. *
  278. * @see i18n_menu_menu_link_alter()
  279. */
  280. function i18n_menu_translated_menu_link_alter(&$item) {
  281. // Only process links to be displayed not processed before by i18n_menu.
  282. if (_i18n_menu_link_process($item)) {
  283. if (!_i18n_menu_link_is_visible($item)) {
  284. $item['hidden'] = TRUE;
  285. }
  286. elseif (_i18n_menu_link_is_localizable($item)) {
  287. // Item has undefined language, it is a candidate for localization.
  288. _i18n_menu_link_localize($item);
  289. }
  290. }
  291. }
  292. /**
  293. * Implements hook_help().
  294. */
  295. function i18n_menu_help($path, $arg) {
  296. switch ($path) {
  297. case 'admin/help#i18n_menu' :
  298. $output = '<p>' . t('This module adds support for multilingual menus. You can setup multilingual options for each menu:') . '</p>';
  299. $output .= '<ul>';
  300. $output .= '<li>' . t('Menus can be fully multilingual with translatable (or localized) menu items.') . '</li>';
  301. $output .= '<li>' . t('Menus can be configured to have a fixed language. All menu items share this language setting and the menu will be visible in that language only.') . '</li>';
  302. $output .= '<li>' . t('Menus can also be configured to have no translations.') . '</li>';
  303. $output .= '</ul>';
  304. $output .= '<p>' . t('The multilingual options of a menu must be configured before individual menu items can be translated. Go to the <a href="@menu-admin">Menus administration page</a> and follow the "edit menu" link to the menu in question.', array('@menu-admin' => url('admin/structure/menu') ) ) . '</p>';
  305. $output .= '<p>' . t('To search and translate strings, use the <a href="@translate-interface">translation interface</a> pages.', array('@translate-interface' => url('admin/config/regional/translate'))) . '</p>';
  306. return $output;
  307. case 'admin/config/regional/i18n':
  308. $output = '<p>' . t('Menus and menu items can be translated on the <a href="@configure_menus">Menu administration page</a>.', array('@configure_menus' => url('admin/structure/menu'))) . '</p>';
  309. return $output;
  310. }
  311. }
  312. /**
  313. * Implements hook_variable_info_alter()
  314. */
  315. function i18n_menu_variable_info_alter(&$variables, $options) {
  316. // Make menu variables translatable
  317. $variables['menu_main_links_source']['localize'] = TRUE;
  318. $variables['menu_secondary_links_source']['localize'] = TRUE;
  319. }
  320. /**
  321. * Get localized menu tree.
  322. *
  323. * @param string $menu_name
  324. * The menu the translated tree has to be fetched from.
  325. * @param string $langcode
  326. * Optional language code to get the menu in, defaults to request language.
  327. * @param bool $reset
  328. * Whether to reset the internal i18n_menu_translated_tree cache.
  329. */
  330. function i18n_menu_translated_tree($menu_name, $langcode = NULL, $reset = FALSE) {
  331. $menu_output = &drupal_static(__FUNCTION__);
  332. $langcode = $langcode ? $langcode : i18n_language_interface()->language;
  333. if (!isset($menu_output[$langcode][$menu_name]) || $reset) {
  334. $tree = menu_tree_page_data($menu_name);
  335. $tree = i18n_menu_localize_tree($tree, $langcode);
  336. $menu_output[$langcode][$menu_name] = menu_tree_output($tree);
  337. }
  338. return $menu_output[$langcode][$menu_name];
  339. }
  340. /**
  341. * Localize menu tree.
  342. */
  343. function i18n_menu_localize_tree($tree, $langcode = NULL) {
  344. $langcode = $langcode ? $langcode : i18n_language_interface()->language;
  345. foreach ($tree as $index => &$item) {
  346. $link = $item['link'];
  347. // We only process links that are visible and not processed before.
  348. if (_i18n_menu_link_process($item['link'])) {
  349. if (!_i18n_menu_link_is_visible($item['link'], $langcode)) {
  350. // Remove links for other languages than current.
  351. // Links with language wont be localized.
  352. unset($tree[$index]);
  353. // @todo Research whether the above has any advantage over:
  354. // $item['hidden'] = TRUE;
  355. }
  356. else {
  357. if (_i18n_menu_link_is_localizable($item['link'])) {
  358. // Item has undefined language, it is a candidate for localization.
  359. _i18n_menu_link_localize($item['link'], $langcode);
  360. }
  361. // Localize subtree.
  362. if (!empty($item['below'])) {
  363. $item['below'] = i18n_menu_localize_tree($item['below'], $langcode);
  364. }
  365. }
  366. }
  367. }
  368. return $tree;
  369. }
  370. /**
  371. * Localize menu renderable array
  372. */
  373. function i18n_menu_localize_elements(&$elements) {
  374. foreach (element_children($elements) as $mlid) {
  375. $elements[$mlid]['#title'] = i18n_string(array('menu', 'item', $mlid, 'title'), $elements[$mlid]['#title']);
  376. if (!empty($tree[$mlid]['#localized_options']['attributes']['title'])) {
  377. $elements[$mlid]['#localized_options']['attributes']['title'] = i18n_string(array('menu', 'item', $mlid, 'description'), $tree[$mlid]['#localized_options']['attributes']['title']);
  378. }
  379. i18n_menu_localize_elements($elements[$mlid]);
  380. }
  381. }
  382. /**
  383. * Return an array of localized links for a navigation menu.
  384. *
  385. * Localized version of menu_navigation_links()
  386. */
  387. function i18n_menu_navigation_links($menu_name, $level = 0) {
  388. // Don't even bother querying the menu table if no menu is specified.
  389. if (empty($menu_name)) {
  390. return array();
  391. }
  392. // Get the menu hierarchy for the current page.
  393. $tree = menu_tree_page_data($menu_name, $level + 1);
  394. $tree = i18n_menu_localize_tree($tree);
  395. // Go down the active trail until the right level is reached.
  396. while ($level-- > 0 && $tree) {
  397. // Loop through the current level's items until we find one that is in trail.
  398. while ($item = array_shift($tree)) {
  399. if ($item['link']['in_active_trail']) {
  400. // If the item is in the active trail, we continue in the subtree.
  401. $tree = empty($item['below']) ? array() : $item['below'];
  402. break;
  403. }
  404. }
  405. }
  406. // Create a single level of links.
  407. $router_item = menu_get_item();
  408. $links = array();
  409. foreach ($tree as $item) {
  410. if (!$item['link']['hidden']) {
  411. $class = '';
  412. $l = $item['link']['localized_options'];
  413. $l['href'] = $item['link']['href'];
  414. $l['title'] = $item['link']['title'];
  415. if ($item['link']['in_active_trail']) {
  416. $class = ' active-trail';
  417. $l['attributes']['class'][] = 'active-trail';
  418. }
  419. // Normally, l() compares the href of every link with $_GET['q'] and sets
  420. // the active class accordingly. But local tasks do not appear in menu
  421. // trees, so if the current path is a local task, and this link is its
  422. // tab root, then we have to set the class manually.
  423. if ($item['link']['href'] == $router_item['tab_root_href'] && $item['link']['href'] != $_GET['q']) {
  424. $l['attributes']['class'][] = 'active';
  425. }
  426. // Keyed with the unique mlid to generate classes in theme_links().
  427. $links['menu-' . $item['link']['mlid'] . $class] = $l;
  428. }
  429. }
  430. return $links;
  431. }
  432. /**
  433. * Get localized menu title
  434. */
  435. function _i18n_menu_link_title($link, $langcode = NULL) {
  436. return i18n_string_translate(array('menu', 'item', $link['mlid'], 'title'), $link['link_title'], array('langcode' => $langcode, 'sanitize' => FALSE));
  437. }
  438. /**
  439. * Localize menu item title and description.
  440. *
  441. * This will be invoked always after _menu_item_localize()
  442. *
  443. * Link properties to manage:
  444. * - title, menu router title
  445. * - link_title, menu link title
  446. * - options.attributes.title, menu link description.
  447. * - localized_options.attributes.title,
  448. *
  449. * @see _menu_item_localize()
  450. * @see _menu_link_translate()
  451. */
  452. function _i18n_menu_link_localize(&$link, $langcode = NULL) {
  453. // Only translate title if it has no special callback.
  454. if (empty($link['title callback']) || $link['title callback'] === 't') {
  455. $link['title'] = _i18n_menu_link_title($link, $langcode);
  456. }
  457. if ($description = _i18n_menu_link_description($link, $langcode)) {
  458. $link['localized_options']['attributes']['title'] = $description;
  459. }
  460. }
  461. /**
  462. * Get localized menu description
  463. */
  464. function _i18n_menu_link_description($link, $langcode = NULL) {
  465. if (!empty($link['options']['attributes']['title'])) {
  466. return i18n_string_translate(array('menu', 'item', $link['mlid'], 'description'), $link['options']['attributes']['title'], array('langcode' => $langcode));
  467. }
  468. else {
  469. return NULL;
  470. }
  471. }
  472. /**
  473. * Check whether this link is to be processed by i18n_menu and start processing.
  474. */
  475. function _i18n_menu_link_process(&$link) {
  476. // Only visible links that have a language property and haven't been processed
  477. // before. We also check that they belong to a menu with language options.
  478. if (empty($link['i18n_menu']) && !empty($link['language']) && !empty($link['access']) && empty($link['hidden']) && i18n_menu_mode($link['menu_name'])) {
  479. // Mark so it won't be processed twice.
  480. $link['i18n_menu'] = TRUE;
  481. // Skip if administering this menu or this menu item.
  482. if (arg(0) == 'admin' && arg(1) == 'structure' && arg(2) == 'menu') {
  483. if (arg(3) == 'manage' && $link['menu_name'] == arg(4)) {
  484. return FALSE;
  485. }
  486. elseif (arg(3) == 'item' && arg(4) == $link['mlid']) {
  487. return FALSE;
  488. }
  489. }
  490. return TRUE;
  491. }
  492. else {
  493. return FALSE;
  494. }
  495. }
  496. /**
  497. * Check whether this menu item should be marked for altering.
  498. *
  499. * Menu items that have a language or that have any localizable strings
  500. * will be marked to be run through hook_translated_menu_link_alter().
  501. *
  502. * @see i18n_menu_translated_menu_link_alter()
  503. */
  504. function _i18n_menu_link_check_alter($link) {
  505. return i18n_menu_mode($link['menu_name']) && (i18n_object_langcode($link) || _i18n_menu_link_is_localizable($link, TRUE));
  506. }
  507. /**
  508. * Check whether this link should be localized by i18n_menu.
  509. *
  510. * @param array $link
  511. * Menu link array.
  512. * @param bool $check_strings
  513. * Whether to check if the link has actually localizable strings. Since this
  514. * is a more expensive operation, it will be just checked when editing menu
  515. * items.
  516. *
  517. * @return boolean
  518. * Returns TRUE if link is localizable.
  519. */
  520. function _i18n_menu_link_is_localizable($link, $check_strings = FALSE) {
  521. return !empty($link['customized']) && !i18n_object_langcode($link) && i18n_menu_mode($link['menu_name'], I18N_MODE_LOCALIZE) &&
  522. (!$check_strings || _i18n_menu_link_localizable_properties($link));
  523. }
  524. /**
  525. * Check whether this menu link is visible for current/given language.
  526. */
  527. function _i18n_menu_link_is_visible($link, $langcode = NULL) {
  528. $langcode = $langcode ? $langcode : i18n_language_interface()->language;
  529. return $link['language'] == LANGUAGE_NONE || $link['language'] == $langcode;
  530. }
  531. /**
  532. * Get localizable properties for menu link checking agains the router item.
  533. */
  534. function _i18n_menu_link_localizable_properties($link) {
  535. $props = array();
  536. $router = !empty($link['router_path']) ? _i18n_menu_get_router($link['router_path']) : NULL;
  537. if (!empty($link['link_title'])) {
  538. // If the title callback is 't' and the link title matches the router title
  539. // it will be localized by core, not by i18n_menu.
  540. if (!$router ||
  541. (empty($router['title_callback']) || $router['title_callback'] != 't') ||
  542. (empty($router['title']) || $router['title'] != $link['link_title'])
  543. ) {
  544. $props[] = 'title';
  545. }
  546. }
  547. if (!empty($link['options']['attributes']['title'])) {
  548. // If the description matches the router description, it will be localized
  549. // by core.
  550. if (!$router || empty($router['description']) || $router['description'] != $link['options']['attributes']['title']) {
  551. $props[] = 'description';
  552. }
  553. }
  554. return $props;
  555. }
  556. /**
  557. * Get the menu router for this router path.
  558. *
  559. * We need the untranslated title to compare, and this will be fast.
  560. * There's no api function to do this?
  561. *
  562. * @param string $path
  563. * The path to fetch from the router.
  564. */
  565. function _i18n_menu_get_router($path) {
  566. $cache = &drupal_static(__FUNCTION__, array());
  567. if (!array_key_exists($path, $cache)) {
  568. $cache[$path] = db_select('menu_router', 'mr')
  569. ->fields('mr', array('title', 'title_callback', 'description'))
  570. ->condition('path', $path)
  571. ->execute()
  572. ->fetchAssoc();
  573. }
  574. return $cache[$path];
  575. }
  576. /**
  577. * Implements hook_form_FORM_ID_alter().
  578. */
  579. function i18n_menu_form_menu_edit_menu_alter(&$form, &$form_state) {
  580. $menu = menu_load($form['old_name']['#value']);
  581. $i18n_mode = $menu && isset($menu['i18n_mode']) ? $menu['i18n_mode'] : I18N_MODE_NONE;
  582. $langcode = $menu && isset($menu['language']) ? $menu['language'] : LANGUAGE_NONE;
  583. $form += i18n_translation_mode_element('menu', $i18n_mode, $langcode);
  584. }
  585. /**
  586. * Implements hook_form_FORM_ID_alter().
  587. *
  588. * Add a language selector to the menu_edit_item form and register a submit
  589. * callback to process items.
  590. */
  591. function i18n_menu_form_menu_edit_item_alter(&$form, &$form_state) {
  592. $item = &$form['original_item']['#value'];
  593. $item['language'] = i18n_menu_item_get_language($item);
  594. // Check whether this item belongs to a node object and it is a supported type.
  595. $node_item = ($node = i18n_menu_item_get_node($item)) && i18n_menu_node_supported_type($node->type);
  596. if (!$node_item && i18n_menu_mode($item['menu_name'], I18N_MODE_TRANSLATE)) {
  597. //$form['i18n'] = array('#type' => 'fieldset');
  598. $form['i18n']['language'] = array(
  599. '#description' => t('This item belongs to a multilingual menu. You can set a language for it.'),
  600. ) + i18n_element_language_select($item);
  601. // If the term to be added will be a translation of a source term,
  602. // set the default value of the option list to the target language and
  603. // create a form element for storing the translation set of the source term.
  604. if (isset($_GET['translation']) && isset($_GET['target']) && ($source_item = menu_link_load($_GET['translation']))) {
  605. if (!empty($source_item['i18n_tsid'])) {
  606. $translation_set = i18n_translation_set_load($source_item['i18n_tsid']);
  607. }
  608. else {
  609. // Create object and stick the source information in the translation set.
  610. $translation_set = i18n_translation_set_build('menu_link')
  611. ->add_item($source_item);
  612. }
  613. $form['link_path']['#default_value'] = $source_item['link_path'];
  614. // Maybe we should disable the 'link_path' and 'parent' form elements?
  615. // $form['link_path']['#disabled'] = TRUE;
  616. // $form['parent']['#disabled'] = TRUE;
  617. $form['i18n']['language']['#default_value'] = $_GET['target'];
  618. $form['i18n']['language']['#disabled'] = TRUE;
  619. drupal_set_title(t('%language translation of menu item %title', array('%language' => locale_language_name($_GET['target']), '%title' => $source_item['link_title'])), PASS_THROUGH);
  620. }
  621. elseif (!empty($item['i18n_tsid'])) {
  622. $translation_set = i18n_translation_set_load($item['i18n_tsid']);
  623. }
  624. // Add the translation set to the form so we know the new menu item
  625. // needs to be added to that set.
  626. if (!empty($translation_set)) {
  627. $form['translation_set'] = array(
  628. '#type' => 'value',
  629. '#value' => $translation_set,
  630. );
  631. // If the current term is part of a translation set,
  632. // remove all other languages of the option list.
  633. if ($translations = $translation_set->get_translations()) {
  634. unset($form['i18n']['language']['#options'][LANGUAGE_NONE]);
  635. foreach ($translations as $langcode => $translation) {
  636. if ($translation['mlid'] !== $item['mlid']) {
  637. unset($form['i18n']['language']['#options'][$langcode]);
  638. }
  639. }
  640. }
  641. }
  642. }
  643. else {
  644. $form['language'] = array(
  645. '#type' => 'value',
  646. '#value' => $item['language'],
  647. );
  648. }
  649. if ($node_item && i18n_langcode($item['language'])) {
  650. $form['i18n']['message'] = array(
  651. '#type' => 'item',
  652. '#title' => t('Language'),
  653. '#markup' => i18n_language_name($item['language']),
  654. '#description' => t('This menu item belongs to a node, so it will have the same language as the node and cannot be localized.'),
  655. );
  656. }
  657. array_unshift($form['#validate'], 'i18n_menu_menu_item_prepare_normal_path');
  658. }
  659. /**
  660. * Normal path should be checked with menu item's language to avoid
  661. * troubles when a node and it's translation has the same url alias.
  662. */
  663. function i18n_menu_menu_item_prepare_normal_path($form, &$form_state) {
  664. $item = &$form_state['values'];
  665. $item['link_path'] = i18n_prepare_normal_path($item['link_path'], $item['language']);
  666. }
  667. /**
  668. * Get language for menu item
  669. */
  670. function i18n_menu_item_get_language($item) {
  671. if (isset($item['language'])) {
  672. return $item['language'];
  673. }
  674. else {
  675. $menu = menu_load($item['menu_name']);
  676. switch ($menu['i18n_mode']) {
  677. case I18N_MODE_LANGUAGE:
  678. return $menu['language'];
  679. case I18N_MODE_NONE:
  680. case I18N_MODE_LOCALIZE:
  681. return LANGUAGE_NONE;
  682. default:
  683. if (!empty($item['mlid'])) {
  684. return db_select('menu_links', 'm')
  685. ->fields('m', array('language'))
  686. ->condition('mlid', $item['mlid'])
  687. ->execute()
  688. ->fetchField();
  689. }
  690. else {
  691. return LANGUAGE_NONE;
  692. }
  693. }
  694. }
  695. }
  696. /**
  697. * Implements hook_form_node_form_alter().
  698. *
  699. * Add language to menu settings of the node form, as well as setting defaults
  700. * to match the translated item's menu settings.
  701. */
  702. function i18n_menu_form_node_form_alter(&$form, &$form_state, $form_id) {
  703. if (isset($form['menu'])) {
  704. $node = $form['#node'];
  705. $link = $node->menu;
  706. if (!empty($link['mlid'])) {
  707. // Preserve the menu item language whatever it is.
  708. $form['menu']['link']['language'] = array('#type' => 'value', '#value' => $link['language']);
  709. }
  710. elseif (i18n_menu_node_supported_type($node->type)) {
  711. // Set menu language to node language but only if it is a supported node type.
  712. $form['menu']['link']['language'] = array('#type' => 'value', '#value' => $node->language);
  713. }
  714. else {
  715. $form['menu']['link']['language'] = array('#type' => 'value', '#value' => LANGUAGE_NONE);
  716. }
  717. // Customized must be set to 1 to save language.
  718. $form['menu']['link']['customized'] = array('#type' => 'value', '#value' => 1);
  719. }
  720. }
  721. /**
  722. * Check whether a node type has multilingual support (but not entity translation).
  723. */
  724. function i18n_menu_node_supported_type($type) {
  725. $supported = &drupal_static(__FUNCTION__);
  726. if (!isset($supported[$type])) {
  727. $mode = variable_get('language_content_type_' . $type, 0);
  728. $supported[$type] = $mode == 1 || $mode == 2; // 2 == TRANSLATION_ENABLED
  729. }
  730. return $supported[$type];
  731. }
  732. /**
  733. * Get the node object for a menu item.
  734. */
  735. function i18n_menu_item_get_node($item) {
  736. return isset($item['router_path']) && $item['router_path'] == 'node/%' ? node_load(arg(1, $item['link_path'])) : NULL;
  737. }
  738. /**
  739. * Implements hook_node_presave()
  740. *
  741. * Set menu link language to node language
  742. */
  743. function i18n_menu_node_presave($node) {
  744. if (!empty($node->menu) && isset($node->language) && i18n_menu_node_supported_type($node->type)) {
  745. $node->menu['language'] = i18n_object_langcode($node, LANGUAGE_NONE);
  746. // Store node type with menu item so we can quickly access it later.
  747. $node->menu['options']['node_type'] = $node->type;
  748. }
  749. }
  750. /**
  751. * Implements hook_node_prepare_translation().
  752. */
  753. function i18n_menu_node_prepare_translation($node) {
  754. if (empty($node->menu['mlid']) && !empty($node->translation_source)) {
  755. $tnode = $node->translation_source;
  756. // Prepare the tnode so the menu item will be available.
  757. node_object_prepare($tnode);
  758. $node->menu['link_title'] = $tnode->menu['link_title'];
  759. $node->menu['weight'] = $tnode->menu['weight'];
  760. }
  761. }
  762. /**
  763. * Process menu and menu item add/edit form submissions.
  764. *
  765. * @todo See where this fits
  766. */
  767. /*
  768. function i18n_menu_edit_item_form_submit($form, &$form_state) {
  769. $mid = menu_edit_item_save($form_state['values']);
  770. db_query("UPDATE {menu} SET language = '%s' WHERE mid = %d", array($form_state['values']['language'], $mid));
  771. return 'admin/build/menu';
  772. }
  773. */
  774. /**
  775. * Load translation set. Menu loading callback.
  776. */
  777. function i18n_menu_translation_load($tsid) {
  778. return i18n_translation_set_load($tsid, 'menu_link');
  779. }
  780. /**
  781. * Load menu item by path, language
  782. */
  783. function i18n_menu_link_load($path, $langcode) {
  784. $query = db_select('menu_links', 'ml');
  785. $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  786. $query->fields('ml');
  787. // Weight should be taken from {menu_links}, not {menu_router}.
  788. $query->addField('ml', 'weight', 'link_weight');
  789. $query->fields('m');
  790. $query->condition('ml.link_path', $path);
  791. $query->condition('ml.language', $langcode);
  792. if ($item = $query->execute()->fetchAssoc()) {
  793. $item['weight'] = $item['link_weight'];
  794. _menu_link_translate($item);
  795. return $item;
  796. }
  797. }
  798. /**
  799. * Implements hook_init().
  800. */
  801. function i18n_menu_init() {
  802. // The only way to override the default preferred menu link for a path is to
  803. // inject it into the static cache of the function menu_link_get_preferred().
  804. // The problem with the default implementation is that it does not take the
  805. // language of a menu link into account. Whe having different menu trees for
  806. // different menus, this means that the active trail will not work for all but
  807. // one language.
  808. // The code below is identical to the mentioned function except the added
  809. // language condition on the query.
  810. // TODO: Adding an alter tag to the query would allow to do this with a simple
  811. // hook_query_alter() implementation.
  812. $preferred_links = &drupal_static('menu_link_get_preferred');
  813. $path = $_GET['q'];
  814. // Look for the correct menu link by building a list of candidate paths,
  815. // which are ordered by priority (translated hrefs are preferred over
  816. // untranslated paths). Afterwards, the most relevant path is picked from
  817. // the menus, ordered by menu preference.
  818. $item = menu_get_item($path);
  819. $path_candidates = array();
  820. // 1. The current item href.
  821. $path_candidates[$item['href']] = $item['href'];
  822. // 2. The tab root href of the current item (if any).
  823. if ($item['tab_parent'] && ($tab_root = menu_get_item($item['tab_root_href']))) {
  824. $path_candidates[$tab_root['href']] = $tab_root['href'];
  825. }
  826. // 3. The current item path (with wildcards).
  827. $path_candidates[$item['path']] = $item['path'];
  828. // 4. The tab root path of the current item (if any).
  829. if (!empty($tab_root)) {
  830. $path_candidates[$tab_root['path']] = $tab_root['path'];
  831. }
  832. // Retrieve a list of menu names, ordered by preference.
  833. $menu_names = menu_get_active_menu_names();
  834. // Use an illegal menu name as the key for the preferred menu link.
  835. $selected_menu = MENU_PREFERRED_LINK;
  836. // Put the selected menu at the front of the list.
  837. array_unshift($menu_names, $selected_menu);
  838. $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
  839. $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  840. $query->fields('ml');
  841. // Weight must be taken from {menu_links}, not {menu_router}.
  842. $query->addField('ml', 'weight', 'link_weight');
  843. $query->fields('m');
  844. $query->condition('ml.link_path', $path_candidates, 'IN');
  845. // Only look menu links with none or the current language.
  846. $query->condition('ml.language', array(LANGUAGE_NONE, i18n_language_interface()->language), 'IN');
  847. // Sort candidates by link path and menu name.
  848. $candidates = array();
  849. foreach ($query->execute() as $candidate) {
  850. $candidate['weight'] = $candidate['link_weight'];
  851. $candidates[$candidate['link_path']][$candidate['menu_name']] = $candidate;
  852. // Add any menus not already in the menu name search list.
  853. if (!in_array($candidate['menu_name'], $menu_names)) {
  854. $menu_names[] = $candidate['menu_name'];
  855. }
  856. }
  857. // Store the most specific link for each menu. Also save the most specific
  858. // link of the most preferred menu in $preferred_link.
  859. foreach ($path_candidates as $link_path) {
  860. if (isset($candidates[$link_path])) {
  861. foreach ($menu_names as $menu_name) {
  862. if (empty($preferred_links[$path][$menu_name]) && isset($candidates[$link_path][$menu_name])) {
  863. $candidate_item = $candidates[$link_path][$menu_name];
  864. $map = explode('/', $path);
  865. _menu_translate($candidate_item, $map);
  866. if ($candidate_item['access']) {
  867. $preferred_links[$path][$menu_name] = $candidate_item;
  868. if (empty($preferred_links[$path][MENU_PREFERRED_LINK])) {
  869. // Store the most specific link.
  870. $preferred_links[$path][MENU_PREFERRED_LINK] = $candidate_item;
  871. }
  872. }
  873. }
  874. }
  875. }
  876. }
  877. }