entity.inc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide a node context. A node context is a node wrapped in a
  5. * context object that can be utilized by anything that accepts contexts.
  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"),
  13. 'description' => t('Entity object.'),
  14. 'context' => 'ctools_context_create_entity',
  15. 'edit form' => 'ctools_context_entity_settings_form',
  16. 'defaults' => array('entity_id' => ''),
  17. 'convert list' => 'ctools_context_entity_convert_list',
  18. 'convert' => 'ctools_context_entity_convert',
  19. 'placeholder form' => array(
  20. '#type' => 'textfield',
  21. '#description' => t('Enter the ID of an entity for this context.'),
  22. ),
  23. // Tell ctools_context_get_context_from_context() how to interpret its $argument.
  24. 'placeholder name' => 'entity_id',
  25. 'get child' => 'ctools_context_entity_get_child',
  26. 'get children' => 'ctools_context_entity_get_children',
  27. );
  28. function ctools_context_entity_get_child($plugin, $parent, $child) {
  29. $plugins = ctools_context_entity_get_children($plugin, $parent);
  30. return $plugins[$parent . ':' . $child];
  31. }
  32. function ctools_context_entity_get_children($plugin, $parent) {
  33. $entities = entity_get_info();
  34. $plugins = array();
  35. foreach ($entities as $entity_type => $entity) {
  36. $child_plugin = $plugin;
  37. $child_plugin['title'] = $entity['label'];
  38. $child_plugin['keyword'] = $entity_type;
  39. $child_plugin['context name'] = $entity_type;
  40. $child_plugin['name'] = $parent . ':' . $entity_type;
  41. $child_plugin['description'] = t('Creates @entity context from an entity ID.', array('@entity' => $entity_type));
  42. $child_plugin_id = $parent . ':' . $entity_type;
  43. drupal_alter('ctools_entity_context', $child_plugin, $entity, $child_plugin_id);
  44. $plugins[$child_plugin_id] = $child_plugin;
  45. }
  46. drupal_alter('ctools_entity_contexts', $plugins);
  47. return $plugins;
  48. }
  49. /**
  50. * It's important to remember that $conf is optional here, because contexts
  51. * are not always created from the UI.
  52. */
  53. function ctools_context_create_entity($empty, $data = NULL, $conf = FALSE, $plugin) {
  54. $entity_type = $plugin['keyword'];
  55. $entity = entity_get_info($entity_type);
  56. $context = new ctools_context(array('entity:' . $entity_type, 'entity', $entity_type));
  57. $context->plugin = $plugin['name'];
  58. $context->keyword = $entity_type;
  59. if ($empty) {
  60. return $context;
  61. }
  62. // Attempt to retain compatibility with broken id:
  63. if (is_array($data) && !isset($data['entity_id']) && isset($data['id'])) {
  64. $id = $data['id'];
  65. }
  66. elseif (is_array($data) && isset($data['entity_id'])) {
  67. $id = $data['entity_id'];
  68. }
  69. elseif (is_object($data)) {
  70. $ids = entity_extract_ids($entity_type, $data);
  71. $id = $ids[0];
  72. }
  73. elseif (is_numeric($data)) {
  74. $id = $data;
  75. $data = entity_load($entity_type, array($id));
  76. $data = !empty($data[$id]) ? $data[$id] : FALSE;
  77. }
  78. if (is_array($data)) {
  79. $data = entity_load($entity_type, array($id));
  80. $data = !empty($data[$id]) ? $data[$id] : FALSE;
  81. }
  82. if (!empty($data)) {
  83. $context->data = $data;
  84. if (!empty($entity['entity keys']['label'])) {
  85. $context->title = $data->{$entity['entity keys']['label']};
  86. }
  87. $context->argument = $id;
  88. if ($entity['entity keys']['bundle']) {
  89. $context->restrictions['type'] = array($data->{$entity['entity keys']['bundle']});
  90. }
  91. return $context;
  92. }
  93. }
  94. function ctools_context_entity_settings_form($form, &$form_state) {
  95. $conf = &$form_state['conf'];
  96. $plugin = &$form_state['plugin'];
  97. $form['entity'] = array(
  98. '#title' => t('Enter the title or ID of a @entity entity', array('@entity' => $plugin['keyword'])),
  99. '#type' => 'textfield',
  100. '#maxlength' => 512,
  101. '#autocomplete_path' => 'ctools/autocomplete/' . $plugin['keyword'],
  102. '#weight' => -10,
  103. );
  104. if (!empty($conf['entity_id'])) {
  105. $info = entity_load($plugin['keyword'], array($conf['entity_id']));
  106. $info = $info[$conf['entity_id']];
  107. if ($info) {
  108. $entity = entity_get_info($plugin['keyword']);
  109. $uri = entity_uri($plugin['keyword'], $info);
  110. if (is_array($uri) && $entity['entity keys']['label']) {
  111. $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));
  112. }
  113. elseif (is_array($uri)) {
  114. $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));
  115. }
  116. elseif ($entity['entity keys']['label']) {
  117. $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));
  118. }
  119. else {
  120. $link = t("[%type id %id]", array('%type' => $plugin['keyword'], '%id' => $conf['entity_id']));
  121. }
  122. $form['entity']['#description'] = t('Currently set to !link', array('!link' => $link));
  123. }
  124. }
  125. $form['entity_id'] = array(
  126. '#type' => 'value',
  127. '#value' => $conf['entity_id'],
  128. );
  129. $form['entity_type'] = array(
  130. '#type' => 'value',
  131. '#value' => $plugin['keyword'],
  132. );
  133. $form['set_identifier'] = array(
  134. '#type' => 'checkbox',
  135. '#default_value' => FALSE,
  136. '#title' => t('Reset identifier to entity label'),
  137. '#description' => t('If checked, the identifier will be reset to the entity label of the selected entity.'),
  138. );
  139. return $form;
  140. }
  141. /**
  142. * Validate a node.
  143. */
  144. function ctools_context_entity_settings_form_validate($form, &$form_state) {
  145. // Validate the autocomplete.
  146. if (empty($form_state['values']['entity_id']) && empty($form_state['values']['entity'])) {
  147. form_error($form['entity'], t('You must select an entity.'));
  148. return;
  149. }
  150. if (empty($form_state['values']['entity'])) {
  151. return;
  152. }
  153. $id = $form_state['values']['entity'];
  154. $preg_matches = array();
  155. $match = preg_match('/\[id: (\d+)\]/', $id, $preg_matches);
  156. if (!$match) {
  157. $match = preg_match('/^id: (\d+)/', $id, $preg_matches);
  158. }
  159. if ($match) {
  160. $id = $preg_matches[1];
  161. }
  162. if (is_numeric($id)) {
  163. $entity = entity_load($form_state['values']['entity_type'], array($id));
  164. $entity = $entity[$id];
  165. }
  166. else {
  167. $entity_info = entity_get_info($form_state['values']['entity_type']);
  168. $field = $entity_info['entity keys']['label'];
  169. $entity = entity_load($form_state['values']['entity_type'], FALSE, array($field => $id));
  170. }
  171. // Do not allow unpublished nodes to be selected by unprivileged users
  172. // || (empty($node->status) && !(user_access('administer nodes'))) need a new sanity check at some point.
  173. if (!$entity) {
  174. form_error($form['entity'], t('Invalid entity selected.'));
  175. }
  176. else {
  177. $entity_id = entity_extract_ids($form_state['values']['entity_type'], $entity);
  178. form_set_value($form['entity_id'], $entity_id[0], $form_state);
  179. }
  180. }
  181. function ctools_context_entity_settings_form_submit($form, &$form_state) {
  182. if ($form_state['values']['set_identifier']) {
  183. $entity_info = entity_get_info($form_state['values']['entity_type']);
  184. $entity = entity_load($form_state['values']['entity_type'], array($form_state['values']['entity_id']));
  185. $entity = $entity[$form_state['values']['entity_id']];
  186. $form_state['values']['identifier'] = $entity->{$entity_info['entity keys']['label']};
  187. }
  188. // This will either be the value set previously or a value set by the
  189. // validator.
  190. $form_state['conf']['entity_id'] = $form_state['values']['entity_id'];
  191. }
  192. /**
  193. * Provide a list of ways that this context can be converted to a string.
  194. */
  195. function ctools_context_entity_convert_list($plugin) {
  196. $list = array();
  197. $entity = entity_get_info($plugin['context name']);
  198. if (isset($entity['token type'])) {
  199. $token = $entity['token type'];
  200. }
  201. else {
  202. $token = $plugin['context name'];
  203. }
  204. // Hack: we need either token.module or a core fix for this to work right,
  205. // until then, we just muscle it.
  206. if ($token == 'taxonomy_term') {
  207. $token = 'term';
  208. }
  209. $tokens = token_info();
  210. if (isset($tokens['tokens'][$token])) {
  211. foreach ($tokens['tokens'][$token] as $id => $info) {
  212. if (!isset($list[$id])) {
  213. $list[$id] = $info['name'];
  214. }
  215. }
  216. }
  217. return $list;
  218. }
  219. /**
  220. * Convert a context into a string.
  221. */
  222. function ctools_context_entity_convert($context, $type, $options = array()) {
  223. $entity_type = $context->type[2];
  224. $entity = entity_get_info($entity_type);
  225. if (isset($entity['token type'])) {
  226. $token = $entity['token type'];
  227. }
  228. else {
  229. $token = $entity_type;
  230. }
  231. // Hack: we need either token.module or a core fix for this to work right,
  232. // until then, we just muscle it.
  233. if ($token == 'taxonomy_term') {
  234. $token = 'term';
  235. }
  236. $tokens = token_info();
  237. $values = token_generate($token, array($type => $type), array($token => $context->data), $options);
  238. if (isset($values[$type])) {
  239. return $values[$type];
  240. }
  241. }