I have a custom module that implements hook nodeapi to execute some code when the node is created or updated.
Basically I want to create an alias based off of the automatically generated alias on node save or update.
Right now I'm using a call to path_set_alias and I only want to do this with a specific type of content, "product".
Here is my nodeapi call to get me started
function product_url_helper_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if($node->type == 'product'){
switch($op){
case 'insert':
_create_alternate_url($node);
break;
case 'update':
_create_alternate_url($node);
break;
case 'view':
//do nothing
break;
default:
break;
}
}
return;
}
Then I have this function, the one I'm trying to get to save my second URL alias for me.
function _create_alternate_url($node){
$aliasExists = db_fetch_object(db_query("SELECT count(dst) as total FROM {url_alias} WHERE dst = 'alternate/".$node->path."'"));
if($aliasExists->total == 0){
$product_url = $node->path;
$alternate_url = "alt/" . $node->path;
$default_node_path = "node/" . $node->nid;
path_set_alias($default_node_path, $alternate_url, 0, '');
drupal_set_message("Created Alternate path for Product: " . $node->title . " <br/> Path <a href='/" . $default_node_path ."'>" . $default_node_path . "</a> is now aliased by <a href='/" . $alternate_url . "'>". $alternate_url ."</a>");
}
This doesn't set the alias though, it just creates a duplicate of the product's original alias. So If i started off with my product being "Green Fern". I would save it, and it would use pathauto to generate products/green-fern then after call my module code and make an alias "alt/products/green-fern" and still make it point back to the "node/nid" path.
However, when I run this code a duplicate in the database is created. So I save Green Fern one time and all of a sudden I see two duplicate records at the end of the url_alias in the database. "products/green-fern" and "products/green-fern"
I feel like I'm thinking about this in a much too comlpex way. My client is aware of the SEO hit they get when making more than one alias point to the same node, they just want it to do this. Halp!