I am building a bilingual site that's English and French. When I add a content item in English, then go to Menu Settings, click on Provide a menu link, and click on Parent Item drop down menu, I get all the menu items English and French. How do I get the drop down menu item to display only English menu items if my content is in English language?
4 Answers
My experience is that user 1 (admin) sees all menu items no matter their language setting, but i'm not sure if it's meant to be or if it's a bug.
Better have a look at the Issues of i18n and in particular at Drupal 7 & i18n: Only current language items visible in Menu Settings / Parent menu item
No one gave me an answer, so I decided to modify the core, which is completely pathetic. In hacking the core, I was able to add a class="en"
or class="fr"
to the option of a select menu. Now I can use jquery to hide and show options as needed. Here's a git diff of changes I made to the core system.
diff --git a/includes/form.inc b/includes/form.inc
index a337b03..55f705f 100644
--- a/includes/form.inc
+++ b/includes/form.inc
@@ -2564,7 +2564,14 @@ function form_select_options($element, $choices = NULL) {
else {
$selected = '';
}
- $options .= '<option value="' . check_plain($key) . '"' . $selected . '>' . check_plain($choice) . '</option>';
+ $choice_text = check_plain($choice);
+ $class=' class="opt"';
+ if(preg_match('/\{\{\w{2}\}\}/',$choice_text,$matches))
+ {
+ $class = ' class="'.preg_replace('/[^\w]/','',$matches[0]).'"';
+ $choice_text = preg_replace('/\s\{\{\w{2}\}\}/','',$choice_text);
+ }
+ $options .= '<option value="' . check_plain($key) . '"' . $selected . $class.'>' . $choice_text . '</option>';
}
}
return $options;
diff --git a/modules/menu/menu.module b/modules/menu/menu.module
index fc8f68a..b991970 100644
--- a/modules/menu/menu.module
+++ b/modules/menu/menu.module
@@ -408,7 +408,7 @@ function _menu_parents_recurse($tree, $menu_name, $indent, &$options, $exclude,
if ($data['link']['hidden']) {
$title .= ' (' . t('disabled') . ')';
}
- $options[$menu_name . ':' . $data['link']['mlid']] = $title;
+ $options[$menu_name . ':' . $data['link']['mlid']] = $title.' {{'.$data['link']['language'].'}}';
if ($data['below']) {
_menu_parents_recurse($data['below'], $menu_name, $indent . '--', $options, $exclude, $depth_limit);
}
diff --git a/themes/seven/seven.info b/themes/seven/seven.info
index 599879f..b625854 100644
--- a/themes/seven/seven.info
+++ b/themes/seven/seven.info
@@ -12,6 +12,7 @@ regions[page_top] = Page top
regions[page_bottom] = Page bottom
regions[sidebar_first] = First sidebar
regions_hidden[] = sidebar_first
+scripts[] = lang.js
; Information added by drupal.org packaging script on 2011-05-25
version = "7.2"
And I added a js file called /themes/seven/lang.js which toggles the options to appropriate language.
if (typeof jQuery == 'function'){
jQuery(document).ready(function($) {
$('#edit-language').change(function(){
if($(this).val() != 'und')
{
$('#edit-menu-parent option').css('display','none');
$('#edit-menu-parent .'+$(this).val()+', #edit-menu-parent .opt').css('display','block');
}
});
});
}
Please suggest a better way to achieve the same objective as this answer.
-
-
-
admin is normally user 1, you can double check if you edit your account.– AttiksAug 18, 2011 at 20:53
After struggling on issue my solution was:
- Enable -> Multilingual select (module)
- Go To: admin/config/regional/i18n/select
- Check the Select nodes by language
-
-
-
I follow the instruction but I still have all the menu for every language...– gagarineSep 26, 2012 at 13:26
I think you can use Drupal's hook_form_alter to filter out items that are not in the user's language or neutral.
It would be something like this:
function hook_form_alter(&$form, &$form_state, $form_id) {
global $user;
foreach ($form['menu']['link']['parent']['#options'] as $key => $option) {
$keyArray = explode(':', $key);
$mid = $keyArray[1];
$node = menu_node_get_node($mid);
if ($node && ($node->language !== $user->language && $node->language !== 'und')) {
unset($form['menu']['link']['parent']['#options'][$key]);
}
}
}
Terribly late answer, but probably relevant for people finding this on Google today.
-
You're assuming the $form has a menu attribute. What is the form id in this case? Aug 18, 2015 at 19:04