« Beyond Pathauto »
The Pathauto module is the Drupal supplied solution to automatically create URL aliases based on the title of a node. You can adjust this in the Pathauto configuration following some basic patterns, but sometimes this is not enough.
Drupal has another mechanism with which you can create custom URL’s for each node. One just has to edit the node, find the “URL path settings” fieldset, and add an alias to that node. If you have a website that publishes a lot of content, doing this manually will become tedious, error prone, and even impractical.
We had a requirement on a recent project where there were Drupal nodes that had to be accessible from a URL that was not possible to automatically generate with the Pathauto module. Our custom content type stored data for a media review that was one of several types: a video game review, movie review, book review and so on. For SEO purposes, we needed to generate a URL based on the review type and its title, for example a game would have the URL games/game-title, a movie would have movies/movie-title. The problem with the Pathauto module was that it could only categorize a content type with one type of URL pattern so all reviews could be stored in games/game-title or movies/movie-title, but not both. Using a custom Drupal module allows you the flexibility to create different URL alias patterns based on your own rules and data. Here is how:
Step 1: Configure Pathauto
- Go to the Pathauto configuration page at "/admin/settings/pathauto".
- Click on the "Node Path Settings" field set.
- Find the Label "Pattern for all Page", then set the "pattern" for that content type.
- For now we just need to add the "[title]" pattern in the text box.
Step 2: Create a Module:
We need to create a module that is the same name as the CCK content type you have existing in your system. In our case we would create a module named Review.module. (Please see Drupal documentation for how to add modules).
Step 3: Override the Hook Nodeapi:
What we will do in the module is hijack this pattern and add what we want to it.
In the review.info module you will need to add a dependency with the AutoPath module to ensure that it is turned on for this to work (Please see Drupal documentation for how to add module dependencies).
Next edit the review.module file and add the following code.
// This allows us to use the functions defined in the
// autopath module.
require_once '../modules/contrib/pathauto/pathauto.module';
//Now all we need to do is to overload the node_api hook
function review_nodeapi (&$node, $op, $a3 = NULL, $a4 = NULL){
$is_inserting = FALSE;
if ($node->type == 'review') {
if ($op == 'insert'){
$is_inserting = TRUE;
}
// Preparing the array for the URL alias insertion
$placeholders = pathauto_get_placeholders('node', $node);
// Detect what type it is, and modify the title
// the Pathauto module must be enabled for this to work.
// $node->node_type stores the type of review
// (‘game’,’music’, ‘movie’)
$node_type = $node->node_type;
// pathauto_cleanstring removes all spaces and special
// characters from the title in preparation of url format.
$cleantitle = pathauto_cleanstring($node->title, FALSE);
// The url that will be created:
$cleantitle = ‘reviews/’ . $node_type . '/'. $cleantitle;
// Place it in the array in the 15, 16 position
// (this indicates it is a alias for a node)
$placeholders['values'][15] = $cleantitle;
$placeholders['values'][16] = $cleantitle;
if ($is_inserting) {
pathauto_create_alias('node', 'insert', $placeholders,
"node/" .$node->nid, $node->nid, $node->type);
} else {
pathauto_create_alias('node', 'update', $placeholders,
"node/" .$node->nid, $node->nid, $node->type);
}
} // end if node->type = ‘review’
} // end node_api hook
Now any time you add a review it will have a url alias that consists of www.yoursite.com/reviews/[review-type]/[title-of-review]’
5 Comments | 
Reader Comments (5)
It's a whole lot easier to use the Token module and implement a token for the node 'sub-type'. This is pretty standard use of the Token module who happens to also be the primary developer of the Pathauto module. See hook_token_list and hook_token_values.
You could say define a token 'content-sub-type' in your module and use that pattern in the Pathauto configuration screen. In your module you would then provide the value for the token in your implementation of hook_token_values which having access to your node object would have the value of your field that defines the sub-type you want.
Your implementation does things the hard way.
Nice post.Thank you for taking the time to publish this information very useful
Christian Louboutin D'orsay
Christian Louboutin D'orsay
[url=http://www.christianlouboutinshoesale.org/ ]Christian Louboutin D'orsay[/url]
[url="http://www.christianlouboutinshoesale.org/ " title="Christian Louboutin D'orsay"]Christian Louboutin D'orsay[/url]
Perhaps this is one of the most interesting blogs that I have ever seen. Interesting article, Funny comment. Keep it up!
Christian Louboutin Hot Red
Christian Louboutin Hot Red
[url=http://www.christianlouboutinshoesale.org/ ]Christian Louboutin Hot Red[/url]
[url="http://www.christianlouboutinshoesale.org/ " title="Christian Louboutin Hot Red"]Christian Louboutin Hot Red[/url]
http://shuijin0024.blog.163.com
http://hi.baidu.com/shuijin0024/blog
http://blog.sina.com.cn/u/2192630013
http://blog.sohu.com/people/!c2h1aWppbjAwMjRAaG90bWFpbC5jb20=
http://shuijin12.blog.com
http://www.shuijin0024.19dog.com
http://shuijin12.over-blog.com
http://17278441.blog.hexun.com
http://www.thoughts.com/shuijin0024
http://www.equestrianblogging.com/blogs/shuijin0024
http://shuijin0024.insanejournal.com
That's what I am looking for.This is awesome and I also want to share something cool with you.That is cheap dvds and I hope you would like it.
Tower Prep on dvd