comparison sites/all/modules/ctools/plugins/relationships/entity_from_field.inc @ 0:ff03f76ab3fe

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