Automatically creating alias paths in Drupal 6 module
Here is one way to automatically set alias paths (with argument) in drupal 6 module. Notice that in most cases you probably can get away with pathauto module instead. It is quite powerful and you should check it out before continuing. In fact, this simplified example can be also achieved by that module.
Original path
Lets say I have module called "myaliascreator" and I have some kind of archive functionality inside of it. In hook_menu I define MENU_CALLBACK with drupal path "myaliascreator/%". That "%" char is argument placeholder for year later retrieved in module. Code goes like this.
function myaliascreator_menu() { return array( 'myaliascreator/%' => array( 'title' => 'myaliascreator', 'type' => MENU_CALLBACK, 'page callback' => 'myaliascreator_callback_function', 'access arguments' => array('access myaliascreator content') ) ); }
Creating aliases
Now I want each possible archive path to be aliased programatically when content is changed on site. That means I can use hook_nodeapi to do that. I think on nodes insert and update actions are enough. Also delete action logic should be defined if aliases should be deleted.
In this example I use creation timestamp to make up and set alias path. Function path_set_alias is defined in path module, so it must be enabled to make it work. Don't forget to clear cache before testing.
function myaliascreator_nodeapi(&$node, $op, $teaser, $page) { switch ($op) { case 'insert': case 'update': if ( $node->type == 'page' ) { //create argument $year = date('Y', $node->created); //create original valid path $pathOriginal = "myaliascreator/" . $year; //in here some fancy custom logic should be used to create alias $pathAlias = "nice-and-aliased-path/" . $year; //create actual alias, no need for dublicate check- it is built in path_set_alias( $pathOriginal, $pathAlias, null, $node->language ); } break; default: break; } }
Content duplication
Remember to do something with orginal paths also, because drupal does not magically redirect these to aliased paths by default. Probably it is not a big issue like discussed in article "Drupal, duplicate content, and you" by Jeff Eaton, but if sceptical you can check out global direct module.
Categories:
Drupal