-
Notifications
You must be signed in to change notification settings - Fork 2
/
entity.module
829 lines (772 loc) · 27.6 KB
/
entity.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
<?php
// $Id$
/**
* @file
* Module file for the entity API.
*/
module_load_include('inc', 'entity', 'modules/callbacks');
module_load_include('inc', 'entity', 'includes/entity.property');
/**
* Defines status codes used for exportable entities.
*/
/**
* A bit flag used to let us know if an entity is in the database.
*/
define('ENTITY_IN_DB', 0x01);
/**
* A bit flag used to let us know if an entity is a 'default' in code.
*/
define('ENTITY_IN_CODE', 0x02);
/**
* A bit flag used to mark entities as overridden, e.g. they were originally
* definded in code and are saved now in the database. Same as
* (ENTITY_IN_DB | ENTITY_IN_CODE).
*/
define('ENTITY_OVERRIDDEN', 0x03);
/**
* A bit flag used to mark entities as fixed, thus not changeable for any
* user.
*/
define('ENTITY_FIXED', 0x04);
/**
* Determines whether for the given entity type a given operation is available.
*
* @param $entity_type
* The type of the entity.
* @param $op
* One of 'create', 'view', 'save', 'delete' or 'access.
*/
function entity_type_supports($entity_type, $op) {
$info = entity_get_info($entity_type);
$keys = array(
'view' => 'view callback',
'create' => 'creation callback',
'delete' => 'deletion callback',
'save' => 'save callback',
'access' => 'access callback',
);
if (isset($info[$keys[$op]]) || ($op != 'access' && in_array('EntityAPIControllerInterface', class_implements($info['controller class'])))) {
return TRUE;
}
}
/**
* Permanently save an entity.
*
* In case of failures, an exception is thrown.
*
* @param $entity_type
* The type of the entity.
* @param $entity
* The entity to save.
* @return
* For entity types provided by the CRUD API, SAVED_NEW or SAVED_UPDATED is
* returned depending on the operation performed. If there is no information
* how to save the entity, FALSE is returned.
*
* @see entity_type_supports()
*/
function entity_save($entity_type, &$entity) {
$info = entity_get_info($entity_type);
if (method_exists($entity, 'save')) {
return $entity->save();
}
elseif (isset($info['save callback'])) {
$info['save callback']($entity);
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->save($entity);
}
else {
return FALSE;
}
}
/**
* Permanently delete the given entity.
*
* In case of failures, an exception is thrown.
*
* @param $entity_type
* The type of the entity.
* @param $id
* The uniform identifier of the entity to delete.
*
* @return
* FALSE, if there were no information how to delete the entity.
*
* @see entity_type_supports()
*/
function entity_delete($entity_type, $id) {
$info = entity_get_info($entity_type);
if (isset($info['deletion callback'])) {
$info['deletion callback']($id);
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
entity_get_controller($entity_type)->delete(array($id));
}
else {
return FALSE;
}
}
/**
* Permanently delete multiple entities.
* @todo: Add support for entity types not provided via the CRUD API.
*
* @param $entity_type
* The type of the entity.
* @param $ids
* An array of uniform identifiers of the entities to delete.
* @return
* FALSE if the given entity type isn't compatible to the CRUD API.
*/
function entity_delete_multiple($entity_type, $ids) {
$info = entity_get_info($entity_type);
if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
entity_get_controller($entity_type)->delete($ids);
}
else {
return FALSE;
}
}
/**
* Create a new entity object.
*
* @param $entity_type
* The type of the entity.
* @param $values
* An array of values to set, keyed by property name. If the entity type has
* bundles the bundle key has to be specified.
* @return
* A new instance of the entity type or FALSE if there is no information for
* the given entity type.
*
* @see entity_type_supports()
*/
function entity_create($entity_type, array $values) {
$info = entity_get_info($entity_type);
if (isset($info['creation callback'])) {
return $info['creation callback']($values, $entity_type);
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->create($values);
}
return FALSE;
}
/**
* Exports an entity.
*
* Note: Currently, this only works for entity types provided with the entity
* CRUD API.
*
* @param $entity_type
* The type of the entity.
* @param $entity
* The entity to export.
* @param $prefix
* An optional prefix for each line.
* @return
* The exported entity as serialized string. The format is determined by the
* respective entity controller, e.g. it is JSON for the EntityAPIController.
* The output is suitable for entity_import().
*/
function entity_export($entity_type, $entity, $prefix = '') {
if (method_exists($entity, 'export')) {
return $entity->export($prefix);
}
$info = entity_get_info($entity_type);
if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->export($entity, $prefix);
}
}
/**
* Imports an entity.
*
* Note: Currently, this only works for entity types provided with the entity
* CRUD API.
*
* @param $entity_type
* The type of the entity.
* @param string $export
* The string containing the serialized entity as produced by
* entity_export().
* @return
* The imported entity object not yet saved.
*/
function entity_import($entity_type, $export) {
$info = entity_get_info($entity_type);
if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->import($export);
}
}
/**
* Builds a structured array representing the entity's content.
*
* The content built for the entity will vary depending on the $view_mode
* parameter.
*
* Note: Currently, this only works for entity types provided with the entity
* CRUD API.
*
* @param $entity_type
* The type of the entity.
* @param $entity
* An entity object.
* @param $view_mode
* A view mode as used by this entity type, e.g. 'full', 'teaser'...
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
* @return
* The renderable array.
*/
function entity_build_content($entity_type, $entity, $view_mode = 'full', $langcode = NULL) {
$info = entity_get_info($entity_type);
if (method_exists($entity, 'buildContent')) {
return $entity->buildContent($view_mode, $langcode);
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->buildContent($entity, $view_mode, $langcode);
}
}
/**
* Returns the entity identifier.
*
* Unlike entity_extract_ids() this function returns the uniform identifier,
* thus if an exportable entity makes use of a name key, the name of the entity
* is returned. Otherwise the usual numeric id is returned.
*
* @param $entity_type
* The type of the entity.
* @param $entity
* An entity object.
*
* @see entity_extract_ids()
*/
function entity_id($entity_type, $entity) {
if (method_exists($entity, 'identifier')) {
return $entity->identifier();
}
$info = entity_get_info($entity_type);
$key = isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'];
return isset($entity->$key) ? $entity->$key : NULL;
}
/**
* Generate an array for rendering the given entities.
*
* @param $entity_type
* The type of the entity.
* @param $entities
* An array of entities to render, keyed by their ids. E.g. as returned from
* entity_load().
* @param $view_mode
* A view mode as used by this entity type, e.g. 'full', 'teaser'...
* @param $langcode
* (optional) A language code to use for rendering. Defaults to the global
* content language of the current request.
* @return
* The renderable array, or FALSE if there is no information how to view an
* entity.
*/
function entity_view($entity_type, $entities, $view_mode = 'full', $langcode = NULL) {
$info = entity_get_info($entity_type);
if (isset($info['view callback'])) {
return $info['view callback']($entities, $view_mode, $langcode, $entity_type);
}
elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_get_controller($entity_type)->view($entities, $view_mode, $langcode);
}
}
/**
* Determines whether the given user has access to an entity.
*
* @param $op
* The operation being performed. One of 'view', 'update', 'create' or
* 'delete'.
* @param $entity_type
* The entity type of the entity to check for.
* @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.
*
* @return boolean
* Whether access is allowed or not. If the entity type does not specify any
* access information, NULL is returned.
*
* @see entity_type_supports()
*/
function entity_access($op, $entity_type, $entity = NULL, $account = NULL) {
if (($info = entity_get_info()) && isset($info[$entity_type]['access callback'])) {
return $info[$entity_type]['access callback']($op, $entity, $account, $entity_type);
}
}
/**
* Returns an array of entity info for the entity types provided via the entity CRUD API.
*/
function entity_crud_get_info() {
$types = array();
foreach (entity_get_info() as $type => $info) {
if (isset($info['controller class']) && in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
$types[$type] = $info;
}
}
return $types;
}
/**
* Checks if a given entity has a certain exportable status.
*
* @param $entity_type
* The type of the entity.
* @param $entity
* The entity to check the status on.
* @param $status
* The constant status like ENTITY_IN_DB, ENTITY_IN_CODE, ENTITY_OVERRIDDEN
* or ENTITY_FIXED.
*
* @return
* TRUE if the entity has the status, FALSE otherwise.
*/
function entity_has_status($entity_type, $entity, $status) {
$info = entity_get_info($entity_type);
$status_key = empty($info['entity keys']['status']) ? 'status' : $info['entity keys']['status'];
return isset($entity->{$status_key}) && ($entity->{$status_key} & $status) == $status;
}
/**
* Export a variable. Copied from ctools.
*
* This is a replacement for var_export(), allowing us to more nicely
* format exports. It will recurse down into arrays and will try to
* properly export bools when it can.
*/
function entity_var_export($var, $prefix = '') {
if (is_array($var)) {
if (empty($var)) {
$output = 'array()';
}
else {
$output = "array(\n";
foreach ($var as $key => $value) {
$output .= " '$key' => " . entity_var_export($value, ' ') . ",\n";
}
$output .= ')';
}
}
else if (is_bool($var)) {
$output = $var ? 'TRUE' : 'FALSE';
}
else {
$output = var_export($var, TRUE);
}
if ($prefix) {
$output = str_replace("\n", "\n$prefix", $output);
}
return $output;
}
/**
* Export a variable in pretty formatted JSON.
*/
function entity_var_json_export($var, $prefix = '') {
if (is_array($var) && $var) {
// Defines whether we use a JSON array or object.
$use_array = ($var == array_values($var));
$output = $use_array ? "[" : "{";
foreach ($var as $key => $value) {
if ($use_array) {
$values[] = entity_var_json_export($value, ' ');
}
else {
$values[] = entity_var_json_export((string) $key, ' ') . ' : ' . entity_var_json_export($value, ' ');
}
}
// Use several lines for long content. However for objects with a single
// entry keep the key in the first line.
if (strlen($content = implode(", ", $values)) > 70 && ($use_array || count($values) > 1)) {
$output .= "\n " . implode(",\n ", $values) . "\n";
}
elseif (strpos($content, "\n") !== FALSE) {
$output .= " " . $content . "\n";
}
else {
$output .= " " . $content . " ";
}
$output .= $use_array ? "]" : "}";
}
else {
$output = drupal_json_encode($var);
}
if ($prefix) {
$output = str_replace("\n", "\n$prefix", $output);
}
return $output;
}
/**
* Implements hook_modules_enabled().
*
* Invokes hook_entity_enabled() for new default entities.
*/
function entity_modules_enabled($modules) {
foreach (entity_crud_get_info() as $entity_type => $info) {
if (!empty($info['exportable']) && $entities = _entity_modules_get_defaults($entity_type, $modules)) {
module_invoke_all($entity_type . '_enabled', $entities);
module_invoke_all('entity_enabled', $entities, $entity_type);
}
}
}
/**
* Implements hook_modules_disabled().
*
* Invokes hook_entity_disabled() for new default entities.
*/
function entity_modules_disabled($modules) {
foreach (entity_crud_get_info() as $entity_type => $info) {
if (!empty($info['exportable']) && $entities = _entity_modules_get_defaults($entity_type, $modules)) {
module_invoke_all($entity_type . '_disabled', $entities);
module_invoke_all('entity_disabled', $entities, $entity_type);
// Make sure the cache is cleared afterwards, so removed default entities
// are not left there.
entity_get_controller($entity_type)->resetCache();
}
}
}
function _entity_modules_get_defaults($entity_type, $modules) {
$info = entity_get_info($entity_type);
$entities = array();
$hook = isset($info['export']['default hook']) ? $info['export']['default hook'] : 'default_' . $entity_type;
$module_key = isset($info['entity keys']['module']) ? $info['entity keys']['module'] : 'module';
// Collect the ids of all new entities.
foreach ($modules as $module) {
if (module_hook($module, $hook)) {
$result = module_invoke($module, $hook);
if ($result && is_array($result)) {
$entities += $result;
}
}
}
if ($ids = array_keys($entities)) {
// To make sure the new default entities are retrieved, make sure the cache
// is cleared.
entity_get_controller($entity_type)->resetCache();
// Check whether some of the new entities are already overridden.
$entities = array();
foreach (entity_load($entity_type, $ids) as $id => $entity) {
if (!entity_has_status($entity_type, $entity, ENTITY_OVERRIDDEN)) {
$entities[$id] = $entity;
}
}
return $entities;
}
}
/**
* Implements hook_theme().
*/
function entity_theme() {
return array(
'entity_status' => array(
'variables' => array('status' => NULL, 'html' => TRUE),
),
'entity' => array(
'render element' => 'elements',
'template' => 'entity',
),
'entity_ui_overview_item' => array(
'variables' => array('label' => NULL, 'entity_type' => NULL, 'url' => FALSE, 'name' => FALSE),
'file' => 'includes/entity.ui.inc'
),
);
}
/**
* Themes the exportable status of an entity.
*/
function theme_entity_status($variables) {
$status = $variables['status'];
$html = $variables['html'];
if ($status & ENTITY_FIXED) {
$label = t('Fixed');
$help = t('The configuration is fixed and cannot be changed.');
return $html ? "<span class='entity-status-fixed' title='$help'>" . $label . "</span>" : $label;
}
elseif (($status & ENTITY_OVERRIDDEN) == ENTITY_OVERRIDDEN) {
$label = t('Overridden');
$help = t('This configuration is provided by a module, but has been changed.');
return $html ? "<span class='entity-status-overridden' title='$help'>" . $label . "</span>" : $label;
}
elseif ($status & ENTITY_IN_CODE) {
$label = t('Default');
$help = t('A module provides this configuration.');
return $html ? "<span class='entity-status-default' title='$help'>" . $label . "</span>" : $label;
}
elseif ($status & ENTITY_IN_DB) {
$label = t('Custom');
$help = t('A custom configuration by a user.');
return $html ? "<span class='entity-status-custom' title='$help'>" . $label . "</span>" : $label;
}
}
/**
* Process variables for entity.tpl.php.
*/
function template_preprocess_entity(&$variables) {
$variables['view_mode'] = $variables['elements']['#view_mode'];
$entity_type = $variables['elements']['#entity_type'];
$variables['entity_type'] = $entity_type;
$entity = $variables['elements']['#entity'];
$variables[$variables['elements']['#entity_type']] = $entity;
$info = entity_get_info($entity_type);
$variables['title'] = check_plain(entity_label($entity_type, $entity));
$uri = entity_uri($entity_type, $entity);
$variables['url'] = $uri ? url($uri['path'], $uri['options']) : FALSE;
$variables['page'] = $uri && $uri['path'] == $_GET['q'];
// Helpful $content variable for templates.
$variables['content'] = array();
foreach (element_children($variables['elements']) as $key) {
$variables['content'][$key] = $variables['elements'][$key];
}
if (!empty($info['fieldable'])) {
// Make the field variables available with the appropriate language.
field_attach_preprocess($entity_type, $entity, $variables['content'], $variables);
}
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
// Gather css classes.
$variables['classes_array'][] = drupal_html_class('entity-' . $entity_type);
$variables['classes_array'][] = drupal_html_class($entity_type . '-' . $bundle);
// Add suggestions.
$variables['theme_hook_suggestions'][] = $entity_type;
$variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle;
if ($id = entity_id($entity_type, $entity)) {
$variables['theme_hook_suggestions'][] = $entity_type . '__' . $id;
}
}
/**
* Label callback that refers to the entity classes label method.
*/
function entity_class_label($entity) {
return $entity->label();
}
/**
* URI callback that refers to the entity classes uri method.
*/
function entity_class_uri($entity) {
return $entity->uri();
}
/**
* Implements hook_file_download_access() for entity types provided by the CRUD API.
*/
function entity_file_download_access($field, $entity_type, $entity) {
$info = entity_get_info($entity_type);
if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
return entity_access('view', $entity_type, $entity);
}
}
/**
* Determines the UI controller class for a given entity type.
*
* @return EntityDefaultUIController
* If a type is given, the controller for the given entity type. Else an array
* of all enabled UI controllers keyed by entity type is returned.
*/
function entity_ui_controller($type = NULL) {
$static = &drupal_static(__FUNCTION__);
$all = &drupal_static(__FUNCTION__);
if (isset($type) && !isset($static[$type])) {
$info = entity_get_info($type);
$class = isset($info['admin ui']['controller class']) ? $info['admin ui']['controller class'] : 'EntityDefaultUIController';
$static[$type] = (isset($info['admin ui']['path']) && $class) ? new $class($type, $info) : FALSE;
}
if (!isset($type) && !isset($all)) {
// Invoke the function for each type to ensure we have fully populated the
// static variable $static.
foreach (entity_get_info() as $entity_type => $info) {
$all[$entity_type] = entity_ui_controller($entity_type);
}
}
if ($type) {
return isset($static[$type]) ? $static[$type] : FALSE;
}
return array_filter($static);
}
/**
* Implements hook_menu().
*
* @see EntityDefaultUIController::hook_menu()
*/
function entity_menu() {
$items = array();
foreach (entity_ui_controller() as $controller) {
$items += $controller->hook_menu();
}
return $items;
}
/**
* Implements hook_forms().
*
* @see EntityDefaultUIController::hook_forms()
* @see entity_ui_get_form()
*/
function entity_forms($form_id, $args) {
// For efficiency only invoke an entity types controller, if a form of it is
// requested. Thus if the first (overview and operation form) or the third
// argument (edit form) is an entity type name, add in the types forms.
if (isset($args[0]) && is_string($args[0]) && entity_get_info($args[0])) {
$type = $args[0];
}
elseif (isset($args[2]) && is_string($args[2]) && entity_get_info($args[2])) {
$type = $args[2];
}
if (isset($type) && $controller = entity_ui_controller($type)) {
return $controller->hook_forms();
}
}
/**
* Gets an entity form.
*
* This function may be used by entities to build their entity form. It has to
* be used instead of calling drupal_get_form().
* Entity forms built with this helper receive useful defaults suiting for
* editing a single entity, whereas the special cases of adding and cloning
* of entities are supported too.
*
* While this function is intended to be used to get entity forms for entities
* using the entity ui controller, it may be used for entity types not using
* the ui controller too.
*
* @param $entity_type
* The entity type for which to get the form.
* @param $entity
* The entity for which to return the form.
* If $op is 'add' the entity has to be either initialized before calling this
* function, or NULL may be passed. If NULL is passed, an entity will be
* initialized with empty values using entity_create(). Thus entities, for
* which this is problematic have to care to pass in an initialized entity.
* @param $op
* One of 'edit', 'add' or 'clone'. Defaults to edit.
*
* @return
* The fully built and processed form, ready to be rendered.
*
* @see EntityDefaultUIController::hook_forms()
* @see entity_ui_form_submit_build_entity()
*/
function entity_ui_get_form($entity_type, $entity, $op = 'edit') {
if (isset($entity)) {
list(, , $bundle) = entity_extract_ids($entity_type, $entity);
}
$form_id = (!isset($bundle) || $bundle == $entity_type) ? $entity_type . '_form' : $entity_type . '_edit_' . $bundle . '_form';
if (!isset($entity) && $op == 'add') {
$entity = entity_create($entity_type, array());
}
// Do not use drupal_get_form(), but invoke drupal_build_form() ourself so
// we can prepulate the form state.
$form_state['wrapper_callback'] = 'entity_ui_main_form_defaults';
$form_state['entity_type'] = $entity_type;
// We don't pass the entity type as first parameter, as the implementing
// module knows the type anyway. However, in order to allow for efficient
// hook_forms() implementiations we append the entity type as last argument,
// which the module implementing the form constructor may safely ignore.
// @see entity_forms()
$form_state['build_info']['args'] = array($entity, $op, $entity_type);
module_load_include('inc', 'entity', 'includes/entity.ui');
return drupal_build_form($form_id, $form_state);
}
/**
* Returns a property wrapper for the given data.
*
* If an entity is wrapped, the wrapper can be used to retrieve further wrappers
* for the entitity properties. For that the wrapper support chaining, e.g. you
* can use a node wrapper to get the node authors mail address:
*
* @code
* echo $wrappedNode->author->mail->value();
* @endcode
*
* @param $type
* The type of the passed data.
* @param $data
* The data to wrap. It may be set to NULL, so the wrapper can be used
* without any data for getting information about properties.
* @param $info
* (optional) Specify additional information for the passed data:
* - langcode: (optional) If the data is language specific, its langauge
* code. Defaults to NULL, what means language neutral.
* - bundle: (optional) If an entity is wrapped but not passed, use this key
* to specify the bundle to return a wrapper for.
* - property info: (optional) May be used to use a wrapper with an arbitrary
* data structure (type 'struct'). Use this key for specifying info about
* properties in the same structure as used by hook_entity_property_info().
* - property info alter: (optional) A callback for altering the property
* info before it is utilized by the wrapper.
* - property defaults: (optional) An array of defaults for the info of
* each property of the wrapped data item.
* @return EntityMetadataWrapper
* Dependend on the passed data the right wrapper is returned.
*/
function entity_metadata_wrapper($type, $data = NULL, array $info = array()) {
if (($entity_info = entity_get_info()) && isset($entity_info[$type])) {
return new EntityDrupalWrapper($type, $data, $info);
}
elseif ($type == 'list' || entity_property_list_extract_type($type)) {
return new EntityListWrapper($type, $data, $info);
}
elseif (isset($info['property info'])) {
return new EntityStructureWrapper($type, $data, $info);
}
else {
return new EntityValueWrapper($type, $data, $info);
}
}
/**
* Returns a metadata wrapper for accessing site-wide properties.
*
* Although there is no 'site' entity or such, modules may provide info about
* site-wide properties using hook_entity_property_info(). This function returns
* a wrapper for making use of this properties.
*
* @return EntityMetadataWrapper
* A wrapper for accessing site-wide properties.
*
* @see entity_metadata_system_entity_property_info()
*/
function entity_metadata_site_wrapper() {
$site_info = entity_get_property_info('site');
$info['property info'] = $site_info['properties'];
return entity_metadata_wrapper('site', FALSE, $info);
}
/**
* Implements hook_entity_info_alter().
*/
function entity_entity_info_alter(&$entity_info) {
// Set access callbacks.
$entity_info['node']['access callback'] = 'entity_metadata_no_hook_node_access';
$entity_info['user']['access callback'] = 'entity_metadata_user_access';
// CRUD function callbacks.
$entity_info['node']['creation callback'] = 'entity_metadata_create_node';
$entity_info['node']['save callback'] = 'node_save';
$entity_info['node']['deletion callback'] = 'node_delete';
$entity_info['user']['creation callback'] = 'entity_metadata_create_object';
$entity_info['user']['save callback'] = 'entity_metadata_user_save';
$entity_info['user']['deletion callback'] = 'user_delete';
$entity_info['file']['save callback'] = 'file_save';
$entity_info['file']['deletion callback'] = 'entity_metadata_delete_file';
// View callbacks.
$entity_info['node']['view callback'] = 'entity_metadata_view_node';
$entity_info['user']['view callback'] = 'entity_metadata_view_single';
if (module_exists('comment')) {
$entity_info['comment']['access callback'] = 'entity_metadata_comment_access';
$entity_info['comment']['creation callback'] = 'entity_metadata_create_comment';
$entity_info['comment']['save callback'] = 'comment_save';
$entity_info['comment']['deletion callback'] = 'comment_delete';
$entity_info['comment']['view callback'] = 'entity_metadata_view_single';
}
if (module_exists('taxonomy')) {
$entity_info['taxonomy_term']['access callback'] = 'entity_metadata_taxonomy_access';
$entity_info['taxonomy_term']['creation callback'] = 'entity_metadata_create_object';
$entity_info['taxonomy_term']['save callback'] = 'taxonomy_term_save';
$entity_info['taxonomy_term']['deletion callback'] = 'taxonomy_term_delete';
$entity_info['taxonomy_term']['view callback'] = 'entity_metadata_view_single';
$entity_info['taxonomy_vocabulary']['access callback'] = 'entity_metadata_taxonomy_access';
$entity_info['taxonomy_vocabulary']['creation callback'] = 'entity_metadata_create_object';
$entity_info['taxonomy_vocabulary']['save callback'] = 'taxonomy_vocabulary_save';
$entity_info['taxonomy_vocabulary']['deletion callback'] = 'taxonomy_vocabulary_delete';
// Token type mapping.
$entity_info['taxonomy_term']['token type'] = 'term';
$entity_info['taxonomy_vocabulary']['token type'] = 'vocabulary';
}
}