This repository has been archived by the owner on Apr 18, 2024. It is now read-only.
forked from drupalprojects/entityqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entityqueue.module
858 lines (772 loc) · 25.5 KB
/
entityqueue.module
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
<?php
/**
* @file
* Allows users to collect entities in arbitrarily ordered lists.
*/
/**
* Implements hook_ctools_plugin_directory().
*/
function entityqueue_ctools_plugin_directory($module, $plugin) {
return 'plugins/' . $module . '/' . $plugin;
}
/**
* Returns the hook to use in order to determine if modules support the
* entityqueue API.
*
* @return string
*/
function entityqueue_ctools_plugin_api_hook_name() {
return 'entityqueue_api';
}
/**
* Implements hook_ctools_plugin_type().
*/
function entityqueue_ctools_plugin_type() {
$plugins['handler'] = array(
'classes' => array('class'),
// The default behavior of handler plugins is to have multiple subqueues per
// queue.
'defaults' => array('queue type' => 'multiple'),
);
return $plugins;
}
/**
* Gets the handler for a given queue.
*
* @param EntityQueue $queue
* An EntityQueue object.
*
* @return EntityQueueHandlerInterface
*/
function entityqueue_get_handler(EntityQueue $queue) {
$object_cache = &drupal_static(__FUNCTION__);
if (!isset($object_cache[$queue->name])) {
ctools_include('plugins');
$class = ctools_plugin_load_class('entityqueue', 'handler', $queue->handler, 'class');
if (class_exists($class)) {
$object_cache[$queue->name] = call_user_func(array($class, 'getInstance'), $queue);
}
else {
$object_cache[$queue->name] = BrokenEntityQueueHandler::getInstance($queue);
}
}
return $object_cache[$queue->name];
}
/**
* Implements hook_permission().
*/
function entityqueue_permission() {
$permissions = array(
'administer entityqueue' => array(
'title' => t('Administer entityqueue'),
'description' => t('Administer entityqueue configuration and create, update and delete all queues.'),
'restrict access' => TRUE,
),
'manipulate entityqueues' => array(
'title' => t('Manipulate queues'),
'description' => t('Access the entityqueues list.')
),
'manipulate all entityqueues' => array(
'title' => t('Manipulate all queues'),
'description' => t('Access to update all queues.'),
),
);
$queues = entityqueue_queue_load_multiple(array(), TRUE);
$handlers = ctools_get_plugins('entityqueue', 'handler');
foreach ($queues as $name => $queue) {
if ($handlers[$queue->handler]['queue type'] == 'multiple') {
$permissions["create $name entityqueue"] = array(
'title' => t('Add %queue subqueues', array('%queue' => $queue->label())),
'description' => t('Access to create new subqueue to the %queue queue.', array('%queue' => $queue->label())),
);
$permissions["delete $name entityqueue"] = array(
'title' => t('Delete %queue subqueues', array('%queue' => $queue->label())),
'description' => t('Access to delete subqueues of the %queue queue.', array('%queue' => $queue->label())),
);
}
$permissions["update $name entityqueue"] = array(
'title' => t('Manipulate %queue queue', array('%queue' => $queue->label())),
'description' => t('Access to update the %queue queue.', array('%queue' => $queue->label())),
);
}
return $permissions;
}
/**
* Implements hook_menu().
*/
function entityqueue_menu() {
$items = array();
return $items;
}
/**
* Constructs a new EntityQueue object, without saving it to the database.
*
* @param array $values
* An array of queue properties. Defaults to an empty array.
*
* @return EntityQueue|false
* An EntityQueue object, or FALSE if creation fails.
*
* @see EntityQueue
*/
function entityqueue_queue_create($values = array()) {
$values = (array) $values;
// Add default properties if they are not set.
$values += array(
'is_new' => TRUE,
'language' => language_default()->language,
'export_type' => EXPORT_IN_CODE,
);
$queue = new EntityQueue($values);
// Invoke the queue handler in order to allow it to alter the created queue.
entityqueue_get_handler($queue)->create();
return $queue;
}
/**
* Saves a queue.
*
* @param EntityQueue $queue
* EntityQueue object with queue properties. See entityqueue_queue_create().
* @param bool $rebuild_menu
* Boolean indicating whether the the database tables used by various menu
* functions should be rebuilt. Setting this to FALSE is useful if multiple
* queues are being created programmatically. Defaults to TRUE.
*
* @return int|bool
* If the queue insert or update failed, returns FALSE. If it succeeded,
* returns SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
*/
function entityqueue_queue_save(EntityQueue $queue, $rebuild_menu = TRUE) {
// Make sure all keys are populated.
$queue = entityqueue_queue_create($queue);
if ($queue->export_type & EXPORT_IN_DATABASE) {
// Existing queue.
$write_record_keys = array('name');
$queue->is_new = FALSE;
}
else {
// New queue.
$write_record_keys = array();
$queue->export_type = EXPORT_IN_DATABASE;
$queue->is_new = TRUE;
}
entityqueue_get_handler($queue)->preSave();
$transaction = db_transaction();
try {
$return = drupal_write_record('entityqueue_queue', $queue, $write_record_keys);
_entityqueue_queue_ensure_instance($queue);
}
catch (Exception $e) {
$transaction->rollback();
watchdog_exception('Entityqueue', $e);
throw $e;
}
if ($queue->is_new) {
entityqueue_get_handler($queue)->insert();
}
else {
entityqueue_get_handler($queue)->update();
}
if ($rebuild_menu) {
menu_rebuild();
}
return $return;
}
/**
* Loads a queue.
*
* @param string $name
* The machine name of the queue (bundle) to be loaded.
*
* @return EntityQueue|false
* A EntityQueue object in the same format as expected by
* entityqueue_queue_save(), or FALSE if the queue doesn't exist.
*/
function entityqueue_queue_load($name) {
$queues = entityqueue_queue_load_multiple(array($name));
return isset($queues[$name]) ? $queues[$name] : FALSE;
}
/**
* Loads multiple queues.
*
* @param array $names
* An array of machine names of the queues to be loaded. If $names is empty,
* load all queues.
*
* @return EntityQueue[]
* An array of EntityQueue objects, keyed by queue name.
*/
function entityqueue_queue_load_multiple($names = array(), $reset = FALSE) {
ctools_include('export');
$queues = !empty($names) ? ctools_export_load_object('entityqueue_queue', 'names', $names) : ctools_export_crud_load_all('entityqueue_queue', $reset);
// Bail out early if we haven't found any queues.
if (empty($queues)) {
return array();
}
static $recursion = FALSE;
if (!$recursion && !drupal_static('entityqueue_install')) {
$recursion = TRUE;
foreach ($queues as $name => $queue) {
if (!empty($queue->in_code_only)) {
_entityqueue_queue_ensure_instance($queue);
// Invoke a special queue handler method for queues that are stored only
// in code (e.g. a hook_entityqueue_default_queues() implementation).
entityqueue_get_handler($queue)->loadFromCode();
}
entityqueue_get_handler($queue)->load();
}
}
$recursion = FALSE;
return $queues;
}
/**
* Loads multiple queues with a specific target type.
*
* @param string $target_type
* An entity type (e.g. 'node', 'comment', 'user').
*
* @return array
* An array of EntityQueue objects, keyed by queue name.
*/
function entityqueue_queue_load_by_target_type($target_type) {
ctools_include('export');
return ctools_export_load_object('entityqueue_queue', 'conditions', array('target_type' => $target_type));
}
/**
* Deletes a queue.
*
* @param EntityQueue|string $queue
* An EntityQueue object or the machine name of a queue.
*/
function entityqueue_queue_delete($queue) {
// If the argument is not an EntityQueue object, load it now.
if (!is_object($queue)) {
$queue = entityqueue_queue_load($queue);
}
entityqueue_get_handler($queue)->preDelete();
// Delete this queue's subqueues first.
$query = new EntityFieldQuery();
$query
->entityCondition('entity_type', 'entityqueue_subqueue')
->entityCondition('bundle', $queue->name);
$result = $query->execute();
if (!empty($result['entityqueue_subqueue'])) {
if ($queue->export_type == EXPORT_IN_CODE + EXPORT_IN_DATABASE) {
db_delete('entityqueue_queue')
->condition('name', $queue->name)
->execute();
return;
}
entity_delete_multiple('entityqueue_subqueue', array_keys($result['entityqueue_subqueue']));
}
// Delete the entity reference field instance that was created for this queue.
$field_name = _entityqueue_get_target_field_name($queue->target_type);
$entityreference_field = field_read_instance('entityqueue_subqueue', $field_name, $queue->name);
field_delete_instance($entityreference_field, FALSE);
field_attach_delete_bundle('entityqueue_subqueue', $queue->name);
// And finally we can delete the queue.
db_delete('entityqueue_queue')->condition('name', $queue->name)->execute();
entityqueue_get_handler($queue)->postDelete();
}
/**
* Implements hook_entity_info().
*/
function entityqueue_entity_info() {
$return = array(
'entityqueue_subqueue' => array(
'label' => t('Subqueue'),
'plural label' => t('Subqueues'),
'entity class' => 'EntitySubqueue',
'controller class' => 'EntitySubqueueEntityController',
'module' => 'entityqueue',
'base table' => 'entityqueue_subqueue',
'load hook' => 'entityqueue_subqueue_load',
'uri callback' => 'entityqueue_subqueue_uri',
'label callback' => 'entityqueue_subqueue_label',
'access callback' => 'entityqueue_access',
'fieldable' => TRUE,
'entity keys' => array(
'id' => 'subqueue_id',
'bundle' => 'queue',
'label' => 'label',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'name',
),
'view modes' => array(
'full' => array(
'label' => t('queue'),
'custom settings' => FALSE,
),
),
'metadata controller class' => '',
),
);
foreach (entityqueue_queue_load_multiple() as $name => $queue) {
$return['entityqueue_subqueue']['bundles'][$name] = array(
'label' => $queue->label,
'admin' => array(
'path' => 'admin/structure/entityqueue/list/%entityqueue_queue',
'real path' => 'admin/structure/entityqueue/list/' . $name,
'bundle argument' => 4,
'access callback' => 'entityqueue_queue_access',
'access arguments' => array('view', 4),
),
);
}
// Support the Entity cache module.
// if (module_exists('entitycache')) {
// $return['entityqueue']['field cache'] = FALSE;
// $return['entityqueue']['entity cache'] = TRUE;
// }
return $return;
}
/**
* Implements hook_entity_property_info().
*/
function entityqueue_entity_property_info() {
$info = array();
$properties = &$info['entityqueue_subqueue']['properties'];
$properties['subqueue_id'] = array(
'label' => t('Subqueue ID'),
'type' => 'integer',
'description' => t('The unique subqueue ID.'),
'schema field' => 'subqueue_id',
);
// @todo convert to a token type, like the language property
$properties['queue'] = array(
'label' => t('Queue Name'),
'description' => t('The entityqueue machine name.'),
'schema field' => 'queue',
'required' => TRUE,
);
$properties['name'] = array(
'label' => t('Name'),
'description' => t('The subqueue machine name.'),
'schema field' => 'name',
'required' => TRUE,
);
$properties['label'] = array(
'label' => t('Label'),
'description' => t('The subqueue label.'),
'schema field' => 'label',
'required' => TRUE,
);
$properties['language'] = array(
'label' => t('Language'),
'type' => 'token',
'description' => t('The language the subqueue is written in.'),
'setter callback' => 'entity_property_verbatim_set',
'options list' => 'entity_metadata_language_list',
'schema field' => 'language',
'setter permission' => 'administer entityqueue',
);
// @todo figure out how other modules do this
$properties['module'] = array(
'label' => t('Module'),
'description' => t('The machine name of the module that defines the subqueue.'),
'schema field' => 'module',
'required' => TRUE,
);
$properties['uid'] = array(
'label' => t('Author'),
'type' => 'user',
'description' => t('The creator of the subqueue.'),
'setter callback' => 'entity_property_verbatim_set',
'setter permission' => 'administer entityqueue',
'required' => TRUE,
'schema field' => 'uid',
);
return $info;
}
/**
* Access callback for the entity API.
* @see entity_access()
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create' or
* 'delete'.
* @param $entity
* Optionally an entity to check access for. If no entity is given, it will be
* determined whether access is allowed for all entities of the given type.
* @param $account
* The user to check for. Leave it to NULL to check for the global user.
* @param $entity_type
* The entity type of the entity to check for.
*
* @return bool
* TRUE if the user has permission for $op, FALSE otherwise.
*/
function entityqueue_access($op, $entity = NULL, $account = NULL, $entity_type = NULL) {
if (empty($account)) {
global $user;
$account = $user;
}
$administer = user_access('administer entityqueue', $account);
if ($administer || user_access('manipulate all entityqueues', $account)) {
return TRUE;
}
if ($op == 'view' && user_access('manipulate entityqueues', $account)) {
return TRUE;
}
if (!(isset($entity) && is_object($entity))) {
return FALSE;
}
if (!isset($entity->queue)) {
watchdog('entityqueue', 'Missing queue property of entity object in entityqueue_access().', NULL, WATCHDOG_DEBUG);
return FALSE;
}
// For view, if they don't have the 'manipulate entityqueues' permission,
// check all other operations.
if ($op == 'view') {
foreach (array('update', 'create', 'delete') as $subop) {
if (user_access("$subop {$entity->queue} entityqueue", $account)) {
return TRUE;
}
}
}
return user_access("$op {$entity->queue} entityqueue", $account);
}
/**
* Menu access callback.
*/
function entityqueue_queue_access($op, $queue) {
if (is_object($queue) && ($queue instanceof EntityQueue)) {
$queue_name = $queue->name;
}
elseif (is_string($queue)) {
$queue_name = $queue;
}
else {
return;
}
$stub = (object) array(
'queue' => $queue_name,
);
return entity_access($op, 'entityqueue_subqueue', $stub);
}
/**
* Constructs a new EntitySubqueue object, without saving it to the database.
*
* @param array $values
* An array of values to set, keyed by property name.
*
* @return EntitySubqueue
* A new EntitySubqueue object.
*/
function entityqueue_subqueue_create($values = array()) {
return entity_get_controller('entityqueue_subqueue')->create($values);
}
/**
* Saves a subqueue.
*
* @param EntitySubqueue $subqueue
* The full EntitySubqueue object to save.
*
* @return int
* SAVED_NEW or SAVED_UPDATED depending on the operation performed.
*/
function entityqueue_subqueue_save(EntitySubqueue $subqueue) {
return entity_get_controller('entityqueue_subqueue')->save($subqueue);
}
/**
* Loads a subqueue by name or by ID.
*
* @param string|int $name
* The subqueue_id or machine name of a EntitySubqueue.
*
* @return EntitySubqueue|false
* An EntitySubqueue object.
*/
function entityqueue_subqueue_load($name) {
$subqueues = entityqueue_subqueue_load_multiple(array($name));
return $subqueues ? reset($subqueues) : FALSE;
}
/**
* Loads multiple subqueues by ID, name or based on a set of matching conditions.
*
* @param array $names
* An array of queue names or IDs.
* @param array $conditions
* An array of conditions on the {entityqueue_subqueue} table in the form
* 'field' => $value.
* @param bool $reset
* Whether to reset the internal queue loading cache.
*
* @return array
* An array of EntitySubqueue objects, keyed by subuque_id. When no results
* are found, an empty array is returned.
*/
function entityqueue_subqueue_load_multiple($names = FALSE, $conditions = array(), $reset = FALSE) {
if (!empty($names) && !is_numeric(reset($names))) {
$conditions += array('name' => $names);
$names = FALSE;
}
$queues = entity_load('entityqueue_subqueue', $names, $conditions, $reset);
return !empty($queues) ? $queues : array();
}
/**
* Loads multiple subqueues with a specific target type.
*
* @param string $target_type
* An entity type (e.g. 'node', 'comment', 'user').
*
* @return array
* An array of EntitySubqueue objects, keyed by subqueue_id.
*/
function entityqueue_subqueue_load_by_target_type($target_type) {
$queues = entityqueue_queue_load_by_target_type($target_type);
return entity_load('entityqueue_subqueue', FALSE, array('queue' => array_keys($queues)));
}
/**
* Implements hook_contextual_links_view_alter().
*/
function entityqueue_contextual_links_view_alter(&$element, $items) {
// Do not add contextual link on view preview.
if (module_exists('views_ui') && views_ui_contextual_links_suppress()) {
return;
}
// Add contextual link "Edit entityqueue".
$views_ui_element = array();
if (isset($element['#element']['#views_contextual_links_info']['views_ui'])) {
$views_ui_element = $element['#element']['#views_contextual_links_info']['views_ui'];
}
// In case of block #views_contextual_links_info element is inside of
// 'content' and not '#element' directly.
// @see http://drupal.org/node/1413596#comment-5912688
if (empty($views_ui_element) && isset($element['#element']['content']['#views_contextual_links_info']['views_ui'])) {
$views_ui_element = $element['#element']['content']['#views_contextual_links_info']['views_ui'];
}
if (!empty($views_ui_element['view_display_id']) && isset($views_ui_element['view'])) {
$display_id = $views_ui_element['view_display_id'];
$view = $views_ui_element['view'];
$view->build($display_id);
// Proceed only if there is entityqueue sort criteria available.
if (!$sort_key = entityqueue_get_entityqueue_sort($view)) {
return;
}
// Get view display relationships.
$relationships = $view->display[$display_id]->handler->get_option('relationships');
foreach ($relationships as $relationship) {
if ($relationship['field'] == 'entityqueue_relationship') {
$referenced_subqueues = array_keys(array_filter($relationship['queues']));
if (!empty($referenced_subqueues)) {
// Contextual links can handle only one set of links coming from a module,
// so we'll have to settle for the first referenced queue.
$subqueue = entityqueue_subqueue_load(reset($referenced_subqueues));
if ($subqueue) {
$path = 'admin/structure/entityqueue/list/' . $subqueue->name . '/subqueues/' . $subqueue->subqueue_id . '/edit';
$element['#links']['entityqueue-order'] = array(
'title' => t('Edit subqueue'),
'href' => $path,
'query' => array('destination' => current_path()),
);
}
}
}
}
}
}
/**
* Get the entityqueue position sort of a view if there is one and return its
* ID. If there are multiple of these sorts the first is returned.
*
* @param $view
* The view object.
*
* @return
* The ID of the sort or FALSE if there isn't one.
*/
function entityqueue_get_entityqueue_sort($view) {
foreach ($view->sort as $id => $sort) {
if ($sort->definition['handler'] == 'entityqueue_handler_sort_position') {
return $id;
}
}
return FALSE;
}
/**
* Implements hook_views_api().
*/
function entityqueue_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'entityqueue') . '/includes/views',
);
}
/**
* Implements hook_theme().
*/
function entityqueue_theme() {
return array(
'entityqueue_overview_item' => array(
'variables' => array('label' => NULL, 'name' => FALSE, 'status' => FALSE),
'file' => 'includes/entityqueue.theme.inc',
),
'entityqueue_status' => array(
'variables' => array('status' => NULL, 'html' => TRUE),
'file' => 'includes/entityqueue.theme.inc',
),
);
}
/**
* Returns all queues or subqueues in a way which can be used on form options.
*
* @param array $objects
* (optional) An array of fully loaded objects to display.
* @param string $object_type
* (optional) A string representing what needs to be loaded, queues or
* subqueues. Defaults to 'subqueue';
*
* @return array
* An array of EntityQueue or EntitySubqueue objects, keyed by name.
*/
function entityqueue_get_options($objects = array(), $object_type = 'subqueue') {
if (empty($objects)) {
switch ($object_type) {
case 'subqueue':
$objects = entityqueue_subqueue_load_multiple();
break;
case 'queue':
default:
$objects = entityqueue_queue_load_multiple();
break;
}
}
$options = array();
foreach ($objects as $object) {
$options[$object->name] = $object->label;
}
return $options;
}
/**
* Helper function for getting the name of a entityreference field.
*
* @param string $entity_type
* A Drupal entity type.
*
* @return string
* The name of the entityreference field for the given entity type.
*/
function _entityqueue_get_target_field_name($entity_type) {
if (drupal_strlen($entity_type) <= 29) {
return 'eq_' . $entity_type;
}
else {
// Field names cannot be longer than 32 characters, so have to use a hashing
// trick in order to get a human-readable field name for long entity types.
return 'eq_' . substr($entity_type, 0, 20) . '_' . substr(md5($entity_type), 0, 8);
}
}
/**
* Makes sure that a entityreference field instance exists for a queue.
*
* @param EntityQueue $queue
* An EntityQueue object.
*/
function _entityqueue_queue_ensure_instance(EntityQueue $queue) {
$all_queue_names = variable_get('entityqueue_queue_names', array());
if (!in_array($queue->name, $all_queue_names)) {
$all_queue_names[] = $queue->name;
variable_set('entityqueue_queue_names', $all_queue_names);
}
$field_name = _entityqueue_get_target_field_name($queue->target_type);
$all_eq_fields = variable_get('entityqueue_field_names', array());
if (!in_array($field_name, $all_eq_fields)) {
$all_eq_fields[] = $field_name;
variable_set('entityqueue_field_names', $all_eq_fields);
}
$handler_settings = array(
'behaviors' => array(
'entityqueue' => array(
'status' => 1,
),
),
);
if (!field_info_instance('entityqueue_subqueue', $field_name, $queue->name)) {
_entityqueue_create_entityreference_field($queue, $field_name, 'entityqueue_subqueue', $queue->name, t('Queue items'), 0, array(), $handler_settings);
}
}
/**
* Creates a locked instance of a entityreference field on the specified bundle.
*
* @param EntityQueue $queue
* An EntityQueue object.
* @param string $field_name
* The name of the field; if it already exists, a new instance of the existing
* field will be created.
* @param string $entity_type
* The type of entity the field instance will be attached to.
* @param string $bundle
* The bundle name of the entity the field instance will be attached to.
* @param string $label
* The label of the field instance.
* @param int $weight
* The default weight of the field instance widget and display.
* @param array $display
* An array of default display data used for the entity's current view modes.
* @param array $handler_settings
* An array of Entityrefence field handler settings.
*/
function _entityqueue_create_entityreference_field($queue, $field_name, $entity_type, $bundle, $label, $weight = 0, $display = array(), $handler_settings = array()) {
// If a field type we know should exist isn't found, clear the Field cache.
if (!field_info_field_types('entityreference')) {
field_cache_clear();
}
// Look for or add the specified entityreference field to the requested entity
// bundle.
$field = field_read_field($field_name);
$instance = field_read_instance($entity_type, $field_name, $bundle);
if (empty($field)) {
$field = array(
'field_name' => $field_name,
'type' => 'entityreference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'entity_types' => array($entity_type),
'translatable' => FALSE,
'locked' => TRUE,
'settings' => array(
'target_type' => $queue->target_type,
'handler' => 'entityqueue',
'handler_settings' => $handler_settings,
),
);
field_create_field($field);
}
if (empty($instance)) {
$instance = array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'label' => $label,
'required' => FALSE,
'settings' => array(),
'widget' => array(
'type' => 'entityqueue_dragtable',
'weight' => $weight,
'settings' => array(),
),
'display' => $display,
);
field_create_instance($instance);
}
}
/**
* Workaround for missing breadcrumbs on callback and action paths.
*
* Shamelessly stolen from http://drupal.org/project/xmlsitemap
*
* @todo Remove when http://drupal.org/node/576290 is fixed.
*/
function _entityqueue_set_breadcrumb($path = 'admin/structure/entityqueue') {
$breadcrumb = array();
$path = explode('/', $path);
do {
$menu_path = implode('/', $path);
$menu_item = menu_get_item($menu_path);
array_unshift($breadcrumb, l($menu_item['title'], $menu_path));
} while (array_pop($path) && !empty($path));
array_unshift($breadcrumb, l(t('Home'), NULL));
drupal_set_breadcrumb($breadcrumb);
}