uc_roles.tokens.inc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @file
  4. * Token hooks for the uc_roles module.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function uc_roles_token_info() {
  10. $type = array(
  11. 'name' => t('Role promotions'),
  12. 'description' => t('Tokens related to purchased, temporary roles.'),
  13. 'needs-data' => 'uc_role',
  14. );
  15. $tokens['expiration'] = array(
  16. 'name' => t('Expiration'),
  17. 'description' => t('The date the role will expire.'),
  18. 'type' => 'date',
  19. );
  20. $tokens['name'] = array(
  21. 'name' => t('Role'),
  22. 'description' => t('The associated role name'),
  23. );
  24. return array(
  25. 'types' => array('uc_role' => $type),
  26. 'tokens' => array('uc_role' => $tokens),
  27. );
  28. }
  29. /**
  30. * Implements hook_tokens().
  31. */
  32. function uc_roles_tokens($type, $tokens, $data = array(), $options = array()) {
  33. $language_code = NULL;
  34. if (isset($options['language'])) {
  35. $language_code = $options['language']->language;
  36. }
  37. $sanitize = !empty($options['sanitize']);
  38. $replacements = array();
  39. if ($type == 'uc_role' && !empty($data['uc_role'])) {
  40. $object = $data['uc_role'];
  41. foreach ($tokens as $name => $original) {
  42. switch ($name) {
  43. case 'expiration':
  44. $replacements[$original] = format_date($object->expiration, 'medium');
  45. break;
  46. case 'name':
  47. $replacements[$original] = $sanitize ? check_plain(_uc_roles_get_name($object->rid)) : _uc_roles_get_name($object->rid);
  48. break;
  49. }
  50. }
  51. if ($expiration_tokens = token_find_with_prefix($tokens, 'expiration')) {
  52. $replacements += token_generate('date', $expiration_tokens, array('date' => $object->expiration), $options);
  53. }
  54. }
  55. return $replacements;
  56. }