I try to use token starter. I have done my own module by copying the 2 files .info and .module.
I succesfully my new modules.

But I can't see my new tokens in pathauto. any help are welcome:
I'm a newbie in php...

/**
 * Implementation of hook_token_list().
 */
function tokenSTARTER_token_list($type = 'all') {
  if ($type == 'user' || $type == 'all') {
    $tokens['global']['highest-role'] = t('Highest role from current user.');
  }
  return $tokens;
}

/**
 * Implementation of hook_token_values().
 */
function tokenSTARTER_token_values($type, $object = NULL) {
  $values = array();
  switch ($type) {
	case 'user' :
		global $user;
		$values['highest-role'] = role_weights_get_highest($user->roles);
		break;
  }
  return $values;
}

Comments

heyyo’s picture

I finally found a solution to create token from user roles. Advices are really welcome, if my code needs modification for performance or security.
My users have only one role : painter or sculptor or photographer or designer (except authenticated users or anonymous)

function mytokens_token_list($type = 'all') {
  if ($type == 'user' || $type == 'all') {
    $tokens['user']['role'] = t('Artist role.');
  }
  return $tokens;
}

function mytokens_token_values($type, $object = NULL, $options = array()) {
  $values = array();
  if ($type == 'user') {
	$user = $object;
	$account = user_load(array('uid' => $user->uid));
  
	if (in_array('Painter', array_values($account->roles))) {
		$values['role'] = 'painting';
		return $values;
	}
	if (in_array('Sculptor', array_values($account->roles))) {
		$values['role'] = 'sculpture';
		return $values;
	}
	if (in_array('Photographer', array_values($account->roles))) {
		$values['role'] = 'photo';
		return $values;
	}
	if (in_array('Designer', array_values($account->roles))) {
		$values['role'] = 'design';
		return $values;
	}

	$values['role'] = 'user';
	return $values;
  }  
}
apaderno’s picture

  if ($type == 'user') {
    $user = $object;
    $account = user_load(array('uid' => $user->uid));

Why is the code loading the user object, when it is already passed to the function?

MGN’s picture

The reason for loading the user object is that $object defaults to NULL. You might want to do the following instead...

if (isset($object)) {
   $account = $object;
}
 else {
   global $user;
   $account = user_load(array('uid' => $user->uid));
}

You could also economize on the in_array() calls. Perhaps something like....

  $role = array_pop($account->roles);
  switch ($role) {
    case 'Painter':
      $values['role'] = t('painting');
      break;
    case 'Sculptor':
      $values['role'] = t('sculpture');
      break;
    case 'Photographer':
      $values['role'] = t('photo');
      break;
    case 'Designer':
      $values['role'] = t('design');
      break;
    default:
      $values['role'] = t('user');
  }
  return $values;
}  
Dave Reid’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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