Imagine you work with two menus: primary-links and seo-links.
Node 1 has a menu-entry in both primary-links and seo-links.
If pathauto generates an url-alias for node 1, which menu-entry is source?

Behavior now: pathauto takes the first entry found in table menu_links matching link_path (is that right?)

I suggest to provide an interface where we can define which menu pathauto should take as a source for [menupath].

Comments

greggles’s picture

I think you are right about how it works now. I think this is an overly complex feature that we should not expose in the UI. Instead you could write a site-specific token implementation that provides the menu token you want. I've been meaning to write a tutorial on how to do that for a while, so...I'll leave this open and provide a link when I've done it.

guldi’s picture

I would like to help.
I'll check if I can do it.

Any tips appreciated. :)

greggles’s picture

guldi’s picture

Hum. Did I understand right, you just want a tutorial on how to provide user-defined tokens?

Another suggestion would be to just implement the [menuxy-menupath-raw] tokens in the token module. (For each menu one new token)

greggles’s picture

Of course it is possible to provide these tokens in the token module, but we are generally not adding new tokens to it because each new token is a performance issue and UI issue.

guldi’s picture

Ok, so I did it via a helper module to just satisfy my needs.


/**
 * Implementation of hook_token_values().
 */
function rubitas_helpers_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  switch ($type) {
    case 'node':
      $node = $object;    
      foreach(menu_get_menus() as $menukey => $menu) {        
        $mlid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'", $menukey, 'node/'. $node->nid));
        if (!empty($mlid)) {
          $menu_link = menu_link_load($mlid);
          $trail_raw = _menu_titles($menu_link, $node->nid);

          $trail = array();
          foreach ($trail_raw as $title) {
            $trail[] = check_plain($title);
          }

          $menu = $menus[$menu_link['menu_name']];
          
          $values[$menukey.'-menupath']            = implode('/', $trail);
          $values[$menukey.'-menupath-raw']        = implode('/', $trail_raw);
          $values[$menukey.'-menu']                = check_plain($menu);
          $values[$menukey.'-menu-raw']            = $menu;
          $values[$menukey.'-menu-link-title']     = check_plain($menu_link['title']);
          $values[$menukey.'-menu-link-title-raw'] = $menu_link['link_title'];
        } else {
          $values[$menukey.'-menu']                = '';
          $values[$menukey.'-menu-raw']            = '';
          $values[$menukey.'-menupath']            = '';
          $values[$menukey.'-menupath-raw']        = '';
          $values[$menukey.'-menu-link-title']     = '';
          $values[$menukey.'-menu-link-title-raw'] = '';
        }
      }
    break;
  }
  return $values;
}

/**
 * Implementation of hook_token_list().
 */
function rubitas_helpers_token_list($type = 'all') {
  if ($type == 'node' || $type == 'all') {
    foreach(menu_get_menus() as $menukey => $menu) {     
      $tokens['node'][$menukey.'-menu']                = t("!name: The name of the menu the node belongs to.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menu-raw']            = t("!name: The name of the menu the node belongs to. WARNING - raw user input.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menupath']            = t("!name: The menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menupath-raw']        = t("!name: The unfiltered menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /. WARNING - raw user input.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menu-link-title']     = t("!name: The text used in the menu as link text for this item.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menu-link-title-raw'] = t("!name: The unfiltered text used in the menu as link text for this item. WARNING - raw user input.",array("!name" => $menu));
    }
    return $tokens;
  }
}

So after adding this functions, you are able to district your menu-tokens to a specific menu.
Eg: [primary-links-menupath-raw] (what I needed)

guldi’s picture

Status: Active » Closed (fixed)

@greggles: for me it's closed.
Feel free to reopen it in order to keep it as a reminder for you to write your tut.
Thx!

Proct0t’s picture

the code above works fine for me at the moment, thanks!

imiksu’s picture

Status: Closed (fixed) » Needs review

#6 code example wont fully work in my case. It created tokens as expected, but I'm now having similar problem as #881270: [bookpath], [menupath], [*path] tokens not cleaned: aliases without punctuation removed, lower casing, etc.. Tokens gets not cleaned.

I'm using "Pathauto 6.x-1.5" and "Token 6.x-1.15".

imiksu’s picture

Status: Needs review » Closed (fixed)

This code example works with latest "Pathauto 6.x-1.5" and "Token 6.x-1.15" modules. I used this patch as reference: http://drupal.org/files/issues/881270-token-path-tokens_109.patch

<?php
/**
* Implementation of hook_token_values().
*/
function rubitas_helpers_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  switch ($type) {
    case 'node':
      $node = $object;    
      foreach(menu_get_menus() as $menukey => $menu) {        
        $mlid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE menu_name = '%s' AND link_path = '%s'", $menukey, 'node/'. $node->nid));
        if (!empty($mlid)) {
          $menu_link = menu_link_load($mlid);
          $trail_raw = _menu_titles($menu_link, $node->nid);

          $trail = array();
          foreach ($trail_raw as $title) {
            $trail[] = check_plain($title);
          }

          $menu = $menus[$menu_link['menu_name']];
          
          $values[$menukey.'-menupath']            = !empty($options['pathauto']) ? $trail : implode('/', $trail);
          $values[$menukey.'-menupath-raw']        = !empty($options['pathauto']) ? $trail_raw : implode('/', $trail_raw);
          $values[$menukey.'-menu']                = check_plain($menu);
          $values[$menukey.'-menu-raw']            = $menu;
          $values[$menukey.'-menu-link-title']     = check_plain($menu_link['title']);
          $values[$menukey.'-menu-link-title-raw'] = $menu_link['link_title'];
        } else {
          $values[$menukey.'-menu']                = '';
          $values[$menukey.'-menu-raw']            = '';
          $values[$menukey.'-menupath']            = '';
          $values[$menukey.'-menupath-raw']        = '';
          $values[$menukey.'-menu-link-title']     = '';
          $values[$menukey.'-menu-link-title-raw'] = '';
        }
      }
    break;
  }
  return $values;
}

/**
* Implementation of hook_token_list().
*/
function rubitas_helpers_token_list($type = 'all') {
  if ($type == 'node' || $type == 'all') {
    foreach(menu_get_menus() as $menukey => $menu) {     
      $tokens['node'][$menukey.'-menu']                = t("!name: The name of the menu the node belongs to.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menu-raw']            = t("!name: The name of the menu the node belongs to. WARNING - raw user input.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menupath']            = t("!name: The menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menupath-raw']        = t("!name: The unfiltered menu path (as reflected in the breadcrumb), not including Home or [menu]. Separated by /. WARNING - raw user input.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menu-link-title']     = t("!name: The text used in the menu as link text for this item.",array("!name" => $menu));
      $tokens['node'][$menukey.'-menu-link-title-raw'] = t("!name: The unfiltered text used in the menu as link text for this item. WARNING - raw user input.",array("!name" => $menu));
    }
    return $tokens;
  }
}
?>
brankoc’s picture

Thanks iMiksu, just what I needed.

In case anyone's wondering, Pathauto 1.3's

          $values[$menukey.'-menupath']            = implode('/', $trail);
          $values[$menukey.'-menupath-raw']        = implode('/', $trail_raw);

was changed into Pathauto 1.5's

          $values[$menukey.'-menupath']            = !empty($options['pathauto']) ? $trail : implode('/', $trail);
          $values[$menukey.'-menupath-raw']        = !empty($options['pathauto']) ? $trail_raw : implode('/', $trail_raw);