uuid.tokens.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. /**
  3. * @file
  4. * Builds placeholder replacement tokens for uuid-related data.
  5. */
  6. /**
  7. * Implements hook_token_info().
  8. */
  9. function uuid_token_info() {
  10. $tokens = array();
  11. foreach (entity_get_info() as $entity_type => $info) {
  12. if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid'])) {
  13. $token_type = !empty($info['token type']) ? $info['token type'] : $entity_type;
  14. $tokens['tokens'][$token_type][$info['entity keys']['uuid']] = array(
  15. 'name' => t('@entity_type UUID', array('@entity_type' => $info['label'])),
  16. 'description' => t('The universally unique ID of the @entity', array('@entity' => drupal_strtolower($info['label']))),
  17. );
  18. if (!empty($info['entity keys']['revision uuid'])) {
  19. $tokens['tokens'][$token_type][$info['entity keys']['revision uuid']] = array(
  20. 'name' => t('@entity_type revision UUID', array('@entity_type' => $info['label'])),
  21. 'description' => t('The universally unique revision ID of the @entity', array('@entity' => drupal_strtolower($info['label']))),
  22. );
  23. }
  24. }
  25. }
  26. return $tokens;
  27. }
  28. /**
  29. * Implements hook_token_info_alter().
  30. */
  31. function uuid_token_info_alter(&$data) {
  32. foreach (entity_get_info() as $entity_type => $info) {
  33. if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid'])) {
  34. $token_type = !empty($info['token type']) ? $info['token type'] : $entity_type;
  35. if (empty($data['types'][$token_type])) {
  36. unset($data['tokens'][$token_type]);
  37. }
  38. }
  39. }
  40. }
  41. /**
  42. * Implements hook_tokens().
  43. */
  44. function uuid_tokens($token_type, $tokens, $data = array(), $options = array()) {
  45. $replacements = array();
  46. if ($token_type == 'entity') {
  47. $info = entity_get_info($data['entity_type']);
  48. if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid']) && !empty($tokens[$info['entity keys']['uuid']])) {
  49. $replacements[$tokens[$info['entity keys']['uuid']]] = $data['entity']->{$info['entity keys']['uuid']};
  50. if (!empty($info['entity keys']['revision uuid']) && !empty($tokens[$info['entity keys']['revision uuid']])) {
  51. $replacements[$tokens[$info['entity keys']['revision uuid']]] = $data['entity']->{$info['entity keys']['revision uuid']};
  52. }
  53. }
  54. }
  55. return $replacements;
  56. }