entity_uuid.inc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an argument handler for all entity IDs.
  5. */
  6. /**
  7. * Plugins are described by creating a $plugin array which will be used
  8. * by the system that includes this file.
  9. */
  10. $plugin = array(
  11. 'title' => t("Entity: UUID"),
  12. 'description' => t('Creates an entity context from an entity UUID argument.'),
  13. 'context' => 'uuid_entity_uuid_context',
  14. 'get child' => 'uuid_entity_uuid_get_child',
  15. 'get children' => 'uuid_entity_uuid_get_children',
  16. );
  17. /**
  18. * @todo document me properly
  19. */
  20. function uuid_entity_uuid_get_child($plugin, $parent, $child) {
  21. $plugins = uuid_entity_uuid_get_children($plugin, $parent);
  22. return $plugins[$parent . ':' . $child];
  23. }
  24. /**
  25. * @todo document me properly
  26. */
  27. function uuid_entity_uuid_get_children($original_plugin, $parent) {
  28. $entities = entity_get_info();
  29. $plugins = array();
  30. foreach ($entities as $entity_type => $entity) {
  31. $plugin = $original_plugin;
  32. $plugin['title'] = t('@entity: UUID', array('@entity' => $entity['label']));
  33. $plugin['keyword'] = $entity_type;
  34. $plugin['description'] = t('Creates @entity context from an UUID argument.', array('@entity' => $entity_type));
  35. $plugin['name'] = $parent . ':' . $entity_type;
  36. $plugin_id = $parent . ':' . $entity_type;
  37. drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id);
  38. $plugins[$plugin_id] = $plugin;
  39. }
  40. drupal_alter('ctools_entity_contexts', $plugins);
  41. return $plugins;
  42. }
  43. /**
  44. * Discover if this argument gives us the entity we crave.
  45. */
  46. function uuid_entity_uuid_context($arg = NULL, $conf = NULL, $empty = FALSE) {
  47. $entity_type = explode(':', $conf['name']);
  48. $entity_type = $entity_type[1];
  49. // If unset it wants a generic, unfilled context.
  50. if ($empty) {
  51. return ctools_context_create_empty('entity:' . $entity_type);
  52. }
  53. // We can accept either an entity object or a pure id.
  54. if (is_object($arg)) {
  55. return ctools_context_create('entity:' . $entity_type, $arg);
  56. }
  57. if (!is_string($arg)) {
  58. return FALSE;
  59. }
  60. $entity_ids = entity_get_id_by_uuid($entity_type, array($arg));
  61. if (!isset($entity_ids[$arg])) {
  62. return FALSE;
  63. }
  64. $entity_id = $entity_ids[$arg];
  65. $entities = entity_load($entity_type, array($entity_id));
  66. if (!$entities) {
  67. return FALSE;
  68. }
  69. return ctools_context_create('entity:' . $entity_type, $entities[$entity_id]);
  70. }