I am using the i18n module (with all submodules activated incl. Menu Translation).

My default language is german (without prefix). English is the second language (en as prefix).
Menu Block is working with default language but if I am switching to the secondary language then the block dissapears. Everything else on the site is working fine, only Menu Block seems to be affected.

working:
www.domain.com/produkte/title

not working:
www.domain.com/en/produkte/title

thanks in advance!

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drupalninja99’s picture

Status: Active » Patch (to be ported)
FileSize
1023 bytes

I have struggle with menu blocks that follow the active menu tree bc the menu prune function in menu_block does not work with i18n_menu in drupal 7. So I created a small patch that forces the menu_tree_prune_tree() function to lookup the source node id when it's checking to see if the current page is in the active trail.

Without this patch for translated nodes it will never work and also return nothing. I don't think this is a problem for view pages, etc. other pages that don't change their paths like nodes do. This patch works great for me, the menu gets pruned as expected.

Goekmen’s picture

Thanks for the patch, meanwhile I uninstalled Menu Block and used the Context Module.
But good to know for future projects :-)

andreiashu’s picture

Status: Patch (to be ported) » Needs work

Hi and thanks for the patch.
Unfortunately the patch from #1 only works for 1 level items. Also I don't think its place is in menu_tree_prune_tree function. I still need to investigate but this definitely needs more work

andreiashu’s picture

Version: 7.x-2.3 » 7.x-2.x-dev
Status: Needs work » Needs review
FileSize
646 bytes

Attached patch is against 2.x-dev. Let me know if it fixes your problem.

andreiashu’s picture

FileSize
849 bytes

Attached an updated patch which makes sure that the rendered menu is the one in the current $node object when adding the active link.

andreiashu’s picture

FileSize
824 bytes

Attached a version against the latest stable 2.3 version

nateman332’s picture

Status: Needs review » Needs work

#4, #5 and #6 aren't working for stable version 2.3 for me; menu items are still not showing on secondary language.

nateman332’s picture

This patch worked for me! All you do is delete this line of code in sites/all/modules/i18n/i18n_menu/i18n_menu.module:

$query->condition('ml.menu_name', $menu_names, 'IN');
pierrozone’s picture

Version: 7.x-2.x-dev » 7.x-2.3

Patch #6 Displays menu block for default language but doesn't show child items and doesn't also show menu block in other languages

nateman332’s picture

Status: Needs work » Needs review
constantinejohny’s picture

#8 Worked for me for translated content, paren and child items. Thanks!

JohnAlbin’s picture

Status: Needs review » Needs work

Patch #6 Displays menu block for default language but doesn't show child items and doesn't also show menu block in other languages

Needs work then.

muschpusch’s picture

Sorry this isn't really related to menu_block but if people look for a workaround for rendering a second level menu (language aware)

Let's say your menu looks like this:

Main Item (english)
-- sub item (english)
-- sub item (english)
-- sub item (english)

Main Item (german)
-- sub item (german)
-- sub item (german)
-- sub item (german)

/* 
 * this could be your hook_block_view callback
 */
function _custom_callback_user_menu($menu_name){
  $tree = menu_tree_page_data($menu_name);
  $langcode = i18n_langcode();
  /* get the correct language to render */
  foreach($tree as $key => $value ){
    if ($langcode == $tree[$key]['link']['language']){
      $i18n_tree = $tree[$key]['below'];
    }
  }
  $user_menu = menu_tree_output($i18n_tree, 1);
  return drupal_render($user_menu);
}

It will render
-- sub item (german)
-- sub item (german)
-- sub item (german)

OR

-- sub item (english)
-- sub item (english)
-- sub item (english)

jetcode’s picture

Issue summary: View changes

Seems like this bug hasn't been fixed yet. Here is my workaround (ugly but should works and that would be awesome if someone has an idea how to improve this!!!).
In a custom module, just implement hook_menu_block_tree_alter() and add this:

function mymodule_menu_block_tree_alter(&$tree, &$config) {
if ($config['menu_name'] && $config['parent_mlid']) {
    global $language;
    $tree = menu_tree_all_data($config['menu_name']);
    $link = menu_link_load($config['parent_mlid']);
    if ($tsid = $link['i18n_tsid']) {
      foreach ($tree as $item) {
        if ($tsid == $item['link']['i18n_tsid'] && $item['link']['language'] == $language->language) {
          $config['parent_mlid'] = $item['link']['mlid'];
        }
      }
    }
  }
}

This way, all your menu blocks that display a menu starting from depth > 0 will be dynamic depending on the current language. Menu block with menu starting from root level (depth = 0) are not concerned.

Chandan Chaudhary’s picture

I was facing the same issue, as the translated menu items were not displaying on the respective language page.

The below code solves my problem

if (module_exists('i18n_menu')) {
   $menu_object = menu_load($config['menu_name']);
    if($menu_object['i18n_mode'] == '5')  {
    if (!empty($config['parent_mlid'])) {
      $item = menu_link_load($config['parent_mlid']);
      $set = i18n_translation_object('menu_link', $item);
      if (!empty($set)) {
        $translation_set = $set->get_translations();
        global $language;
        if (isset($translation_set[$language->language]) && !empty($translation_set[$language->language]['mlid'])) {
          $config['parent_mlid'] = $translation_set[$language->language]['mlid'];
        }
      }
    } 
   }
  }

i have also attached the patch file as "menu_block_translation_fix.patch"

Chandan Chaudhary’s picture

Priority: Normal » Major
Lukey’s picture

I have been working on an issue where the menu_block cannot decide which language the active trail should be in.

For example, if I am on the english (default language) version of the page, the menu_block for this secondary level menu does not show up. dpm() of the $tree shows that the chinese menu items are getting active trail = TRUE when it should be the english translation of these menu items that is active. When it comes time to output the menu, only chinese items are in the menu tree, so nothing shows up at all and the block disappears since it is an english page.

The below seems to work to fix, although obviously recursing twice is nuts but I haven't had a chance to fix that problem yet. Basically $active_hack is storing the parent items so I can loop through again and set those to active_trail.

This solved the problem - any suggestions on edits to travel backwards from the active item so that I don't have to iterate once to find the active trail, and again to edit each link to set to active trail?

function mymodule_menu_block_tree_alter(&$tree, &$config){
  
  if($config['menu_name'] == 'main-menu'){
    $options = array();
    $active_hack = array();
    //intentionally running this twice for now :(
    mymodule_menu_block_tree_alter_recurse($tree, $config['menu_name'], $options, array(), 0, 100, $active_hack);
    mymodule_menu_block_tree_alter_recurse($tree, $config['menu_name'], $options, array(), 0, 100, $active_hack);
  }
}


function mymodule_menu_block_tree_alter_recurse(&$tree, $menu_name, $indent, $options, $exclude, $depth_limit, &$active_hack){
  global $language;
  foreach ($tree as $key => $data) {
    if ($data['link']['depth'] > $depth_limit) {
      // Don't iterate through any links on this level.
      break;
    }

    if ($data['link']['mlid'] != $exclude && $data['link']['hidden'] >= 0) {
      if(($data['link']['link_path'] == $_GET['q'] && $language->language == $data['link']['language']) || in_array($data['link']['mlid'], $active_hack)){
        $tree[$key]['link']['in_active_trail'] = TRUE;
        for($i = 1; $i < 11; $i++){
          $p = $tree[$key]['link']['p' . $i];
          if($p > 0){
            $active_hack[] = $p;
          }
          
        }
      }
      if ($data['below']) {
        who_pq_base_menu_block_tree_alter_recurse($tree[$key]['below'], $menu_name, '', $options, $exclude, $depth_limit, $active_hack);
      }
    }
  }
}