3

I'm creating a webform when organic groups are created and I'm trying to set an alias for it with hook_nodeapi(). It successfully creates the alias when the insert operation for the group is called after the webform has been created and associated to the group, but if you edit and update the group it does not update the alias with the code I have written. and a SQL error is displayed.

Here is the code that is called by hook_nodeapi() on insert and update operations:

// Webform
$nid = $node->field_webform_id[0]['value'];
$webform = db_fetch_object(db_query("SELECT count(src) as total FROM {url_alias} WHERE src = 'node/%d'", $nid));
if ($webform->total > 0) {
  db_query("DELETE FROM {url_alias} WHERE src LIKE 'node/%d'", $nid);
}
path_set_alias('node/'.$nid, 'local/'.$state.'/'.$county.'/submit-tip');

I'm getting the webform node ID from a CCK field on the group, checking if there are aliases set already for that node, and deleting them from the url_alias table. Then I'm using path_set_alias to recreate an alias (in this case it is the same as before).

The error message displayed when updating a group:

user warning: Duplicate entry 'local/pa/adams/submit-tip-' for key 'source_language' query: INSERT INTO path_redirect (source, redirect, query, fragment, language, type, last_used) VALUES ('local/pa/adams/submit-tip', 'node/209', '', '', '', 301, 1311098626) in /var/aegir/platforms/crimewatchpa-dev-v2011-07-12-15-42/includes/common.inc on line 3538.

Instead of creating local/pa/adams/submit-tip as an alias to node/209, it creates an alias submit-tip-0 (submit-tip already exists on another node). A 301 redirect is also created with the path: local/pa/adams/submit-tip to node/209.

1 Answer 1

0

The trick was to set $node->pathauto_perform_alias = FALSE; before calling node_save. Then my manual aliasing was allowed to take effect.

I still haven't figured out how to prevent the 301 redirect. It only produces a warning now, and the alias stays the way I want.

1
  • 301 redirect stopped after I found one additional line of code where I had saved the node but forgot to set pathauto_perform_alias to false.
    – Arosboro
    Sep 5, 2011 at 20:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.