entity_from_field.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * @file
  4. * Plugin to provide an relationship handler for an entity from a field.
  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'),
  12. 'description' => t('Creates an entity context from a foreign key on a field.'),
  13. 'context' => 'ctools_entity_from_field_context',
  14. 'edit form' => 'ctools_entity_from_field_edit_form',
  15. 'get child' => 'ctools_entity_from_field_get_child',
  16. 'get children' => 'ctools_entity_from_field_get_children',
  17. 'defaults' => array('delta' => 0),
  18. );
  19. function ctools_entity_from_field_get_child($plugin, $parent, $child) {
  20. $plugins = ctools_entity_from_field_get_children($plugin, $parent);
  21. return $plugins[$parent . ':' . $child];
  22. }
  23. function ctools_entity_from_field_get_children($parent_plugin, $parent) {
  24. $cid = $parent_plugin['name'] . ':' . $parent;
  25. $cache = &drupal_static(__FUNCTION__);
  26. if (!empty($cache[$cid])) {
  27. return $cache[$cid];
  28. }
  29. ctools_include('fields');
  30. $entities = entity_get_info();
  31. $plugins = array();
  32. $context_types = array();
  33. // Get the schema information for every field.
  34. $fields_info = field_info_fields();
  35. foreach ($fields_info as $field_name => $field) {
  36. foreach ($field['bundles'] as $from_entity => $bundles) {
  37. foreach ($bundles as $bundle) {
  38. // There might be fields attached to bundles that are disabled (e.g. a
  39. // module that declared a node's content type, is now disabled), but the
  40. // field instance is still shown.
  41. if (!empty($entities[$from_entity]['bundles'][$bundle])) {
  42. $foreign_keys = ctools_field_foreign_keys($field_name);
  43. foreach ($foreign_keys as $key => $info) {
  44. if (isset($info['table'])) {
  45. foreach ($entities as $to_entity => $to_entity_info) {
  46. $from_entity_info = $entities[$from_entity];
  47. // If somehow the bundle doesn't exist on the to-entity,
  48. // skip.
  49. if (!isset($from_entity_info['bundles'][$bundle])) {
  50. continue;
  51. }
  52. if (isset($to_entity_info['base table']) && $to_entity_info['base table'] == $info['table'] && array_keys($info['columns'], $to_entity_info['entity keys']['id'])) {
  53. $name = $field_name . '-' . $from_entity . '-' . $to_entity;
  54. $plugin_id = $parent . ':' . $name;
  55. // Record the bundle for later.
  56. $context_types[$plugin_id]['types'][$bundle] = $from_entity_info['bundles'][$bundle]['label'];
  57. // We check for every bundle; this plugin may already have
  58. // been created, so don't recreate it.
  59. if (!isset($plugins[$plugin_id])) {
  60. $plugin = $parent_plugin;
  61. $replacements = array(
  62. '@to_entity' => $to_entity_info['label'],
  63. '@from_entity' => $from_entity_info['label'],
  64. '@field_name' => $field_name,
  65. '@field_label' => ctools_field_label($field_name),
  66. );
  67. $plugin['title'] = t('@to_entity from @from_entity (on @from_entity: @field_label [@field_name])', $replacements);
  68. $plugin['keyword'] = $to_entity;
  69. $plugin['context name'] = $name;
  70. $plugin['name'] = $plugin_id;
  71. $plugin['description'] = t('Creates a @to_entity context from @from_entity using the @field_name field on @from_entity.', $replacements);
  72. $plugin['from entity'] = $from_entity;
  73. $plugin['to entity'] = $to_entity;
  74. $plugin['field name'] = $field_name;
  75. $plugin['join key'] = $key;
  76. $plugin['source key'] = current(array_keys($info['columns']));
  77. $plugin['parent'] = $parent;
  78. $plugins[$plugin_id] = $plugin;
  79. /*
  80. -- commented out until I figure out how to actually load the reverse properly.
  81. // Build the reverse
  82. $plugin = $parent_plugin;
  83. $name = $field_name . '-' . $from_entity . '-' . $to_entity . '-reverse';
  84. $plugin_id = $parent . ':' . $name;
  85. $plugin['title'] = t('@from_entity from @to_entity (on @from_entity: @field_name)', $replacements);
  86. $plugin['keyword'] = $to_entity;
  87. $plugin['context name'] = $name;
  88. $plugin['name'] = $plugin_id;
  89. $plugin['description'] = t('Creates a @from_entity context from @to_entity using the @field_name field on @from_entity.', $replacements);
  90. $plugin['from entity'] = $from_entity;
  91. $plugin['to entity'] = $to_entity;
  92. $plugin['field name'] = $field_name;
  93. $plugin['reverse'] = TRUE;
  94. $plugin['parent'] = $parent;
  95. // Since we can't apply restrictions on the reverse relationship
  96. // we just add the required context here.
  97. $plugin['required context'] = new ctools_context_required($to_entity_info['label'], $to_entity);
  98. $plugin_entities = array(
  99. 'to' => array($from_entity => $from_entity_info),
  100. 'from' => array($to_entity => $to_entity_info)
  101. );
  102. drupal_alter('ctools_entity_context', $plugin, $plugin_entities, $plugin_id);
  103. $plugins[$plugin_id] = $plugin;
  104. */
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }
  114. foreach ($context_types as $key => $context) {
  115. list($parent, $plugin_name) = explode(':', $key);
  116. list($field_name, $from_entity, $to_entity) = explode('-', $plugin_name);
  117. $from_entity_info = $entities[$from_entity];
  118. $to_entity_info = $entities[$to_entity];
  119. $plugins[$key]['required context'] = new ctools_context_required($from_entity_info['label'], $from_entity, array('type' => array_keys($context['types'])));
  120. $plugin_entities = array(
  121. 'to' => array($to_entity => $to_entity_info),
  122. 'from' => array($from_entity => $from_entity_info)
  123. );
  124. drupal_alter('ctools_entity_context', $plugins[$key], $plugin_entities, $key);
  125. }
  126. drupal_alter('ctools_entity_contexts', $plugins);
  127. $cache[$cid] = $plugins;
  128. return $plugins;
  129. }
  130. /**
  131. * Return a new context based on an existing context.
  132. */
  133. function ctools_entity_from_field_context($context, $conf) {
  134. // Perform access check on current logged in user.
  135. global $user;
  136. // Clone user object so account can be passed by value to access callback.
  137. $account = clone $user;
  138. $delta = !empty($conf['delta']) ? intval($conf['delta']) : 0;
  139. $plugin = $conf['name'];
  140. list($plugin, $plugin_name) = explode(':', $plugin);
  141. list($field_name, $from_entity, $to_entity) = explode('-', $plugin_name);
  142. // If unset it wants a generic, unfilled context, which is just NULL.
  143. $entity_info = entity_get_info($from_entity);
  144. if (empty($context->data) || !isset($context->data->{$entity_info['entity keys']['id']})) {
  145. return ctools_context_create_empty('entity:' . $to_entity, NULL);
  146. }
  147. if (isset($context->data->{$entity_info['entity keys']['id']})) {
  148. // Load the entity.
  149. $id = $context->data->{$entity_info['entity keys']['id']};
  150. $entity = entity_load($from_entity, array($id));
  151. $entity = $entity[$id];
  152. if ($items = field_get_items($from_entity, $entity, $field_name)) {
  153. if (isset($items[$delta])) {
  154. ctools_include('fields');
  155. $to_entity_info = entity_get_info($to_entity);
  156. $plugin_info = ctools_get_relationship($conf['name']);
  157. $to_entity_id = $items[$delta][$plugin_info['source key']];
  158. $loaded_to_entity = entity_load($to_entity, array($to_entity_id));
  159. $loaded_to_entity = array_shift($loaded_to_entity);
  160. // Pass current user account and entity type to access callback.
  161. if (isset($to_entity_info['access callback']) && function_exists($to_entity_info['access callback']) && !call_user_func($to_entity_info['access callback'], 'view', $loaded_to_entity, $account, $to_entity)) {
  162. return ctools_context_create_empty('entity:' . $to_entity, NULL);
  163. }
  164. else {
  165. // Send it to ctools.
  166. return ctools_context_create('entity:' . $to_entity, $to_entity_id);
  167. }
  168. }
  169. else {
  170. // In case that delta was empty.
  171. return ctools_context_create_empty('entity:' . $to_entity, NULL);
  172. }
  173. }
  174. }
  175. }
  176. function ctools_entity_from_field_edit_form($form, &$form_state) {
  177. $field = field_info_field($form_state['plugin']['field name']);
  178. $conf = $form_state['conf'];
  179. if ($field && $field['cardinality'] != 1) {
  180. if ($field['cardinality'] == -1) {
  181. $form['delta'] = array(
  182. '#type' => 'textfield',
  183. '#title' => t('Delta'),
  184. '#description' => t('The relationship can only create one context, but multiple items can be related. Please select which one. Since this can have unlimited items, type in the number you want. The first one will be 0.'),
  185. '#default_value' => !empty($conf['delta']) ? $conf['delta'] : 0,
  186. );
  187. }
  188. else {
  189. $form['delta'] = array(
  190. '#type' => 'select',
  191. '#title' => t('Delta'),
  192. '#description' => t('The relationship can only create one context, but multiple items can be related. Please select which one.'),
  193. '#options' => range(1, $field['cardinality']),
  194. '#default_value' => !empty($conf['delta']) ? $conf['delta'] : 0,
  195. );
  196. }
  197. }
  198. return $form;
  199. }