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