I have a rule:

Name: node
Action with breadcrumbs: add parents
Action: leave

When I use this rule the breadcrumb prints parent items in the breadcrumb but that items aren't translated. Also their url's links to the untranslated menu item.

I search a little bit more and see that the function menu_hansel_get_parent in hansel.actions.inc doesn't support localization at all. The logic for geting menu item only works with unlocalized drupal. So I make a modification for that function and now it is working very well. Also it works with the domain module and multiple menus. I tested it on my live site.

Here is modified function:

/**
 * Implements hook_hansel_get_parent().
 * 
 * @param string $path 
 * @return array
 */
function menu_hansel_get_parent($path) {

  global $language;
  if (module_exists('menu')) {
  	
  	$trail = menu_get_active_trail ();
  	
  	$active_menu_item = array_pop($trail);
  	
  	$active_menu_name = $active_menu_item["menu_name"];

    // Try to get parent by menu.
    $sql = 'SELECT p.link_path, p.link_title
    FROM {menu_links} c
    JOIN {menu_links} p ON c.plid = p.mlid 
    WHERE c.link_path = :path AND c.menu_name = :active_menu AND ( c.language = :current_language OR  c.language = :default_language ) 
    ORDER BY c.language ASC 
    LIMIT 1'; 

    $res = db_query($sql, array(
    	':path' => $path, 
    	':active_menu' => $active_menu_name,
    	':current_language' => $language->language,
    	':default_language' => "und",
    ));

    if ($link = $res->fetchObject()) {
      return array(
        'path' => $link->link_path,
        'title' => $link->link_title,
      );
    } 
  }
    
  if (preg_match('/^node\\/([0-9]+)$/si', $path, $match)) {

    // Try to get parent by nodetype settings.
    $nodetypes = variable_get('hansel_nodetypes', array());

	$node = node_load($match[1]);

    if (($node = node_load($match[1])) && isset($nodetypes[$node->type])) {
      return $nodetypes[$node->type];
    }
  }
  
  return FALSE;
}

Can someone make a patch and testi it and commit it to dev so other can bedifit form this?

Comments

mauritsl’s picture

Thanks for investigating this issue!

Multilanguage support is indeed weak in Hansel. There some other issues about this as well. I will try to find time soon to test all its features with i18n.

blackandcode’s picture

Thanks for fast response. I also applied patch from http://drupal.org/node/1600700 for localized taxonomies. This patch also work good.

mattew’s picture

This fix no longer works if the menu item is in Language Neutral and is localized (menu in "Translate and localize" mode) using i18n_string.

Here is a fix for that (pick the code above, and change the if ($link = $res->fetchObject()) {...} closure):

/**
 * Implements hook_hansel_get_parent().
 *
 * @param string $path
 * @return array
 */
function menu_hansel_get_parent($path) {

  global $language;
  if (module_exists('menu')) {

    $trail = menu_get_active_trail ();

    $active_menu_item = array_pop($trail);

    $active_menu_name = $active_menu_item["menu_name"];

    // Try to get parent by menu.
    $sql = 'SELECT p.link_path, p.link_title, p.mlid, p.language
      FROM {menu_links} c
      JOIN {menu_links} p ON c.plid = p.mlid
      WHERE c.link_path = :path AND c.menu_name = :active_menu AND ( c.language = :current_language OR c.language = :default_language )
      ORDER BY c.language ASC
      LIMIT 1';

    $res = db_query($sql, array(
        ':path' => $path,
        ':active_menu' => $active_menu_name,
        ':current_language' => $language->language,
        ':default_language' => "und",
    ));

    if ($link = $res->fetchObject()) {

      $i18n_mode = i18n_menu_mode($active_menu_name);
      if( $i18n_mode == '5' && $link->language == 'und' ) {
        /* Menu is in "Translate and localise" mode and the menu item is "Language neutral" (translatable) */
        $link_title = i18n_string_translate(array('menu', 'item', $link->mlid, 'title'), $link->link_title, array('langcode' => $language->language, 'sanitize' => FALSE));
      } else {
        /* Menu is in "Fixed language" mode, or item is localized (not in "Language neutral") */
        $link_title = $link->link_title;
      }

      return array(
        'path' => $link->link_path,
        'title' => $link_title,
      );
    }
  }

  if (preg_match('/^node\\/([0-9]+)$/si', $path, $match)) {

    // Try to get parent by nodetype settings.
    $nodetypes = variable_get('hansel_nodetypes', array());

    $node = node_load($match[1]);

    if (($node = node_load($match[1])) && isset($nodetypes[$node->type])) {
      return $nodetypes[$node->type];
    }
  }

  return FALSE;
}

Tested and works for me.

mauritsl’s picture

Status: Active » Needs review
StatusFileSize
new3.8 KB

It breaks the site when i18n_menu is not enabled. And secondly, modified code doesn't follow the Drupal coding standards.

Patch attached should address this issues, but still needs some testing.

mattew’s picture

StatusFileSize
new4.57 KB

New patch for the i18n fix :

  1. Updated for the last dev
  2. The last dev implements new code in the hansel_action_add_link_to_node_get_crumbs() function, this code does not care about i18n! => Update this code with the same pattern as the menu_hansel_get_parent() function.

To the maintainer: please, apply the patch, think about multilingual websites!

  • mauritsl committed 180cc1d on 7.x-1.x
    Issue #1786662 by mattew, blackandcode: Fix i18n issues with add parents...
mauritsl’s picture

Status: Needs review » Fixed

Committed to GIT. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.