Add menupathalias token

xjm - July 31, 2008 - 17:50
Project:Token
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work
Description

The existing menupath token doesn't actually use the path aliases of menu trail nodes. Instead, it generates a fake "path" out of the titles of items in the menu trail.

Example: Suppose you have three nodes in the following menu structure [nids in brackets]:

Administration [1]
-- Human Resources [2]
----Employment [3]

These nodes have the following node titles:

  1. Department Administration: Introduction and Overview
  2. Human Resources
  3. Become one of the team!

The URL alias for [1] is dept; the alias for [2] is dept/hr. Suppose we use the existing [menupath] token with the pathauto module to automatically generate a URL alias for [3]. The generated URL alias would be something like:
department-administration-introduction-overview/human-resources/become-one-team

This doesn't at all create the sense of [3] being an item "inside" the admin/hr "directory". A much better alias would be:
dept/hr/employment
which would reflect both the information in the menu and the implied "directory" hierarchy.

I added two tokens to my copy of the token module to address this: [parent_path_alias], which is the URL alias, if any, of the node's immediate parent in the menu system; and menu_title, which is based on the title of the node's entry in the menu system (e.g., "Employment") rather than the title of the node itself ("Become one of the team!").

AttachmentSize
menu_path_tokens_d5.patch1.73 KB

#1

xjm - July 31, 2008 - 17:55

* That should have been "dept/hr 'directory' " above, but I think it's still fairly clear.

For clarification, I have pathauto set to generate node aliases with this token pattern to achieve the desired aliasing:
[parent_path_alias]/[menu_title].

The application in conjunction with pathauto was the most important one to me, but I can see these tokens being used in a variety of situations.

#2

greggles - August 4, 2008 - 14:50
Title:Add "real" menu path handling» Add menupathalias token
Version:5.x-1.9» 6.x-1.x-dev
Status:needs review» needs work

Thanks for the idea and patch, xjm. I think this is a reasonable token to provide but there are a few corrections before it can be committed.

First, the "ADDED BY" and "END ADDITION" pieces should be removed.

Also, this is a fairly common thing to provide, but is generally called "*alias" (see term and termalias or bookpath and bookpathalias). I think this should be called "menupathalias".

Finally, if possible I'd prefer to add this feature to 6.x first or at least have patches for both 5.x and 6.x

Thanks again.

#3

_double - November 8, 2009 - 02:04

So, is this going to be added to 6.x?

#4

xjm - November 8, 2009 - 04:48

When I submitted the issue last year none of my sites were in D6 so I didn't really have an incentive to do anything beyond just submit the idea for anyone else interested. :) I'm in the process of migrating to 6.x now and will probably be patching my 6.x token.module when I get there in a couple weeks; if so I'll upload a (clean) patch to this issue when I do. Of course, feel free to make your own patch if you like!

#5

_double - November 9, 2009 - 02:51

Here is a quick attempt at providing a ['parent_path_alias'] token. This requires the Menu Node API module, which is very useful. Since it depends on a module that token does not, I though it best not to post it in patch form.

<?php
/**
* Implementation of hook_token_values().
*
*/
function token_parent_path_alias_token_values($type, $object = NULL) {
 
$values = array();
  if (
$type == 'node') {
   
$node = $object;

    if (!empty(
$node->menu['plid'])) { // Make sure menu item has a parent
       
       
$parent_mlid = $node->menu['plid']; // Get the mlid of the parent
           
       
$parent_object = menu_node_get_node($parent_mlid);  // Return a node object for the closest parent
   
       
$parent_path = $parent_object->path; // Pull the path out of the parent node object
   
       
$values['parent_path_alias'] = $parent_path; // Set token
   
       
if ($parent_path == 'node/'.$parent_object->nid) { // If there is not an alias, return nothing
           
$values['parent_path_alias'] = '';
        }
    }

    else {
       
$values['parent_path_alias'] = '';
       }

      return
$values;
    }
}

/**
* Implementation of hook_token_list().
*/
function token_parent_path_alias_token_list($type = 'all') {
  if (
$type == 'node' || $type == 'all') {
   
$tokens['node']['parent_path_alias'] = t("The path alias of the closest parent menu item. CAUTION - Be careful with bulk-updating content using this token.");
    return
$tokens;
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.