i18n_menu.module 34 KB

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