6

I have installed pathauto and implemented hook_node_insert in my module, the problem is when I use entity_uri to get new node's URL I will get node/xxx instead of pathauto url alias.

How can I get final URL of the node, after ALL other modules had their manipulations?

function HOOK_node_insert($node) {
    $uri = entity_uri('node', $node);
    $uri['path']; // this should be the final path
}
6

If you also implement hook_module_implements_alter then you can change when your insert hook fires, like this:

function mymodule_module_implements_alter(&$implementations, $hook) {
    if ($hook == 'node_insert') {
    // Move my_module_node_insert() to the end of the list. 
    $group = $implementations['my_module'];
    unset($implementations['my_module']);
    $implementations['my_module'] = $group;
  }
}

So now when your insert code executes the path alias should be available.

| improve this answer | |
  • The advantage of this over changing module weight is that you can have different orderings per hook. So if for another hook you want to execute ahead of other modules, you can. – Alfred Armstrong May 24 '13 at 12:11
  • I tried to use MYMODULE_module_implements_alter but it seems its not being called at all. – Aram Alipoor May 24 '13 at 16:13
  • 1
    You have to clear cache. It only gets called when the registry list of module hooks is built, not normally during a page request. – Alfred Armstrong May 25 '13 at 14:34
2

The reason is that pathauto module is triggering hook after your module hook interaction. To resolve this you have to give a higher weight to your module.

To increase weight, Refer this link: How to update a module's weight

OR

hook_module_implements_alter

This hook is invoked during module_implements(). A module may implement this hook in order to reorder the implementing modules, which are otherwise ordered by the module's system weight.

And also you have to use drupal_get_path_alias function in node insert, Following is a working code when the module weight is set higher value.

function MODULENAME_node_insert($node) {
    $system_path = 'node/'.$node->nid;
    $current_path = drupal_get_path_alias($system_path);
    drupal_set_message($current_path);
}
| improve this answer | |
  • I changed my module weight to 10000 but still getting node/xxx off of drupal_get_path_alias. Is this because the new node is not inserted into table yet or something like that? – Aram Alipoor May 24 '13 at 16:00
0

There is a function in drupal which gives you the path_alias

For e.g.:

$path = node/$nid;    
$url = drupal_get_path_alias($path);    
print $url;

By this you will get desired drupal set path.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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