entity_id.inc 5.1 KB

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