Update url alias of user nodes on change of drupal username

One of our client want functionality to change username in edit profile which is easy. We are using pathauto module for pretty URL in Drupal7.
I faced two issues when changing username. which are explained here..

First Issue

We are using username paths (/users/[username]). After changing a username the url alias doesn’t change and the user edit page doesn’t have an option to change it manually.

Second Issue

We are using username before nodes urls. for after changing username Node uel alias does not change automatically.

Solution (for first issue):
For fist issue I used rules modeuls and created following rule:

You can create rule using following export rule.

{ "rules_change_user_url_alias" : {
    "LABEL" : "Change user URL alias",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "path" ],
    "ON" : [ "user_update" ],
    "IF" : [
      { "NOT data_is" : { "data" : [ "account-unchanged:name" ], "value" : [ "account:name" ] } }
    "DO" : [
      { "path_alias" : { "source" : "user\/[account:uid]", "alias" : "member\/[account:name]" } }

Solution (for Second issue):
For second issue, I written custom code as follows:
I used user_update hook for solving this issue.
You just need to put following code in your custom module. It will find all nodes of editing user (logged in user) and update url alias.

 * On user update call this method for updating the node url in url_alias
function MYMODULE_user_update(&$edit, $account, $category) {
    //getting all nodes
    $nodes = get_user_node_by_type('custom_node_type',$account->uid);
    //loop for all nodes of logged in User
    if(!empty($nodes)) {
        foreach ($nodes as $key => $node) {
            //load path auto module
            if(module_load_include('inc','pathauto','pathauto') !== FALSE) {
                if (function_exists('pathauto_cleanstring')) {
                //generates clear url title from node title
                $currnt_page_title_alias = pathauto_cleanstring($node->title); // define in pathauto.inc file.
                //generate new url for selected node
                // echo 'node/'.$node->nid;die;
                $query = db_query("select u.pid from {url_alias} AS u WHERE u.source = 'node/$node->nid'");         
                $result = $query->fetchAll();
                    //If url alias found for node than update the alias
                    if(!empty($result)) {
                        db_query("UPDATE {url_alias} AS u SET u.alias = '$new_url' WHERE u.source = 'node/$node->nid'");

 * Return User nodes based on UID and node type
function get_user_node_by_type($node_type, $uid){
  $nodes = array();
  $nids = db_query('SELECT nid FROM {node} WHERE uid = :uid AND type = :type', array(':uid' => $uid, ':type' => $node_type))->fetchCol();
  if (!empty($nids)) {
    $nodes = node_load_multiple($nids);    
  return $nodes;

Above code will solve my problem. When I edit my username, It collects all nodes which are created by that user and update the url_alias of nodes.

Published by


I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

Leave a Reply

Your email address will not be published.