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!
Tough to tell how complex you'd like the aliases to be, but the
pathauto
module seems like it would be a simple solution to your problem. It allows you to generate aliases automatically based on tokens.As a bonus, it places nicely with the
path_redirect
module, which will set 303 redirects (or whatever redirect you'd like) on duplicate aliases. This will mitigate your SEO concerns somewhat.Edit:
Are you super-duper sure that your unredacted code is calling the functions correctly? The relevant sections of
path_set_alias
are:If you're working on a test site, you could also try dropping some
dpm()
s right before thedb_query
s innode.module
to find out what sections of the code are actually firing, and what data is going into them.JR, improving your code in Drupal7, the db_querys are expensive. A better way could be using the lookup_path drupal function:
if urlAlias has no value, then there is no alias for this url, so we can safely create it:
Pathauto is detecting duplicate aliases for the node and overwriting the custom one with the default content type pattern. By default, pathauto will create a new alias and delete an old one when an update action is taken.
If you switch the Update action option to "Create a new alias. Leave the existing alias functioning" on the admin screen,
/admin/config/search/path/settings
, it will not overwrite the custom alias.You are explicitly setting the pid to 0 in the code. I would imagine that the second node this code runs for would return an error. Try changing 0 to NULL in your
path_set_alias()
call.