When a user with an alias containing the user name updates the name, pathauto will not update the alias to the new name, but keep the old one.

Suggested fix (sorry having problems wit diffing right now):

In pathauto.module. In pathauto_user hook, change the "case 'update' to case 'after_update' as per below:

function pathauto_user($op, &$edit, &$user, $category = FALSE) {
  _pathauto_include();
  switch ($op) {
    case 'insert':
    case 'after_update':
//    case 'update': patch to ensure path alias changes when user name changes
      // Use the username to automatically create an alias
      $pathauto_user = (object) array_merge((array) $user, $edit);
      if ($user->name) {
        $placeholders = pathauto_get_placeholders('user', $pathauto_user);
        $src = 'user/'. $user->uid;
        $alias = pathauto_create_alias('user', $op, $placeholders, $src, $user->uid);

and In pathauto.inc. In pathauto_create_alias, add 'after_update' as per below:

  // Special handling when updating an item which is already aliased.
  $pid = NULL;
  $old_alias = NULL;
  //patch to ensure 'after_update' is used also. This is the operator used when a user changes his username
  if ($op == 'update' or $op == 'after_update' or $op == 'bulkupdate') {
    if (variable_get('pathauto_update_action', 2) == 0) {
      // Do nothing
      return '';
    }
    $update_data = _pathauto_existing_alias_data($src);
    $pid = $update_data['pid'];
    $old_alias = $update_data['old_alias'];
  }

Comments

Dave Reid’s picture

Status: Active » Postponed (maintainer needs more info)

I can't duplicate the problem with the latest code. Could you please try the latest code and re-confirm?

greggles’s picture

This is likely a bug that is related to another module. However, the use of after_update is likely to fix lots of other problems like the bad interactions Pathauto has (or maybe had) with Email_Registration module.

If this doesn't introduce bugs it seems like it would be a useful change.

magic72’s picture

Hi Dave
I tried yesterday and for some reason I could not reproduce. I am puzzled and starting to think that it is related to other modifications or modules, like what greggles is indicating. I need to test this more over the next couple of days.

Still, the fact is (at least in D6, seems user hook is removed in D7) that op="update" the user object will carry the old username, and when "after_update" the new username is carried.

Will get back to you soon.

magic72’s picture

OK, here is some more info.

The way to reproduce this is when we use [user-name] in the alias path, then the url alias will end up with the old user name. If we instead use [user] or [user-raw], the path will be updated properly and there is no problem.

I traced this back to the token module. token.module generates these tokens/placeholders differently upon call to token_get_values() from pathauto_get_placeholders().

[user-name], the faulty one, is generated in token.module by using the global $user parameter (which during op="update" AND op="after_update" (this one was surprising) is still the old user name):

[user] and [user-raw] on the other hand is generated with input from the $pathauto_user object constucted in pathauto.module. $pathuto_user is merged from two arrays, $user and $edit passed as arguments in to pathauto_user hook, where $user contains the old username, and $edit contains the new fresh and updated username. The array merge will ensure the $edit array values takes precedence and thus the correct updated user->name is passed to the token module.

At the end of the day, this could be seen as a problem in token.module, what do you think?
I can not think of any way to workaround this in pathauto, except for recommending to avoid using [user-name] in the path.

Sorry for raising this bug report with (obviously) lacking investigation, but I am still puzzled how I before issuing it actually could make the problem appear and disappear with the patches in he original post. Therefore it would be good witha second opinion on this issue and the conclusion I am making.

greggles’s picture

Yeah, user-name is not appropriate in this context because it is a global token meant to always give the current user's name.

If anything we should clarify the docs or add to the code in Pathauto so it advises people not to use [user-name] in the user token area.

Dave Reid’s picture

Status: Postponed (maintainer needs more info) » Fixed

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

IWasBornToWin’s picture

Version: 6.x-1.1 » 7.x-1.1
Status: Closed (fixed) » Active

This is currently still happening. I use realname module. I set path auto to token [user:name-raw]. When new user is created everything is fine but when user edits and changes their username it does not change the alias. Any suggestions?

Thank you

amit.verma’s picture

I am also facing same issue.
hook_user_update not have updated user data, it showing old data of user.

markdc’s picture

I had this problem as well. So I created a rule to update alias on account edit.

ishani.addweb’s picture

Please Review below-mentioned steps as the solution which works for me

->Yes it does not show change directly but you have performed several steps.
->After edit username goto Pathauto module, go to delete aliases and choose an alias to delete for example aliases for user check it and click on delete aliases now.
->Now go to bulk generate and check your option to update path and click on the update button.
->At last, goto list and check, the changes.

Let me know if you have any query about it.
Thanks!..

System Lord’s picture

Having the same issue here. I've tried every relative token with no success. The only way to have path updated is to save twice.