googleanalytics.tokens.inc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for user-related data.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function googleanalytics_token_info() {
  10. $user['role-names'] = array(
  11. 'name' => t('User role names'),
  12. 'description' => t('The role names the user account is a member of as comma separated list.'),
  13. 'needs-data' => 'user',
  14. );
  15. $user['role-ids'] = array(
  16. 'name' => t('User role ids'),
  17. 'description' => t('The role ids the user account is a member of as comma separated list.'),
  18. 'needs-data' => 'user',
  19. );
  20. return array(
  21. 'tokens' => array('user' => $user),
  22. );
  23. }
  24. /**
  25. * Implements hook_tokens().
  26. */
  27. function googleanalytics_tokens($type, $tokens, array $data = array(), array $options = array()) {
  28. $sanitize = !empty($options['sanitize']);
  29. $replacements = array();
  30. if ($type == 'user' && !empty($data['user']->roles)) {
  31. $account = $data['user'];
  32. foreach ($tokens as $name => $original) {
  33. switch ($name) {
  34. // Basic user account information.
  35. case 'role-names':
  36. $names = implode(',', $account->roles);
  37. $replacements[$original] = $sanitize ? check_plain($names) : $names;
  38. break;
  39. case 'role-ids':
  40. $ids = implode(',', array_keys($account->roles));
  41. $replacements[$original] = $sanitize ? check_plain($ids) : $ids;
  42. break;
  43. }
  44. }
  45. }
  46. return $replacements;
  47. }