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]’