i18n_menu.module 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  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. $variables['menu_parent_[node_type]']['localize'] = TRUE;
  320. $variables['menu_options_[node_type]']['localize'] = TRUE;
  321. }
  322. /**
  323. * Get localized menu tree.
  324. *
  325. * @param string $menu_name
  326. * The menu the translated tree has to be fetched from.
  327. * @param string $langcode
  328. * Optional language code to get the menu in, defaults to request language.
  329. * @param bool $reset
  330. * Whether to reset the internal i18n_menu_translated_tree cache.
  331. */
  332. function i18n_menu_translated_tree($menu_name, $langcode = NULL, $reset = FALSE) {
  333. $menu_output = &drupal_static(__FUNCTION__);
  334. $langcode = $langcode ? $langcode : i18n_language_interface()->language;
  335. if (!isset($menu_output[$langcode][$menu_name]) || $reset) {
  336. $tree = menu_tree_page_data($menu_name);
  337. $tree = i18n_menu_localize_tree($tree, $langcode);
  338. $menu_output[$langcode][$menu_name] = menu_tree_output($tree);
  339. }
  340. return $menu_output[$langcode][$menu_name];
  341. }
  342. /**
  343. * Localize menu tree.
  344. */
  345. function i18n_menu_localize_tree($tree, $langcode = NULL) {
  346. $langcode = $langcode ? $langcode : i18n_language_interface()->language;
  347. foreach ($tree as $index => &$item) {
  348. $link = $item['link'];
  349. // We only process links that are visible and not processed before.
  350. if (_i18n_menu_link_process($item['link'])) {
  351. if (!_i18n_menu_link_is_visible($item['link'], $langcode)) {
  352. // Remove links for other languages than current.
  353. // Links with language wont be localized.
  354. unset($tree[$index]);
  355. // @todo Research whether the above has any advantage over:
  356. // $item['hidden'] = TRUE;
  357. }
  358. else {
  359. if (_i18n_menu_link_is_localizable($item['link'])) {
  360. // Item has undefined language, it is a candidate for localization.
  361. _i18n_menu_link_localize($item['link'], $langcode);
  362. }
  363. // Localize subtree.
  364. if (!empty($item['below'])) {
  365. $item['below'] = i18n_menu_localize_tree($item['below'], $langcode);
  366. }
  367. }
  368. }
  369. }
  370. return $tree;
  371. }
  372. /**
  373. * Localize menu renderable array
  374. */
  375. function i18n_menu_localize_elements(&$elements) {
  376. foreach (element_children($elements) as $mlid) {
  377. $elements[$mlid]['#title'] = i18n_string(array('menu', 'item', $mlid, 'title'), $elements[$mlid]['#title']);
  378. if (!empty($tree[$mlid]['#localized_options']['attributes']['title'])) {
  379. $elements[$mlid]['#localized_options']['attributes']['title'] = i18n_string(array('menu', 'item', $mlid, 'description'), $tree[$mlid]['#localized_options']['attributes']['title']);
  380. }
  381. i18n_menu_localize_elements($elements[$mlid]);
  382. }
  383. }
  384. /**
  385. * Return an array of localized links for a navigation menu.
  386. *
  387. * Localized version of menu_navigation_links()
  388. */
  389. function i18n_menu_navigation_links($menu_name, $level = 0) {
  390. // Don't even bother querying the menu table if no menu is specified.
  391. if (empty($menu_name)) {
  392. return array();
  393. }
  394. // Get the menu hierarchy for the current page.
  395. $tree = menu_tree_page_data($menu_name, $level + 1);
  396. $tree = i18n_menu_localize_tree($tree);
  397. // Go down the active trail until the right level is reached.
  398. while ($level-- > 0 && $tree) {
  399. // Loop through the current level's items until we find one that is in trail.
  400. while ($item = array_shift($tree)) {
  401. if ($item['link']['in_active_trail']) {
  402. // If the item is in the active trail, we continue in the subtree.
  403. $tree = empty($item['below']) ? array() : $item['below'];
  404. break;
  405. }
  406. }
  407. }
  408. // Create a single level of links.
  409. $router_item = menu_get_item();
  410. $links = array();
  411. foreach ($tree as $item) {
  412. if (!$item['link']['hidden']) {
  413. $class = '';
  414. $l = $item['link']['localized_options'];
  415. $l['href'] = $item['link']['href'];
  416. $l['title'] = $item['link']['title'];
  417. if ($item['link']['in_active_trail']) {
  418. $class = ' active-trail';
  419. $l['attributes']['class'][] = 'active-trail';
  420. }
  421. // Normally, l() compares the href of every link with $_GET['q'] and sets
  422. // the active class accordingly. But local tasks do not appear in menu
  423. // trees, so if the current path is a local task, and this link is its
  424. // tab root, then we have to set the class manually.
  425. if ($item['link']['href'] == $router_item['tab_root_href'] && $item['link']['href'] != $_GET['q']) {
  426. $l['attributes']['class'][] = 'active';
  427. }
  428. // Keyed with the unique mlid to generate classes in theme_links().
  429. $links['menu-' . $item['link']['mlid'] . $class] = $l;
  430. }
  431. }
  432. return $links;
  433. }
  434. /**
  435. * Get localized menu title
  436. */
  437. function _i18n_menu_link_title($link, $langcode = NULL) {
  438. $key = i18n_object_info('menu_link', 'key');
  439. return i18n_string_translate(array('menu', 'item', $link[$key], 'title'), $link['link_title'], array('langcode' => $langcode, 'sanitize' => FALSE));
  440. }
  441. /**
  442. * Localize menu item title and description.
  443. *
  444. * This will be invoked always after _menu_item_localize()
  445. *
  446. * Link properties to manage:
  447. * - title, menu router title
  448. * - link_title, menu link title
  449. * - options.attributes.title, menu link description.
  450. * - localized_options.attributes.title,
  451. *
  452. * @see _menu_item_localize()
  453. * @see _menu_link_translate()
  454. */
  455. function _i18n_menu_link_localize(&$link, $langcode = NULL) {
  456. // Only translate title if it has no special callback.
  457. if (empty($link['title callback']) || $link['title callback'] === 't') {
  458. $link['title'] = _i18n_menu_link_title($link, $langcode);
  459. }
  460. if ($description = _i18n_menu_link_description($link, $langcode)) {
  461. $link['localized_options']['attributes']['title'] = $description;
  462. }
  463. }
  464. /**
  465. * Get localized menu description
  466. */
  467. function _i18n_menu_link_description($link, $langcode = NULL) {
  468. if (!empty($link['options']['attributes']['title'])) {
  469. $key = i18n_object_info('menu_link', 'key');
  470. return i18n_string_translate(array('menu', 'item', $link[$key], 'description'), $link['options']['attributes']['title'], array('langcode' => $langcode));
  471. }
  472. else {
  473. return NULL;
  474. }
  475. }
  476. /**
  477. * Check whether this link is to be processed by i18n_menu and start processing.
  478. */
  479. function _i18n_menu_link_process(&$link) {
  480. // Only visible links that have a language property and haven't been processed
  481. // before. We also check that they belong to a menu with language options.
  482. if (empty($link['i18n_menu']) && !empty($link['language']) && !empty($link['access']) && empty($link['hidden']) && i18n_menu_mode($link['menu_name'])) {
  483. // Mark so it won't be processed twice.
  484. $link['i18n_menu'] = TRUE;
  485. // Skip if administering this menu or this menu item.
  486. if (arg(0) == 'admin' && arg(1) == 'structure' && arg(2) == 'menu') {
  487. if (arg(3) == 'manage' && $link['menu_name'] == arg(4)) {
  488. return FALSE;
  489. }
  490. elseif (arg(3) == 'item' && arg(4) == $link['mlid']) {
  491. return FALSE;
  492. }
  493. }
  494. // Skip if administering this menu item through the node edit form.
  495. elseif (arg(0) == 'node' && arg(2) == 'edit' && $link['link_path'] == arg(0) . '/' . arg(1)) {
  496. return FALSE;
  497. }
  498. return TRUE;
  499. }
  500. else {
  501. return FALSE;
  502. }
  503. }
  504. /**
  505. * Check whether this menu item should be marked for altering.
  506. *
  507. * Menu items that have a language or that have any localizable strings
  508. * will be marked to be run through hook_translated_menu_link_alter().
  509. *
  510. * @see i18n_menu_translated_menu_link_alter()
  511. */
  512. function _i18n_menu_link_check_alter($link) {
  513. return i18n_menu_mode($link['menu_name']) && (i18n_object_langcode($link) || _i18n_menu_link_is_localizable($link, TRUE));
  514. }
  515. /**
  516. * Check whether this link should be localized by i18n_menu.
  517. *
  518. * @param array $link
  519. * Menu link array.
  520. * @param bool $check_strings
  521. * Whether to check if the link has actually localizable strings. Since this
  522. * is a more expensive operation, it will be just checked when editing menu
  523. * items.
  524. *
  525. * @return boolean
  526. * Returns TRUE if link is localizable.
  527. */
  528. function _i18n_menu_link_is_localizable($link, $check_strings = FALSE) {
  529. return !empty($link['customized']) && !i18n_object_langcode($link) && i18n_menu_mode($link['menu_name'], I18N_MODE_LOCALIZE) &&
  530. (!$check_strings || _i18n_menu_link_localizable_properties($link));
  531. }
  532. /**
  533. * Check whether this menu link is visible for current/given language.
  534. */
  535. function _i18n_menu_link_is_visible($link, $langcode = NULL) {
  536. $langcode = $langcode ? $langcode : i18n_language_interface()->language;
  537. return $link['language'] == LANGUAGE_NONE || $link['language'] == $langcode;
  538. }
  539. /**
  540. * Get localizable properties for menu link checking agains the router item.
  541. */
  542. function _i18n_menu_link_localizable_properties($link) {
  543. $props = array();
  544. $router = !empty($link['router_path']) ? _i18n_menu_get_router($link['router_path']) : NULL;
  545. if (!empty($link['link_title'])) {
  546. // If the title callback is 't' and the link title matches the router title
  547. // it will be localized by core, not by i18n_menu.
  548. if (!$router ||
  549. (empty($router['title_callback']) || ($router['title_callback'] != 't' || !empty($link['customized']))) ||
  550. (empty($router['title']) || $router['title'] != $link['link_title'])
  551. ) {
  552. $props[] = 'title';
  553. }
  554. }
  555. if (!empty($link['options']['attributes']['title'])) {
  556. // If the description matches the router description, it will be localized
  557. // by core.
  558. if (!$router || empty($router['description']) || ($router['description'] != $link['options']['attributes']['title']) || !empty($link['customized'])) {
  559. $props[] = 'description';
  560. }
  561. }
  562. return $props;
  563. }
  564. /**
  565. * Get the menu router for this router path.
  566. *
  567. * We need the untranslated title to compare, and this will be fast.
  568. * There's no api function to do this?
  569. *
  570. * @param string $path
  571. * The path to fetch from the router.
  572. */
  573. function _i18n_menu_get_router($path) {
  574. $cache = &drupal_static(__FUNCTION__, array());
  575. if (!array_key_exists($path, $cache)) {
  576. $cache[$path] = db_select('menu_router', 'mr')
  577. ->fields('mr', array('title', 'title_callback', 'description'))
  578. ->condition('path', $path)
  579. ->execute()
  580. ->fetchAssoc();
  581. }
  582. return $cache[$path];
  583. }
  584. /**
  585. * Implements hook_form_FORM_ID_alter().
  586. */
  587. function i18n_menu_form_menu_edit_menu_alter(&$form, &$form_state) {
  588. $menu = menu_load($form['old_name']['#value']);
  589. $i18n_mode = $menu && isset($menu['i18n_mode']) ? $menu['i18n_mode'] : I18N_MODE_NONE;
  590. $langcode = $menu && isset($menu['language']) ? $menu['language'] : LANGUAGE_NONE;
  591. $form += i18n_translation_mode_element('menu', $i18n_mode, $langcode);
  592. }
  593. /**
  594. * Implements hook_form_FORM_ID_alter().
  595. *
  596. * Add a language selector to the menu_edit_item form and register a submit
  597. * callback to process items.
  598. */
  599. function i18n_menu_form_menu_edit_item_alter(&$form, &$form_state) {
  600. $item = &$form['original_item']['#value'];
  601. $item['language'] = i18n_menu_item_get_language($item);
  602. // Check whether this item belongs to a node object and it is a supported type.
  603. $node_item = ($node = i18n_menu_item_get_node($item)) && i18n_menu_node_supported_type($node->type);
  604. if (!$node_item && i18n_menu_mode($item['menu_name'], I18N_MODE_TRANSLATE)) {
  605. //$form['i18n'] = array('#type' => 'fieldset');
  606. $form['i18n']['language'] = array(
  607. '#description' => t('This item belongs to a multilingual menu. You can set a language for it.'),
  608. ) + i18n_element_language_select($item);
  609. // If the term to be added will be a translation of a source term,
  610. // set the default value of the option list to the target language and
  611. // create a form element for storing the translation set of the source term.
  612. if (isset($_GET['translation']) && isset($_GET['target']) && ($source_item = menu_link_load($_GET['translation']))) {
  613. if (!empty($source_item['i18n_tsid'])) {
  614. $translation_set = i18n_translation_set_load($source_item['i18n_tsid']);
  615. }
  616. else {
  617. // Create object and stick the source information in the translation set.
  618. $translation_set = i18n_translation_set_build('menu_link')
  619. ->add_item($source_item);
  620. }
  621. $form['link_path']['#default_value'] = $source_item['link_path'];
  622. // Maybe we should disable the 'link_path' and 'parent' form elements?
  623. // $form['link_path']['#disabled'] = TRUE;
  624. // $form['parent']['#disabled'] = TRUE;
  625. $form['i18n']['language']['#default_value'] = $_GET['target'];
  626. $form['i18n']['language']['#disabled'] = TRUE;
  627. drupal_set_title(t('%language translation of menu item %title', array('%language' => locale_language_name($_GET['target']), '%title' => $source_item['link_title'])), PASS_THROUGH);
  628. }
  629. elseif (!empty($item['i18n_tsid'])) {
  630. $translation_set = i18n_translation_set_load($item['i18n_tsid']);
  631. }
  632. // Add the translation set to the form so we know the new menu item
  633. // needs to be added to that set.
  634. if (!empty($translation_set)) {
  635. $form['translation_set'] = array(
  636. '#type' => 'value',
  637. '#value' => $translation_set,
  638. );
  639. // If the current term is part of a translation set,
  640. // remove all other languages of the option list.
  641. if ($translations = $translation_set->get_translations()) {
  642. unset($form['i18n']['language']['#options'][LANGUAGE_NONE]);
  643. foreach ($translations as $langcode => $translation) {
  644. if ($translation['mlid'] !== $item['mlid']) {
  645. unset($form['i18n']['language']['#options'][$langcode]);
  646. }
  647. }
  648. }
  649. }
  650. }
  651. else {
  652. $form['language'] = array(
  653. '#type' => 'value',
  654. '#value' => $item['language'],
  655. );
  656. }
  657. if ($node_item && i18n_langcode($item['language'])) {
  658. $form['i18n']['message'] = array(
  659. '#type' => 'item',
  660. '#title' => t('Language'),
  661. '#markup' => i18n_language_name($item['language']),
  662. '#description' => t('This menu item belongs to a node, so it will have the same language as the node and cannot be localized.'),
  663. );
  664. }
  665. array_unshift($form['#validate'], 'i18n_menu_menu_item_prepare_normal_path');
  666. }
  667. /**
  668. * Implements hook_form_FORM_ID_alter().
  669. * FORM_ID = menu-overview-form.
  670. * Add a "translate" link in operations column for each menu item.
  671. */
  672. function i18n_menu_form_menu_overview_form_alter(&$form, &$form_state) {
  673. foreach (element_children($form) as $element) {
  674. if (substr($element, 0, 5) == 'mlid:') {
  675. $mlid = $form[$element]['#item']['mlid'];
  676. if (i18n_get_object('menu', $mlid)->get_translate_access()) {
  677. $form[$element]['operations']['translate'] = array(
  678. '#type' => 'link',
  679. '#title' => t('translate'),
  680. '#href' => "admin/structure/menu/item/{$mlid}/translate",
  681. );
  682. }
  683. }
  684. }
  685. }
  686. /**
  687. * Normal path should be checked with menu item's language to avoid
  688. * troubles when a node and it's translation has the same url alias.
  689. */
  690. function i18n_menu_menu_item_prepare_normal_path($form, &$form_state) {
  691. $item = &$form_state['values'];
  692. $item['link_path'] = i18n_prepare_normal_path($item['link_path'], $item['language']);
  693. }
  694. /**
  695. * Get language for menu item
  696. */
  697. function i18n_menu_item_get_language($item) {
  698. if (isset($item['language'])) {
  699. return $item['language'];
  700. }
  701. else {
  702. $menu = menu_load($item['menu_name']);
  703. if (!isset($menu['i18n_mode'])) {
  704. return LANGUAGE_NONE;
  705. }
  706. switch ($menu['i18n_mode']) {
  707. case I18N_MODE_LANGUAGE:
  708. return $menu['language'];
  709. case I18N_MODE_NONE:
  710. case I18N_MODE_LOCALIZE:
  711. return LANGUAGE_NONE;
  712. default:
  713. if (!empty($item['mlid'])) {
  714. return db_select('menu_links', 'm')
  715. ->fields('m', array('language'))
  716. ->condition('mlid', $item['mlid'])
  717. ->execute()
  718. ->fetchField();
  719. }
  720. else {
  721. return LANGUAGE_NONE;
  722. }
  723. }
  724. }
  725. }
  726. /**
  727. * Implements hook_form_node_form_alter().
  728. *
  729. * Add language to menu settings of the node form, as well as setting defaults
  730. * to match the translated item's menu settings.
  731. */
  732. function i18n_menu_form_node_form_alter(&$form, &$form_state, $form_id) {
  733. if (isset($form['menu'])) {
  734. $node = $form['#node'];
  735. $link = $node->menu;
  736. if (!empty($link['mlid'])) {
  737. // Preserve the menu item language whatever it is.
  738. $form['menu']['link']['language'] = array('#type' => 'value', '#value' => $link['language']);
  739. }
  740. elseif (i18n_menu_node_supported_type($node->type)) {
  741. // Set menu language to node language but only if it is a supported node type.
  742. $form['menu']['link']['language'] = array('#type' => 'value', '#value' => $node->language);
  743. }
  744. else {
  745. $form['menu']['link']['language'] = array('#type' => 'value', '#value' => LANGUAGE_NONE);
  746. }
  747. // Customized must be set to 1 to save language.
  748. $form['menu']['link']['customized'] = array('#type' => 'value', '#value' => 1);
  749. }
  750. }
  751. /**
  752. * Check whether a node type has multilingual support (but not entity translation).
  753. */
  754. function i18n_menu_node_supported_type($type) {
  755. $supported = &drupal_static(__FUNCTION__);
  756. if (!isset($supported[$type])) {
  757. $mode = variable_get('language_content_type_' . $type, 0);
  758. $supported[$type] = $mode == 1 || $mode == 2; // 2 == TRANSLATION_ENABLED
  759. }
  760. return $supported[$type];
  761. }
  762. /**
  763. * Get the node object for a menu item.
  764. */
  765. function i18n_menu_item_get_node($item) {
  766. return isset($item['router_path']) && $item['router_path'] == 'node/%' ? node_load(arg(1, $item['link_path'])) : NULL;
  767. }
  768. /**
  769. * Implements hook_node_presave()
  770. *
  771. * Set menu link language to node language
  772. */
  773. function i18n_menu_node_presave($node) {
  774. if (!empty($node->menu) && isset($node->language) && i18n_menu_node_supported_type($node->type)) {
  775. $node->menu['language'] = i18n_object_langcode($node, LANGUAGE_NONE);
  776. // Store node type with menu item so we can quickly access it later.
  777. $node->menu['options']['node_type'] = $node->type;
  778. }
  779. }
  780. /**
  781. * Implements hook_node_prepare_translation().
  782. */
  783. function i18n_menu_node_prepare_translation($node) {
  784. if (empty($node->menu['mlid']) && !empty($node->translation_source)) {
  785. $tnode = $node->translation_source;
  786. // Prepare the tnode so the menu item will be available.
  787. node_object_prepare($tnode);
  788. $node->menu['link_title'] = $tnode->menu['link_title'];
  789. $node->menu['weight'] = $tnode->menu['weight'];
  790. }
  791. }
  792. /**
  793. * Process menu and menu item add/edit form submissions.
  794. *
  795. * @todo See where this fits
  796. */
  797. /*
  798. function i18n_menu_edit_item_form_submit($form, &$form_state) {
  799. $mid = menu_edit_item_save($form_state['values']);
  800. db_query("UPDATE {menu} SET language = '%s' WHERE mid = %d", array($form_state['values']['language'], $mid));
  801. return 'admin/build/menu';
  802. }
  803. */
  804. /**
  805. * Load translation set. Menu loading callback.
  806. */
  807. function i18n_menu_translation_load($tsid) {
  808. return i18n_translation_set_load($tsid, 'menu_link');
  809. }
  810. /**
  811. * Load menu item by path, language
  812. */
  813. function i18n_menu_link_load($path, $langcode) {
  814. $query = db_select('menu_links', 'ml');
  815. $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  816. $query->fields('ml');
  817. // Weight should be taken from {menu_links}, not {menu_router}.
  818. $query->addField('ml', 'weight', 'link_weight');
  819. $query->fields('m');
  820. $query->condition('ml.link_path', $path);
  821. $query->condition('ml.language', $langcode);
  822. if ($item = $query->execute()->fetchAssoc()) {
  823. $item['weight'] = $item['link_weight'];
  824. _menu_link_translate($item);
  825. return $item;
  826. }
  827. }
  828. /**
  829. * Implements hook_query_TAG_alter() for features_menu_links.
  830. * Add needed fields to properly serialize localization information.
  831. */
  832. function i18n_menu_query_features_menu_link_alter($query) {
  833. $query->fields('menu_links', array('language', 'customized'));
  834. }
  835. /**
  836. * Implements hook_init().
  837. */
  838. function i18n_menu_init() {
  839. // The only way to override the default preferred menu link for a path is to
  840. // inject it into the static cache of the function menu_link_get_preferred().
  841. // The problem with the default implementation is that it does not take the
  842. // language of a menu link into account. Whe having different menu trees for
  843. // different menus, this means that the active trail will not work for all but
  844. // one language.
  845. // The code below is identical to the mentioned function except the added
  846. // language condition on the query.
  847. // TODO: Adding an alter tag to the query would allow to do this with a simple
  848. // hook_query_alter() implementation.
  849. $preferred_links = &drupal_static('menu_link_get_preferred');
  850. $path = $_GET['q'];
  851. // Look for the correct menu link by building a list of candidate paths,
  852. // which are ordered by priority (translated hrefs are preferred over
  853. // untranslated paths). Afterwards, the most relevant path is picked from
  854. // the menus, ordered by menu preference.
  855. $item = menu_get_item($path);
  856. $path_candidates = array();
  857. // 1. The current item href.
  858. $path_candidates[$item['href']] = $item['href'];
  859. // 2. The tab root href of the current item (if any).
  860. if ($item['tab_parent'] && ($tab_root = menu_get_item($item['tab_root_href']))) {
  861. $path_candidates[$tab_root['href']] = $tab_root['href'];
  862. }
  863. // 3. The current item path (with wildcards).
  864. $path_candidates[$item['path']] = $item['path'];
  865. // 4. The tab root path of the current item (if any).
  866. if (!empty($tab_root)) {
  867. $path_candidates[$tab_root['path']] = $tab_root['path'];
  868. }
  869. // Retrieve a list of menu names, ordered by preference.
  870. $menu_names = menu_get_active_menu_names();
  871. // Use an illegal menu name as the key for the preferred menu link.
  872. $selected_menu = MENU_PREFERRED_LINK;
  873. // Put the selected menu at the front of the list.
  874. array_unshift($menu_names, $selected_menu);
  875. $query = db_select('menu_links', 'ml', array('fetch' => PDO::FETCH_ASSOC));
  876. $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  877. $query->fields('ml');
  878. // Weight must be taken from {menu_links}, not {menu_router}.
  879. $query->addField('ml', 'weight', 'link_weight');
  880. $query->fields('m');
  881. $query->condition('ml.link_path', $path_candidates, 'IN');
  882. // Only look menu links with none or the current language.
  883. $query->condition('ml.language', array(LANGUAGE_NONE, i18n_language_interface()->language), 'IN');
  884. // Sort candidates by link path and menu name.
  885. $candidates = array();
  886. foreach ($query->execute() as $candidate) {
  887. $candidate['weight'] = $candidate['link_weight'];
  888. $candidates[$candidate['link_path']][$candidate['menu_name']] = $candidate;
  889. // Add any menus not already in the menu name search list.
  890. if (!in_array($candidate['menu_name'], $menu_names)) {
  891. $menu_names[] = $candidate['menu_name'];
  892. }
  893. }
  894. // Store the most specific link for each menu. Also save the most specific
  895. // link of the most preferred menu in $preferred_link.
  896. foreach ($path_candidates as $link_path) {
  897. if (isset($candidates[$link_path])) {
  898. foreach ($menu_names as $menu_name) {
  899. if (empty($preferred_links[$path][$menu_name]) && isset($candidates[$link_path][$menu_name])) {
  900. $candidate_item = $candidates[$link_path][$menu_name];
  901. $map = explode('/', $path);
  902. _menu_translate($candidate_item, $map);
  903. if ($candidate_item['access']) {
  904. $preferred_links[$path][$menu_name] = $candidate_item;
  905. if (empty($preferred_links[$path][MENU_PREFERRED_LINK])) {
  906. // Store the most specific link.
  907. $preferred_links[$path][MENU_PREFERRED_LINK] = $candidate_item;
  908. }
  909. }
  910. }
  911. }
  912. }
  913. }
  914. }