entity_id.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. 'default' => array(
  18. 'entity_id' => ''
  19. ),
  20. 'placeholder form' => 'ctools_argument_entity_id_ctools_argument_placeholder',
  21. );
  22. function ctools_argument_entity_id_get_child($plugin, $parent, $child) {
  23. $plugins = ctools_argument_entity_id_get_children($plugin, $parent);
  24. return $plugins[$parent . ':' . $child];
  25. }
  26. function ctools_argument_entity_id_get_children($original_plugin, $parent) {
  27. $entities = entity_get_info();
  28. $plugins = array();
  29. foreach ($entities as $entity_type => $entity) {
  30. $plugin = $original_plugin;
  31. $plugin['title'] = t('@entity: ID', array('@entity' => $entity['label']));
  32. $plugin['keyword'] = $entity_type;
  33. $plugin['description'] = t('Creates @entity context from an ID argument.', array('@entity' => $entity_type));
  34. $plugin['name'] = $parent . ':' . $entity_type;
  35. $plugin_id = $parent . ':' . $entity_type;
  36. drupal_alter('ctools_entity_context', $plugin, $entity, $plugin_id);
  37. $plugins[$plugin_id] = $plugin;
  38. }
  39. drupal_alter('ctools_entity_contexts', $plugins);
  40. return $plugins;
  41. }
  42. /**
  43. * Discover if this argument gives us the entity we crave.
  44. */
  45. function ctools_argument_entity_id_context($arg = NULL, $conf = NULL, $empty = FALSE) {
  46. $entity_type = explode(':', $conf['name']);
  47. $entity_type = $entity_type[1];
  48. // If unset it wants a generic, unfilled context.
  49. if ($empty) {
  50. return ctools_context_create_empty('entity:' . $entity_type);
  51. }
  52. // We can accept either an entity object or a pure id.
  53. if (is_object($arg)) {
  54. return ctools_context_create('entity:' . $entity_type, $arg);
  55. }
  56. if (!is_numeric($arg)) {
  57. $preg_matches = array();
  58. $match = preg_match('/\[id: (\d+)\]/', $arg, $preg_matches);
  59. if (!$match) {
  60. $match = preg_match('/^id: (\d+)/', $arg, $preg_matches);
  61. }
  62. if ($match) {
  63. $id = $preg_matches[1];
  64. }
  65. if (is_numeric($id)) {
  66. return ctools_context_create('entity:' . $entity_type, $id);
  67. }
  68. return FALSE;
  69. }
  70. $entity = entity_load($entity_type, array($arg));
  71. if (!$entity) {
  72. return FALSE;
  73. }
  74. return ctools_context_create('entity:' . $entity_type, $entity[$arg]);
  75. }
  76. function ctools_argument_entity_id_settings_form(&$form, &$form_state, $conf) {
  77. $plugin = &$form_state['plugin'];
  78. $form['settings']['entity'] = array(
  79. '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $plugin['keyword'])),
  80. '#type' => 'textfield',
  81. '#maxlength' => 512,
  82. '#autocomplete_path' => 'ctools/autocomplete/' . $plugin['keyword'],
  83. '#weight' => -10,
  84. );
  85. if (!empty($conf['entity_id'])) {
  86. $info = entity_load($plugin['keyword'], array($conf['entity_id']));
  87. $info = $info[$conf['entity_id']];
  88. if ($info) {
  89. $entity = entity_get_info($plugin['keyword']);
  90. $uri = entity_uri($plugin['keyword'], $info);
  91. if (is_array($uri) && $entity['entity keys']['label']) {
  92. $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
  93. }
  94. elseif (is_array($uri)) {
  95. $link = l(t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), $uri['path'], array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
  96. }
  97. elseif ($entity['entity keys']['label']) {
  98. $link = l(t("'%title' [%type id %id]", array('%title' => $info->{$entity['entity keys']['label']}, '%type' => $plugin['keyword'], '%id' => $conf['entity_id'])), file_create_url($uri), array('attributes' => array('target' => '_blank', 'title' => t('Open in new window')), 'html' => TRUE));
  99. }
  100. else {
  101. $link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id']));
  102. }
  103. $form['settings']['entity']['#description'] = t('Currently set to !link', array('!link' => $link));
  104. }
  105. }
  106. $form['settings']['entity_id'] = array(
  107. '#type' => 'value',
  108. '#value' => $conf['entity_id'],
  109. );
  110. $form['settings']['entity_type'] = array(
  111. '#type' => 'value',
  112. '#value' => $plugin['keyword'],
  113. );
  114. return $form;
  115. }
  116. function ctools_argument_entity_id_ctools_argument_placeholder($conf) {
  117. $conf = array(
  118. '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $conf['keyword'])),
  119. '#type' => 'textfield',
  120. '#maxlength' => 512,
  121. '#autocomplete_path' => 'ctools/autocomplete/' . $conf['keyword'],
  122. '#weight' => -10,
  123. );
  124. return $conf;
  125. }