I have a content type using the following pathauto-token pattern: [node:menupath]/[node:title], I added manually a menu using a hook_node_insert (see my previous question: creating a menu in a hook_node_insert()).

When the node is saved it doesn't have the [node:menupath] token in the URL, just the [node:title]. I decided to generate the path using pathauto API and this code:

$options = array(
  'language' => isset($node->language) ? $node->language : LANGUAGE_NONE,
);
module_load_include('inc', 'pathauto');
$uri = entity_uri('node', $node);
$path = pathauto_create_alias('node', 'insert', $uri['path'], 
  array('node' => $node), $node->type, $options['language']);

Doing this lead to duplicates in the url_alias table, the [node:menupath]/[node:title] alias being created first then the title.

I tried to add a $node->pathauto_perform_alias = FALSE; in a presave, but seems to be inefective.

Also I tried to set my new path in the hook_node_insert but had no success:

$path = pathauto_create_alias('node', 'insert', //...
$node->path = $path;

My code doesn't feel right, what would be the normal way to avoid duplicate and getting my pattern to work ?

Thanks

link|improve this question

feedback

1 Answer

up vote 0 down vote accepted

I finally moved the insertion of my menu in hook_node_presave() and then pathauto worked normally.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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