hey guys, i think this is a core bug; bug i only see it showing up with pathauto so far so i'll report it here and perhaps you guys will have a better idea if this is a core bug or not (and maybe a little more clout to get it fixed).

i am using the token [user:roles:first] (although i am sure all role based tokens are affected) for defining urls for users as [user:roles:first]/[user:field_firstname]-[user:field_lastname]

an example resulting alias for a user in role "faculty" is: 4/melanie-smith

4 = faculty

the issue is that $form-state['user']['roles'] starts out like:

2 => authenticated-user
4 => faculty

but it hits this function: entity_form_submit_build_entity() and gets modified to:

array = 
  4: string = 4
  2: bool = TRUE
  3: long = 0
  5: long = 0
  6: long = 0
  7: long = 0

i am not completely sure; but this gets busted at the call in common.inc at line 7679:

$values_excluding_fields = $info['fieldable'] ? array_diff_key($form_state['values'], field_info_instances($entity_type, $bundle)) : $form_state['values'];

which i think is meant to remove form state values which should not get modified. and $user or $user->roles should likely be in there.

just as a quick test, i removed $user->roles from this array and i then get my url as authenticated-user/melanie-smith (which isn't quite right either; but i closer)

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

liquidcms’s picture

Project: Pathauto » Token
Version: 7.x-1.x-dev » 7.x-1.0-beta6
Component: Tokens » Code

moving to Tokens

liquidcms’s picture

until this code gets fixed either in token or in core; here is code for a new custom token for the user's role.

as can be seen in the code, further evidence that this is likely a core bug, the user object has different values for $user->role depending on if editing a user directly or if doing a bulk change of user's url alias.

function mit_token_info() {
  $info['tokens']['user']['mit-role'] = array(
    'name' => t('User role'),
    'description' => t('The user\'s role not including authenticated, etc.'),
  );

  return $info;
  
}

function mit_tokens($type, $tokens, array $data = array(), array $options = array()) {
  $replacements = array();
  
    // User tokens.
  if ($type == 'user' && !empty($data['user'])) {
    $account = $data['user'];
    unset($account->roles[2]);  // remove authenticated 
    
    if (!count($account->roles)) $rname = t("user");
    // due to bug in core $account->roles is different at different times - so need to handle this
    
    elseif (is_numeric(current($account->roles))) { 
      $role = user_role_load(current($account->roles));  
      $rname = $role->name;
    }
    
    else $rname = current($account->roles);
    
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'mit-role':
          $replacements[$original] = $rname;
          break;
      }
    }
  }
 
  return $replacements;
}
Dave Reid’s picture

Could you please elaborate on how or what you are doing causes the $account->roles data to become non-standard?

Dave Reid’s picture

Version: 7.x-1.0-beta6 » 7.x-1.x-dev
Assigned: Unassigned » Dave Reid

Ah, I see now - it happens when editing an existing user at user/uid/edit.

Dave Reid’s picture

Status: Active » Needs review
FileSize
1.34 KB

Please try the following patch to token module.

Dave Reid’s picture

Status: Needs review » Fixed

Committed #5 with a test to 7.x-1.x: http://drupalcode.org/project/token.git/commit/58b295c

liquidcms’s picture

yup, the role token seems to work now. still will need my custom token however as don't want authenticated to always be the role i get for a user.

also, since i still need my code to check if what format the $user->roles value is in; i would still think there is a core bug here somewhere.

like your array_intersect_key(user_roles(), $account->roles); method though.. :) better than what i did

Status: Fixed » Closed (fixed)

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