entity_uuid.inc 2.5 KB

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