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