entity_uuid.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. function uuid_entity_uuid_get_child($plugin, $parent, $child) {
  18. $plugins = uuid_entity_uuid_get_children($plugin, $parent);
  19. return $plugins[$parent . ':' . $child];
  20. }
  21. function uuid_entity_uuid_get_children($original_plugin, $parent) {
  22. $entities = entity_get_info();
  23. $plugins = array();
  24. foreach ($entities as $entity_type => $entity) {
  25. $plugin = $original_plugin;
  26. $plugin['title'] = t('@entity: UUID', array('@entity' => $entity['label']));
  27. $plugin['keyword'] = $entity_type;
  28. $plugin['description'] = t('Creates @entity context from an UUID argument.', array('@entity' => $entity_type));
  29. $plugin['name'] = $parent . ':' . $entity_type;
  30. $plugin_id = $parent . ':' . $entity_type;
  31. drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id);
  32. $plugins[$plugin_id] = $plugin;
  33. }
  34. drupal_alter('ctools_entity_contexts', $plugins);
  35. return $plugins;
  36. }
  37. /**
  38. * Discover if this argument gives us the entity we crave.
  39. */
  40. function uuid_entity_uuid_context($arg = NULL, $conf = NULL, $empty = FALSE) {
  41. $entity_type = explode(':', $conf['name']);
  42. $entity_type = $entity_type[1];
  43. // If unset it wants a generic, unfilled context.
  44. if ($empty) {
  45. return ctools_context_create_empty('entity:' . $entity_type);
  46. }
  47. // We can accept either an entity object or a pure id.
  48. if (is_object($arg)) {
  49. return ctools_context_create('entity:' . $entity_type, $arg);
  50. }
  51. if (!is_string($arg)) {
  52. return FALSE;
  53. }
  54. $entity_ids = entity_get_id_by_uuid($entity_type, array($arg));
  55. if (!isset($entity_ids[$arg])) {
  56. return FALSE;
  57. }
  58. $entity_id = $entity_ids[$arg];
  59. $entities = entity_load($entity_type, array($entity_id));
  60. if (!$entities) {
  61. return FALSE;
  62. }
  63. return ctools_context_create('entity:' . $entity_type, $entities[$entity_id]);
  64. }