entity.inc 9.0 KB

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