I am working on a custom module to specify the pathauto alias of a new node, based on taxonomy terms that a user sets when they create the node. For instance, for my blog post content type, the alias would be auto-generated as /blogs/this-is-a-new-blog-post. However, if the category taxonomy is set to be special-project, I want to change the alias to be /special-project/blog/this-is-a-new-blog-post.

I'm able to load the node using hook_entity_insert(). However, I haven't been able to figure out how to set the alias.

I've found examples of how to set the path when creating a new node, but I already have an existing node object in this hook. I've tried debugging the path from the entity using $entity->toUrl()->getInternalPath(), but that just gives me the default URL (/node/123), not the alias. Trying to debug $node->path, I get an enormous stdClass object that I can't really suss out. Trying to debug $node->path->alias using drupal_set_message("alias: " . $node->path->alias);, I get this: alias:.

How can I see and alter the path alias on an existing node?

2 Answers 2


Pathauto 8.x is already hooking into the hook_entity_$CRUD_OP() hooks of D8 to do its work. Thus, trying to do this via a hook_entity_insert() means your module hook might fire before/after pathauto has set its alias (depending on module weight).

Instead of doing this, you should use the API functions of pathauto to alter its behavior. For example, hook_pathauto_pattern_alter() or hook_pathauto_alias_alter() will let you alter the pattern/alias prior to generation/saving.

See pathauto.api.php for more info.


As Shawn Conn pointed out, there are pathauto hooks that I can use instead. What I wound up using was hook_pathauto_alias_alter(&$alias, array &$context). The $context has the node entity object in $context['data']['node'], and $alias is the literal alias string. Since it's passed by reference, no need to perform any save function after changing it. Very easy to do what I need to in there.

Your Answer

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

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