after adding a url-aslias to a top-level menu item having subitems, this menu isn't expanding anymore. therefor i cannot select the subitems anymore.

Comments

Roberto Gerola’s picture

I have encountered the same problem.
Because the module overwrite the links adding the locale prefix, then Drupal cannot understand which menu
is selected.
I have solved, temporarly, creating a menu from scratch, not using menu module of Drupal, but it isn't a good solution, I admit.
Some patches to correct these problems have been released, but no one work for me.
An idea could be to create many different menus for every language and activate the correct block depending on the current language.

If you are interested, I've begun to write a new module to solve these type of problems :
http://drupal.org/node/81525
It isn't yet finished, but the basic features work correctly.

marc.bau’s picture

In general your module looks like well working! One thing that you may have not thought about and is very important - you are working with a session var and what will happen now with spiders? they will only spider your english content, while they are detected in this way - of detection is activated. spiders don't use cookies, so they will not follow your language switcher and get the content in different languages. This is a very big problem and for me a killer critera!

maybe you find a way to move the language to the hostname. My idea is "es.domain.com" or "en.domain.com"... then you are out of spiders pain, but possibly get other problems...

so - i think, this tons of bugs should better be fixed in i18n, however unusable and buggy this module is today.

Roberto Gerola’s picture

Hi Marc.
Thank you for the hint.

I have updated the module.
Now it works correctly also with cache enabled (with two little changes to two system Drupal files).
And I've tried to make it spiders-friendly.
Substantially every page now has a link to its translated versions in the form : http://www.example.com?locale=it

If you are interested, I have published a post on my blog :
http://www.speedtech.it/drupal/localizermodule

Bye

marc.bau’s picture

but you know - how difficult it is to get something in a search engine with url params attached?

This is why many websites have spider friendly urls without any url param... however dynamic the page is :-)

Roberto Gerola’s picture

Here there is a rewriting of the function menu_set_active_item located in menu.inc file that should fix
this behaviour.
(In the next days I'll prepare a patch file to submit)
Let me know !

function menu_set_active_item($path = NULL) {
  static $stored_mid;

  if (!isset($stored_mid) || isset($path)) {
    $menu = menu_get_menu();
    if (!isset($path)) {
      $path = $_GET['q'];
    }
    else {
      $_GET['q'] = $path;
    }

		static $valid_locales;
		$valid_locales = localizer_supported_languages();

		$orig_pathalias = drupal_lookup_path('alias', $path);
    $exploded_path = explode('/', $orig_pathalias);
    $localeinpath = $exploded_path[0];
    if(!(($localeinpath!= '') && array_key_exists($localeinpath, $valid_locales))){
    	$localeinpath = '';
    }

    if($localeinpath != ''){
    	array_shift($exploded_path);
      $pathalias_witoutlocale=implode("/", $exploded_path);
    }
    else
    {
    	$pathalias_witoutlocale=$orig_pathalias;
    }

		$new_path = drupal_get_normal_path($pathalias_witoutlocale);
		if($new_path != $pathalias_witoutlocale) {
			$path=$new_path;
		}
		else
		{
			$new_path = drupal_get_normal_path('en/' . $pathalias_witoutlocale);			
			if($new_path != 'en/' . $pathalias_witoutlocale) {
				$path=$new_path;
			}
		}

    while ($path && !isset($menu['path index'][$path])) {
      $path = substr($path, 0, strrpos($path, '/'));
    }
    $stored_mid = isset($menu['path index'][$path]) ? $menu['path index'][$path] : 0;

    // Search for default local tasks to activate instead of this item.
    $continue = TRUE;
    while ($continue) {
      $continue = FALSE;
      if (isset($menu['items'][$stored_mid]['children'])) {
        foreach ($menu['items'][$stored_mid]['children'] as $cid) {
          if ($menu['items'][$cid]['type'] & MENU_LINKS_TO_PARENT) {
            $stored_mid = $cid;
            $continue = TRUE;
          }
        }
      }
    }

    // Reset the cached $menu in menu_get_item().
    menu_get_item(NULL, NULL, TRUE);
  }

  return $stored_mid;
}
Roberto Gerola’s picture

Sorry, a mistake.

Here there is the correct code :

function menu_set_active_item($path = NULL) {
static $stored_mid;

if (!isset($stored_mid) || isset($path)) {
$menu = menu_get_menu();
if (!isset($path)) {
$path = $_GET['q'];
}
else {
$_GET['q'] = $path;
}

static $valid_locales;
$valid_locales = locale_supported_languages();
$valid_locales = $valid_locales['name'];

$orig_pathalias = drupal_lookup_path('alias', $path);
$exploded_path = explode('/', $orig_pathalias);
$localeinpath = $exploded_path[0];
if(!(($localeinpath!= '') && array_key_exists($localeinpath, $valid_locales))){
$localeinpath = '';
}

if($localeinpath != ''){
array_shift($exploded_path);
$pathalias_witoutlocale=implode("/", $exploded_path);
}
else
{
$pathalias_witoutlocale=$orig_pathalias;
}

$new_path = drupal_get_normal_path($pathalias_witoutlocale);
if($new_path != $pathalias_witoutlocale) {
$path=$new_path;
}
else
{
$new_path = drupal_get_normal_path('en/' . $pathalias_witoutlocale);
if($new_path != 'en/' . $pathalias_witoutlocale) {
$path=$new_path;
}
}

while ($path && !isset($menu['path index'][$path])) {
$path = substr($path, 0, strrpos($path, '/'));
}
$stored_mid = isset($menu['path index'][$path]) ? $menu['path index'][$path] : 0;

// Search for default local tasks to activate instead of this item.
$continue = TRUE;
while ($continue) {
$continue = FALSE;
if (isset($menu['items'][$stored_mid]['children'])) {
foreach ($menu['items'][$stored_mid]['children'] as $cid) {
if ($menu['items'][$cid]['type'] & MENU_LINKS_TO_PARENT) {
$stored_mid = $cid;
$continue = TRUE;
}
}
}
}

// Reset the cached $menu in menu_get_item().
menu_get_item(NULL, NULL, TRUE);
}

return $stored_mid;
}
Bodo Maass’s picture

Thanks for the patch. This looks very promising and does fix the secondary menus, but for me it stops the admin menu from expanding. I have the Navigation block enabled, and this block contains the admin menu. The secondary menu is in a separate block. So while the secondary menu expands now, the admin menu in the Navigation block doesn't.

Bodo Maass’s picture

Here is a slight modification that seems to fix the admin menu for me. This works in 4.7.3:

function menu_set_active_item($path = NULL) {
  static $stored_mid;

  if (!isset($stored_mid) || isset($path)) {
    $menu = menu_get_menu();
    if (!isset($path)) {
      $path = $_GET['q'];
    }
    else {
      $_GET['q'] = $path;
    }

    $orig_pathalias = drupal_lookup_path('alias', $path);
    if ($orig_pathalias) {
        static $valid_locales;
        $valid_locales = locale_supported_languages();
        $valid_locales = $valid_locales['name'];

        $exploded_path = explode('/', $orig_pathalias);
        $localeinpath = $exploded_path[0];
        if(!(($localeinpath!= '') && array_key_exists($localeinpath, $valid_locales))){
          $localeinpath = '';
        }

        if($localeinpath != ''){
          array_shift($exploded_path);
          $pathalias_witoutlocale=implode("/", $exploded_path);
        }
        else
        {
          $pathalias_witoutlocale=$orig_pathalias;
        }

        $new_path = drupal_get_normal_path($pathalias_witoutlocale);
        if($new_path != $pathalias_witoutlocale) {
          $path=$new_path;
        }
        else
        {
          $new_path = drupal_get_normal_path('en/' . $pathalias_witoutlocale);
          if($new_path != 'en/' . $pathalias_witoutlocale) {
            $path=$new_path;
          }
        }
    }
    while ($path && !isset($menu['path index'][$path])) {
      $path = substr($path, 0, strrpos($path, '/'));
    }
    $stored_mid = isset($menu['path index'][$path]) ? $menu['path index'][$path] : 0;

    // Search for default local tasks to activate instead of this item.
    $continue = TRUE;
    while ($continue) {
      $continue = FALSE;
      if (isset($menu['items'][$stored_mid]['children'])) {
        foreach ($menu['items'][$stored_mid]['children'] as $cid) {
          if ($menu['items'][$cid]['type'] & MENU_LINKS_TO_PARENT) {
            $stored_mid = $cid;
            $continue = TRUE;
          }
        }
      }
    }
    // Reset the cached $menu in menu_get_item().
    menu_get_item(NULL, NULL, TRUE);
  }

  return $stored_mid;
}
Bodo Maass’s picture

Finally, this depends on the locale module to be enabled and will cause errors if locale is turned off. To allow a more graceful degradation, it should check first if locale is available, so the line

  if ($orig_pathalias)

should be:

    if ($orig_pathalias && function_exists('locale_supported_languages')) {
Roberto Gerola’s picture

StatusFileSize
new22.15 KB

Hi.
In the past days I've worked hard to figure out a way to remove this patch from Drupal core from my localizer module.
I don't use anymore i18n module but I was glad to share my code with you.

The important code is located and the function : localizer_menu_translate_all.
When the module translates the menu it also determine which items to expand
and simply change the type of the menu.

With this new function, also the patch to common.inc l() function is no more necessary
because it changes on the fly the path of the menu item.

I've attached the source code of my module if someone is interested to use some pieces
in i18n module.

joestewart’s picture

The code in #8 does expand the menu however a side effect is the local task tabs ( view, edit, etc) are not shown now.

Any quick hints to get them to display as well? Or do others not have the same problem?

Jose - Is the menu not setting the active item ( not expanding) a confirmed bug now? What are plans to fix? On the site we are implementing this it totally breaks navigation without the fix. But with the patch administration is hindered greatly.

thanks,

Joe

karunadev’s picture

I can confirm that the menu "current", "active" state is not being set with Internationalization in place.

What was once:

Has become:

karunadev’s picture

Lets try this again. Yes the active state is not working.

What was once
<li id="primary-link-1-current" > <a href="/about_khadro" title="How is Khadro?" class="active">About Khadro</a> </li>

Is now:
<li id="primary-link-1" > <a href="/en/about_khadro" title="How is Khadro?">About Khadro</a> </li>

orospakr’s picture

Title: menu not expanding with URL-Alias » A fix for both the active item issue and missing primary tabs issues.
StatusFileSize
new8.03 KB

Hi folks!

With the addition of a few more hacks, this patch takes a stock Drupal 4.7.4 () menu.inc and fixes both the missing tabs issue and the missing active highlight on translated nodes. I did it by creating a second copy of the menu_set_active_item() and menu_get_active_nontask_item() functions that contain the original stock logic. I then modified all the menu_* functions that are involved in producing the primary links to use the the original functions instead. It's not very pretty, but it gets the job done while we wait for a proper fix for menus and URL paths and i18n in general.

Yes, I know .5 is out, I imagine this should apply OK with maybe a hunk or two though.

This patch is not at all appropriate for any kind of merging, and the proper solution to this problem would be to have the menu system refactored so it no longer relies on paths to link together the menu hierarchy (and *cough* properly integrated i18n of some kind in Drupal core would be nice).

Good luck!

jose reyero’s picture

I'm a bit confused about all this. Are you talking here about path aliases with language prefix or without?

Would you please add actual path samples?

The fact that path aliases with language prefix have some problems with menu expansion and active links is a known issue. However I am not sure whether you're talking here about normal aliases of this other ones, with language prefix.

Whatever, the policy for i18n module since a while ago is that we won't add drupal patches into the module but anyway if someone wants to help compiling a list of available patches, will be welcomed.

I'm putting together some extensive documentation for i18n on Drupal 5. Path aliases with language prefixes will be an unsupported 'you-can-use-it-but-has-some-problems' feature for now. Thus the solution atm is to use different aliases for each language.

orospakr’s picture

I think it will only work on language path aliases, not custom ones, given the nature of Roberto and bodomaas' original patch.

It's all a nasty hack to begin with. Drupal's menu system is ass and really needs to be refactored/rewritten.

As I said before, this patch is only meant to tide people over until that day. It is not of appropriate quality to be merged into anything.

orospakr’s picture

As for a sample usecase:

There are two nodes: one has the path /mynode and the other has the path /fr/mynode. Going to /fr/mynode would cause the menus to not expand as they would at /mynode, which gets very awkward when using i18n and category/taxonomy at the same time.

This patch "solves" this by fooling most of the menu system into thinking that you're actually looking at the English version of the page (which is what broke the tasks tabs.).

orospakr’s picture

Title: A fix for both the active item issue and missing primary tabs issues. » menu not expanding with URL-Alias
StatusFileSize
new8.34 KB

I noticed that anonymous users could see the menu items if the patch was applied. I've made yet another nasty hack to 'fix' this. bleh.

Note this diff should be applied to the stock menu.inc.

PS. I mistakenly changed the title of the thread in one of my earlier posts. I've put it back.

orospakr’s picture

hmm, when you're editing a node all the menu items become active.

Oh well, it doesn't seem to be a really serious issue.

hass’s picture

if my menu isn't expanding this IS serious.

solide-echt’s picture

I'm also experiencing the 'all itmes active' issue, but I can't pinpoint where it goes wrong in the code. If I find a solution, I'll post it. I'm using a 4.6 version b.t.w (for historic reasons >_<)

solide-echt’s picture

Do you have any idea where I have to search for the cause ? I really don't have any clue

scb’s picture

I think this is not related to i18n. This is happening to me in 5.0 without i18n module installed. May be an error on my side, but I think it is a bug related to aliasing in general.

orospakr’s picture

Yes, you're right.

Any kind of path rewriting (like i18n does) fouls up the menu system.

It uses the path string to store menus, which is the source of the trouble.

vinoth-kumar’s picture

Title: menu not expanding with URL-Alias » Primary menu is not highlighted.....
Version: 4.7.x-1.x-dev » 5.x-1.0

In drupal 5.3 the primary menu item is not highlighted when secondary items were selected, i need both the secondary item and the corresponding primary item are selected....

How to acheive this?

Thanks...

jose reyero’s picture

Status: Active » Closed (won't fix)

This branch is not supported anymore. Consider upgrading to 5.x-2 or (better) 6.x