I'm trying to add a language switcher to one of my menus using the hook_preprocess_page()
:
function <MODULE>_preprocess_page(&$variables) {
// append language switcher to top_menu
if(drupal_multilingual()) {
global $language;
include_once DRUPAL_ROOT . '/includes/language.inc';
$info = language_types_info();
$lang_types = language_types_configurable(FALSE);
$path = drupal_is_front_page() ? '' : $_GET['q'];
foreach($lang_types as $type) {
$languages = language_negotiation_get_switch_links($type, $path);
if(isset($languages->links)) {
foreach($languages->links as $lang => $link) {
if ($lang != $language->language) {
$alias = drupal_get_path_alias($link['href']);
$info = path_load(array('alias' => $alias));
if(drupal_is_front_page()) {
$alias = '';
}
$variables['page']['top_menu']['menu_block_2']['#content'][] = array(
'#theme' => array('menu_link'),
'#title' => $link['title'],
'#href' => $alias,
'#attributes' => array(),
'#query' => array('language' => 'sv'),
'#localized_options' => array('query' => $link['query']),
'#below' => array(),
'#original_link' => array(),
'#bid' => array('module' => 'menu_block', 'delta' => 2)
);
}
}
}
}
}
}
I display the normal top menu in my page.tpl.php
like this:
<div class="normal-menu">
<?php if(isset($page['top_menu'])): ?>
<div class="top-menu">
<?php print render($page['top_menu']); ?>
</div>
</div>
The above code works fine for my normal menu, but I also have a mobil menu which content I store in the following two variables:
function <THEME>_preprocess_page(&$variables) {
$main_menu_tree = menu_tree_all_data('main-menu');
$variables['main_menu_expanded'] = menu_tree_output($main_menu_tree);
$top_menu_tree = menu_tree_all_data('menu-top-menu');
$variables['top_menu_expanded'] = menu_tree_output($top_menu_tree);
}
Then in my page.tpl.php
file I output the contents for my mobil menu like this:
<div id="mobile-menu">
<?php print render($main_menu_expanded); ?>
<?php print render($top_menu_expanded); ?>
</div>
The strange thing is that the language switcher isn't added to the mobile menu, while it turns up without any problems in the normal menu.