-
Notifications
You must be signed in to change notification settings - Fork 9
/
createapi.helpers.inc
428 lines (384 loc) · 14.2 KB
/
createapi.helpers.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/**
* @file
* Helper functions used to create feeds for CreateAPI.
*/
include 'createapi.helpers.filters.inc';
include 'createapi.helpers.json.inc';
/**
* Process a set of entities IDs, of one type, into an array for output.
*
* @param array $eids
* An array of IDs.
* @param string $entity_type
* The entity type being dealt with.
* @param array $fields
* (optional) Fields to be output. @see hook_createapi_custom_entities().
* @param array $properties
* (optional) Properties to be output. @see hook_createapi_custom_entities().
* @param NULL|string $path
* (optional) The key to use, if any, when outputting the path of this entity.
*
* @return array
* A multidimensional array that can be easily passed into a function such as
* json_encode().
*/
function _createapi__helper__process_entities(array $eids, $entity_type, $fields = array(), $properties = array(), $path = FALSE) {
$output = array();
$entities = entity_load($entity_type, $eids);
foreach ($entities as $entity) {
$field_output = array();
foreach ($properties as $key => $property) {
if (isset($entity->{$property})) {
$field_output[$key] = $entity->{$property};
}
}
foreach ($fields as $key => $field) {
$field_output[$key] = _createapi__helper__feed_format_field($entity_type, $entity, $field);
}
if (isset($path) && $path) {
$path_info = entity_uri($entity_type, $entity);
if (isset($path_info['path'])) {
$field_output[$path] = $path_info['path'];
$aliased_path = drupal_lookup_path('alias', $path_info['path']);
if ($aliased_path) {
$field_output[$path] = drupal_strtolower($aliased_path);
}
}
}
drupal_alter('createapi_process_entity', $field_output, $entity);
$output[] = $field_output;
}
return $output;
}
/**
* Format different field types into an output suitable for use in a feed.
*
* @param string $entity_type
* The type of entity that is being acted upon e.g. 'node' or 'file'.
* @param stdClass $entity
* The entity associated with the field to be formatted.
* @param string $field_name
* The name of the field to be formatted.
*
* @return mixed
* The formatted field as rendered using field_view_value(). The result will
* either be a string or array depending on the cardinality of the field.
*/
function _createapi__helper__feed_format_field($entity_type, $entity, $field_name) {
$output = array();
// File entities or entity references may have attached fields.
if (is_array($field_name)) {
$current = current($field_name);
$field_name = key($field_name);
}
if (isset($entity->{$field_name})) {
$field_info = field_info_field($field_name);
$items = field_get_items($entity_type, $entity, $field_name);
if (is_array($items)) {
for ($i = 0; $i < count($items); $i++) {
if (isset($items[$i])) {
switch ($field_info['type']) {
case 'text':
case 'text_long':
case 'text_with_summary':
case 'list_text':
$view = field_view_value($entity_type, $entity, $field_name, $items[$i]);
if (isset($view['#markup'])) {
$output[] = $view['#markup'];
}
break;
case 'entityreference':
$entity_type = $field_info['settings']['target_type'];
$view = field_view_value($entity_type, $entity, $field_name, $items[$i]);
$reference = array();
if (isset($items[$i]['target_id'])) {
$target_id = $items[$i]['target_id'];
$reference = array(
'eid' => $target_id ,
);
if (isset($current)) {
$referenced_entity = entity_load($entity_type, array($target_id));
if (isset($referenced_entity[$target_id])) {
$referenced_entity = $referenced_entity[$target_id];
// Fields.
if (isset($current['fields'])) {
foreach ($current['fields'] as $key => $field) {
$reference[$key] = _createapi__helper__feed_format_field($entity_type, $referenced_entity, $field);
}
}
// Properties.
if (isset($current['properties'])) {
foreach ($current['properties'] as $key => $property) {
$reference[$key] = $referenced_entity->{$property};
}
}
// Path.
if (isset($current['path'])) {
$path = entity_uri($entity_type, $referenced_entity);
$reference[$current['path']] = drupal_get_path_alias($path['path']);
}
}
}
elseif (isset($view['#markup'])) {
$reference['value'] = $view['#markup'];
}
}
$output[] = $reference;
break;
case 'datetime':
$output[] = strtotime($items[$i]['value']);
break;
case 'datestamp':
$output[] = $items[$i]['value'];
break;
case 'image':
$image = field_view_value($entity_type, $entity, $field_name, $items[$i], array(
'type' => 'image',
'settings' => array(
'image_link' => 'file',
),
));
// Drupal gives us the file entity as an array, so convert this to
// an object.
$file = new stdClass();
foreach ($image['#item'] as $key => $value) {
$file->{$key} = $value;
}
$file_output = array(
'path' => $image['#path']['path'],
'width' => $image['#item']['width'],
'height' => $image['#item']['height'],
'mime' => $image['#item']['filemime'],
);
// Format the output for fields that are attached to this file
// entity by recursively calling this function on child fields.
if (!empty($current['fields'])) {
foreach ($current['fields'] as $key => $field) {
$file_output[$key] = _createapi__helper__feed_format_field('file', $file, $field);
}
}
// Add image styles.
if (!empty($current['styles'])) {
foreach ($current['styles'] as $key => $style) {
$file_output['styles'][$key] = image_style_url($style, $image['#item']['uri']);
}
}
$output[] = $file_output;
break;
case 'link_field':
$output[] = array(
'url' => (isset($items[$i]['url'])) ? $items[$i]['url'] : '',
'title' => (isset($items[$i]['title'])) ? $items[$i]['title'] : '',
);
break;
}
}
}
}
// If the field only takes one value, make sure the output isn't an array.
if (count($output) === 1 && $field_info['cardinality'] == 1) {
$output = array_pop($output);
}
}
return $output;
}
/**
* Retrieve nodes in a given nodequeue.
*
* @param string $queue_name
* The machine name of the nodequeue.
* @param array $fields
* (optional) Fields to be output. @see hook_createapi_nodequeues).
* @param array $properties
* (optional) Properties to be output. @see hook_createapi_nodequeues().
* @param NULL|string $path
* (optional) The key to use, if any, when outputting the path of this entity.
* @param array $filters
* (optional) Which URL filters can be applied to this endpoint. @see
* hook_createapi_nodequeues().
*
* @return array
* A formatted array of nodes for outputting.
*/
function _createapi__helper__process_node_queue($queue_name, $fields = array(), $properties = array(), $path = FALSE, $filters = array()) {
$nids = array();
$query = db_select('nodequeue_queue', 'q');
$query->join('nodequeue_nodes', 'o', 'o.qid = q.qid AND q.name = :qname', array(':qname' => $queue_name));
$query->join('node', 'n', 'o.nid = n.nid');
$query
->fields('n', array('nid'))
->condition('n.status', 1)
->orderBy('position', 'ASC');
// Limit results.
$range = (isset($filters['range'])) ? $filters['range']: FALSE;
$offset = (isset($filters['offset'])) ? $filters['offset']: FALSE;
_createapi__helper__filter__range_offset($query, $range, $offset);
foreach ($filters as $filter => $items) {
if (!$query) {
break;
}
switch ($filter) {
case 'properties':
foreach ($items as $param => $property) {
_createapi__helper__filter__property($query, $param, 'n.' . $property);
}
break;
case 'path':
_createapi__helper__filter__node_path($query, $items, 'n.nid');
break;
}
}
if ($query) {
$result = $query->execute();
foreach ($result as $record) {
$nids[] = $record->nid;
}
}
return _createapi__helper__process_entities($nids, 'node', $fields, $properties, $path);
}
/**
* Retrieves items in a given menu.
*
* @param string $menu_name
* The machine name of the menu.
*
* @return array
* A formatted array of menu items for outputting.
*/
function _createapi__helper__process_menu($menu_name) {
$links = menu_load_links($menu_name);
$formatted_links = array();
foreach ($links as $link) {
if (!$link['hidden']) {
$nid = '';
if (substr($link['link_path'], 0, 5) === 'node/') {
$nid = substr($link['link_path'], 5);
}
$formatted_links[] = array(
'internal_path' => $link['link_path'],
'alias' => drupal_get_path_alias($link['link_path']),
'title' => $link['link_title'],
'nid' => $nid,
);
}
}
return $formatted_links;
}
/**
* Retrieves variables.
*
* @param array
* Variables to return. Keys will be the new array keys and the values are the
* variable names as seen in the variable table.
*
* @return array
* A formatted array of variables for outputting.
*/
function _createapi__helper__process_variables($variables) {
foreach ($variables as $key => $value) {
$variables[$key] = variable_get($value);
}
return $variables;
}
/**
* Retrieve nodes of a given content type.
*
* @param string $content_type
* The node content type.
* @param array $fields
* (optional) Fields to be output. @see hook_createapi_content_types().
* @param array $properties
* (optional) Properties to be output. @see hook_createapi_content_types().
* @param NULL|String $path
* (optional) The key to use, if any, when outputting the path of this entity.
* @param array|bool $filters
* (optional) Which URL filters can be applied to this endpoint. @see
* hook_createapi_content_types().
*
* @return array
* A formatted array of nodes for outputting.
*/
function _createapi__helper__process_content_type($content_type, $fields = array(), $properties = array(), $path = FALSE, $filters = array()) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', $content_type)
->propertyCondition('status', 1)
->propertyOrderBy('nid', 'DESC');
return _createapi__helper__process_custom($query, 'node', $fields, $properties, $path, $filters, 'nid');
}
/**
* Retrieve entities from a given EntityFieldQuery.
*
* @param EntityFieldQuery $query
* The un-executed EntityFieldQuery that will give us a set of entity IDs.
* @param string $entity_type
* The type of entity being queried.
* @param array $fields
* (optional) Fields to be output. @see hook_createapi_custom_entities().
* @param array $properties
* (optional) Properties to be output. @see hook_createapi_customentities().
* @param NULL|String $path
* (optional) The key to use, if any, when outputting the path of this entity.
* @param array|bool $filters
* (optional) Which URL filters can be applied to this endpoint. @see
* hook_createapi_custom_entities().
* @param bool|string $nid_alias
* (optional) If this is a node entity, provide the alias used in the query
* for the nid in order to allow path filtering to function.
*
* @return array
* A formatted array of entities for outputting.
*/
function _createapi__helper__process_custom(EntityFieldQuery $query, $entity_type, $fields = array(), $properties = array(), $path = FALSE, $filters = array(), $nid_alias = FALSE) {
$eids = array();
// Add filters.
// Limit results.
$range = FALSE;
$offset = FALSE;
_createapi__helper__filter__range_offset($query, $range, $offset);
foreach ($filters as $filter => $items) {
if (!$query) {
break;
}
switch ($filter) {
case 'properties':
foreach ($items as $param => $property) {
_createapi__helper__filter__property($query, $param, $property);
}
break;
case 'fields':
foreach ($items as $param => $field) {
_createapi__helper__filter__field($query, $param, $field['column'], $field['field']);
}
break;
case 'path':
if ($entity_type === 'node' && $nid_alias) {
_createapi__helper__filter__node_path($query, $items, $nid_alias);
}
break;
// If we set a range/offset on a query, it gets overridden by a further
// call, so we can safely call it twice.
case 'range':
$range = $items;
_createapi__helper__filter__range_offset($query, $range, $offset);
break;
case 'offset':
$offset = $items;
_createapi__helper__filter__range_offset($query, $range, $offset);
break;
case 'start_end':
$property = (isset($items['property'])) ? $items['property'] : FALSE;
$field = (isset($items['field'])) ? $items['field'] : FALSE;
_createapi__helper__filter__start_end($query, $items['start'], $items['end'], $property, $field);
break;
}
}
if ($query) {
$result = $query->execute();
if (isset($result['node'])) {
$eids = array_keys($result['node']);
}
}
return _createapi__helper__process_entities($eids, $entity_type, $fields, $properties, $path);
}