entity_id.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. // Trim spaces and other garbage.
  57. $arg = trim($arg);
  58. if (!is_numeric($arg)) {
  59. $preg_matches = array();
  60. $match = preg_match('/\[id: (\d+)\]/', $arg, $preg_matches);
  61. if (!$match) {
  62. $match = preg_match('/^id: (\d+)/', $arg, $preg_matches);
  63. }
  64. if ($match) {
  65. $id = $preg_matches[1];
  66. }
  67. if (isset($id) && is_numeric($id)) {
  68. return ctools_context_create('entity:' . $entity_type, $id);
  69. }
  70. return FALSE;
  71. }
  72. $entities = entity_load($entity_type, array($arg));
  73. if (empty($entities)) {
  74. return FALSE;
  75. }
  76. return ctools_context_create('entity:' . $entity_type, reset($entities));
  77. }
  78. function ctools_argument_entity_id_settings_form(&$form, &$form_state, $conf) {
  79. $plugin = &$form_state['plugin'];
  80. $form['settings']['entity'] = array(
  81. '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $plugin['keyword'])),
  82. '#type' => 'textfield',
  83. '#maxlength' => 512,
  84. '#autocomplete_path' => 'ctools/autocomplete/' . $plugin['keyword'],
  85. '#weight' => -10,
  86. );
  87. if (!empty($conf['entity_id'])) {
  88. $info = entity_load($plugin['keyword'], array($conf['entity_id']));
  89. $info = $info[$conf['entity_id']];
  90. if ($info) {
  91. $entity = entity_get_info($plugin['keyword']);
  92. $uri = entity_uri($plugin['keyword'], $info);
  93. if (is_array($uri) && $entity['entity keys']['label']) {
  94. $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));
  95. }
  96. elseif (is_array($uri)) {
  97. $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));
  98. }
  99. elseif ($entity['entity keys']['label']) {
  100. $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));
  101. }
  102. else {
  103. $link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id']));
  104. }
  105. $form['settings']['entity']['#description'] = t('Currently set to !link', array('!link' => $link));
  106. }
  107. }
  108. $form['settings']['entity_id'] = array(
  109. '#type' => 'value',
  110. '#value' => isset($conf['entity_id']) ? $conf['entity_id'] : '',
  111. );
  112. $form['settings']['entity_type'] = array(
  113. '#type' => 'value',
  114. '#value' => $plugin['keyword'],
  115. );
  116. return $form;
  117. }
  118. function ctools_argument_entity_id_ctools_argument_placeholder($conf) {
  119. $conf = array(
  120. '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $conf['keyword'])),
  121. '#type' => 'textfield',
  122. '#maxlength' => 512,
  123. '#autocomplete_path' => 'ctools/autocomplete/' . $conf['keyword'],
  124. '#weight' => -10,
  125. );
  126. return $conf;
  127. }