0

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.

4 Answers 4

1

If you check this page https://www.drupal.org/node/223430 you will see that template preprocess hooks run before module preprocess hooks. This means that language is switched after you have rendered your mobile menu.

1

It's not strange it's a question of order.
You use theme_preprocess_page and module_preprocess_page.
theme_preprocess_page runs first, this mean you save your menu before you change it.
Easy option would be, to add your theme_preprocess_page part to your module_preprocess_page.
(Set it outside of the if(drupal_multilingual()) {} and it run even if the if return false)
Better option would be to create a module for your mobile menu, and set it's weight so it run after your other module.

EDIT: Ignore the Part about the order this https://www.drupal.org/node/223430 confussed me so i thought about template_preprocess_hook not the theme_preprocess_hook.
The Problem is your call, you think you add the switcher to the menu. But you just add temp items. On your call for mobile you get the original Menu (so no lang switch).
You could fix it by adding the lang fix to the menu and not temp.
You could extend your module so you would get the new Menu for your vars.

2
  • Do you think you can edit your answer and add an example showing how to achieve my desired result? Commented Jun 22, 2015 at 11:26
  • I try to set up something similar to get you some code
    – Nugorra
    Commented Jun 22, 2015 at 15:17
1

Try out this module Language Switcher . I hope this may help as you expected

1

I managed to achieve my desired result by using theme_menu_link():

function <THEME>_menu_link(array $variables) {
  $element = $variables ['element'];
  $sub_menu = '';

  if($element ['#below']) {
    $sub_menu = drupal_render($element ['#below']);
  }

  // check so this is the top menu and this link is the last one
  $add_language = (($element['#theme'] == 'menu_link__menu_top_menu' ||  
                    $element['#theme'][0] == 'menu_link__menu_block__2') && 
                    in_array('last', $element['#attributes']['class']) ? 1 : '');
  if($add_language != '') {
    // 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'], $lang);
              $info = path_load(array('alias' => $alias));
              if(drupal_is_front_page()) {
                $alias = '';
              }
              $item = 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)
              );
            }
          }
        }
      }
    }
    $add_language = l($item['#title'], $item['#href'], $item['#localized_options']);
    $add_language = '<li>'.$add_language.'</li>';
  }

  $output = l($element ['#title'], $element ['#href'], $element ['#localized_options']);
  return '<li' . drupal_attributes($element ['#attributes']) . '>' . $output . $sub_menu . "</li>\n".$add_language;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.