uuid.tokens.inc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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_tokens().
  30. */
  31. function uuid_tokens($token_type, $tokens, $data = array(), $options = array()) {
  32. $replacements = array();
  33. if ($token_type == 'entity') {
  34. $info = entity_get_info($data['entity_type']);
  35. if (isset($info['uuid']) && $info['uuid'] == TRUE && !empty($info['entity keys']['uuid']) && !empty($tokens[$info['entity keys']['uuid']])) {
  36. $replacements[$tokens[$info['entity keys']['uuid']]] = $data['entity']->{$info['entity keys']['uuid']};
  37. if (!empty($info['entity keys']['revision uuid']) && !empty($tokens[$info['entity keys']['revision uuid']])) {
  38. $replacements[$tokens[$info['entity keys']['revision uuid']]] = $data['entity']->{$info['entity keys']['revision uuid']};
  39. }
  40. }
  41. }
  42. return $replacements;
  43. }