Help on token Starter

heyyo - October 25, 2009 - 09:34
Project:Token
Version:6.x-1.12
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:active
Description

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;
}

#1

heyyo - October 25, 2009 - 11:41

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;
  } 
}

#2

kiamlaluno - October 25, 2009 - 19:10

<?php
 
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?

#3

MGN - October 25, 2009 - 20:17

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

<?php

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....

<?php
  $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;

?>

 
 

Drupal is a registered trademark of Dries Buytaert.