entity_from_field.inc

  1. cis7 sites/all/modules/ulmus/ctools/plugins/relationships/entity_from_field.inc
  2. cle7 sites/all/modules/ulmus/ctools/plugins/relationships/entity_from_field.inc
  3. ecd7 sites/all/modules/ulmus/ctools/plugins/relationships/entity_from_field.inc
  4. elmsmedia7 sites/all/modules/ulmus/ctools/plugins/relationships/entity_from_field.inc
  5. harmony7 sites/all/modules/ulmus/ctools/plugins/relationships/entity_from_field.inc
  6. icor7 sites/all/modules/ulmus/ctools/plugins/relationships/entity_from_field.inc
  7. meedjum_blog7 sites/all/modules/ulmus/ctools/plugins/relationships/entity_from_field.inc
  8. mooc7 sites/all/modules/ulmus/ctools/plugins/relationships/entity_from_field.inc

Plugin to provide an relationship handler for an entity from a field.

Functions

File

sites/all/modules/ulmus/ctools/plugins/relationships/entity_from_field.inc
View source
  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. $delta = !empty($conf['delta']) ? intval($conf['delta']) : 0;
  135. $plugin = $conf['name'];
  136. list($plugin, $plugin_name) = explode(':', $plugin);
  137. list($field_name, $from_entity, $to_entity) = explode('-', $plugin_name);
  138. // If unset it wants a generic, unfilled context, which is just NULL.
  139. $entity_info = entity_get_info($from_entity);
  140. if (empty($context->data) || !isset($context->data->{$entity_info['entity keys']['id']})) {
  141. return ctools_context_create_empty('entity:' . $to_entity, NULL);
  142. }
  143. if (isset($context->data->{$entity_info['entity keys']['id']})) {
  144. // Load the entity.
  145. $id = $context->data->{$entity_info['entity keys']['id']};
  146. $entity = entity_load($from_entity, array($id));
  147. $entity = $entity[$id];
  148. if ($items = field_get_items($from_entity, $entity, $field_name)) {
  149. if (isset($items[$delta])) {
  150. ctools_include('fields');
  151. $to_entity_info = entity_get_info($to_entity);
  152. $plugin_info = ctools_get_relationship($conf['name']);
  153. $to_entity_id = $items[$delta][$plugin_info['source key']];
  154. $loaded_to_entity = array_shift(entity_load($to_entity, array($to_entity_id)));
  155. if(function_exists($to_entity_info['access callback']) && !call_user_func($to_entity_info['access callback'], 'view', $loaded_to_entity)) {
  156. return ctools_context_create_empty('entity:' . $to_entity, NULL);
  157. }
  158. else {
  159. // Send it to ctools.
  160. return ctools_context_create('entity:' . $to_entity, $to_entity_id);
  161. }
  162. }
  163. else {
  164. // In case that delta was empty.
  165. return ctools_context_create_empty('entity:' . $to_entity, NULL);
  166. }
  167. }
  168. }
  169. }
  170. function ctools_entity_from_field_edit_form($form, &$form_state) {
  171. $field = field_info_field($form_state['plugin']['field name']);
  172. $conf = $form_state['conf'];
  173. if ($field && $field['cardinality'] != 1) {
  174. if ($field['cardinality'] == -1) {
  175. $form['delta'] = array(
  176. '#type' => 'textfield',
  177. '#title' => t('Delta'),
  178. '#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.'),
  179. '#default_value' => !empty($conf['delta']) ? $conf['delta'] : 0,
  180. );
  181. }
  182. else {
  183. $form['delta'] = array(
  184. '#type' => 'select',
  185. '#title' => t('Delta'),
  186. '#description' => t('The relationship can only create one context, but multiple items can be related. Please select which one.'),
  187. '#options' => range(1, $field['cardinality']),
  188. '#default_value' => !empty($conf['delta']) ? $conf['delta'] : 0,
  189. );
  190. }
  191. }
  192. return $form;
  193. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.