entity_id.inc 2.1 KB

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