comparison sites/all/modules/entity/entity.module @ 4:ce11bbd8f642

added modules
author danieleb <danielebarchiesi@me.com>
date Thu, 19 Sep 2013 10:38:44 +0100
parents
children
comparison
equal deleted inserted replaced
3:b28be78d8160 4:ce11bbd8f642
1 <?php
2
3 /**
4 * @file
5 * Module file for the entity API.
6 */
7
8 module_load_include('inc', 'entity', 'modules/callbacks');
9 module_load_include('inc', 'entity', 'includes/entity.property');
10
11
12 /**
13 * Defines status codes used for exportable entities.
14 */
15
16 /**
17 * A bit flag used to let us know if an entity is in the database.
18 */
19
20
21 /**
22 * A bit flag used to let us know if an entity has been customly defined.
23 */
24 define('ENTITY_CUSTOM', 0x01);
25
26 /**
27 * Deprecated, but still here for backward compatibility.
28 */
29 define('ENTITY_IN_DB', 0x01);
30
31 /**
32 * A bit flag used to let us know if an entity is a 'default' in code.
33 */
34 define('ENTITY_IN_CODE', 0x02);
35
36 /**
37 * A bit flag used to mark entities as overridden, e.g. they were originally
38 * definded in code and are saved now in the database. Same as
39 * (ENTITY_CUSTOM | ENTITY_IN_CODE).
40 */
41 define('ENTITY_OVERRIDDEN', 0x03);
42
43 /**
44 * A bit flag used to mark entities as fixed, thus not changeable for any
45 * user.
46 */
47 define('ENTITY_FIXED', 0x04 | 0x02);
48
49
50
51 /**
52 * Determines whether for the given entity type a given operation is available.
53 *
54 * @param $entity_type
55 * The type of the entity.
56 * @param $op
57 * One of 'create', 'view', 'save', 'delete', 'revision delete', 'access' or
58 * 'form'.
59 *
60 * @return boolean
61 * Whether the entity type supports the given operation.
62 */
63 function entity_type_supports($entity_type, $op) {
64 $info = entity_get_info($entity_type);
65 $keys = array(
66 'view' => 'view callback',
67 'create' => 'creation callback',
68 'delete' => 'deletion callback',
69 'revision delete' => 'revision deletion callback',
70 'save' => 'save callback',
71 'access' => 'access callback',
72 'form' => 'form callback'
73 );
74 if (isset($info[$keys[$op]])) {
75 return TRUE;
76 }
77 if ($op == 'revision delete') {
78 return in_array('EntityAPIControllerInterface', class_implements($info['controller class']));
79 }
80 if ($op == 'form') {
81 return (bool) entity_ui_controller($entity_type);
82 }
83 if ($op != 'access') {
84 return in_array('EntityAPIControllerInterface', class_implements($info['controller class']));
85 }
86 return FALSE;
87 }
88
89 /**
90 * Menu loader function: load an entity from its path.
91 *
92 * This can be used to load entities of all types in menu paths:
93 *
94 * @code
95 * $items['myentity/%entity_object'] = array(
96 * 'load arguments' => array('myentity'),
97 * 'title' => ...,
98 * 'page callback' => ...,
99 * 'page arguments' => array(...),
100 * 'access arguments' => array(...),
101 * );
102 * @endcode
103 *
104 * @param $entity_id
105 * The ID of the entity to load, passed by the menu URL.
106 * @param $entity_type
107 * The type of the entity to load.
108 * @return
109 * A fully loaded entity object, or FALSE in case of error.
110 */
111 function entity_object_load($entity_id, $entity_type) {
112 $entities = entity_load($entity_type, array($entity_id));
113 return reset($entities);
114 }
115
116 /**
117 * Page callback to show links to add an entity of a specific bundle.
118 *
119 * Entity modules that provide a further description to their bundles may wish
120 * to implement their own version of this to show these.
121 *
122 * @param $entity_type
123 * The type of the entity.
124 */
125 function entity_ui_bundle_add_page($entity_type) {
126 // Set the title, as we're a MENU_LOCAL_ACTION and hence just get tab titles.
127 module_load_include('inc', 'entity', 'includes/entity.ui');
128 drupal_set_title(entity_ui_get_action_title('add', $entity_type));
129
130 // Get entity info for our bundles.
131 $info = entity_get_info($entity_type);
132 $items = array();
133 foreach ($info['bundles'] as $bundle_name => $bundle_info) {
134 // Create an empty entity with just the bundle set to check for access.
135 $dummy_entity = entity_create($entity_type, array(
136 'bundle' => $bundle_name,
137 ));
138 // If modules use a uid, they can default to the current-user
139 // in their create() method on the storage controller.
140 if (entity_access('create', $entity_type, $dummy_entity, $account = NULL)) {
141 $add_path = $info['admin ui']['path'] . '/add/' . $bundle_name;
142 $items[] = l(t('Add @label', array('@label' => $bundle_info['label'])), $add_path);
143 }
144 }
145 return theme('item_list', array('items' => $items));
146 }
147
148 /**
149 * Page callback to add an entity of a specific bundle.
150 *
151 * @param $entity_type
152 * The type of the entity.
153 * @param $bundle_name
154 * The bundle machine name.
155 */
156 function entity_ui_get_bundle_add_form($entity_type, $bundle_name) {
157 $info = entity_get_info($entity_type);
158 $bundle_key = $info['entity keys']['bundle'];
159
160 // Make a stub entity of the right bundle to pass to the entity_ui_get_form().
161 $values = array(
162 $bundle_key => $bundle_name,
163 );
164 $entity = entity_create($entity_type, $values);
165
166 return entity_ui_get_form($entity_type, $entity, 'add');
167 }
168
169 /**
170 * A wrapper around entity_load() to load a single entity by name or numeric id.
171 *
172 * @todo: Re-name entity_load() to entity_load_multiple() in d8 core and this
173 * to entity_load().
174 *
175 * @param $entity_type
176 * The entity type to load, e.g. node or user.
177 * @param $id
178 * The entity id, either the numeric id or the entity name. In case the entity
179 * type has specified a name key, both the numeric id and the name may be
180 * passed.
181 *
182 * @return
183 * The entity object, or FALSE.
184 *
185 * @see entity_load()
186 */
187 function entity_load_single($entity_type, $id) {
188 $entities = entity_load($entity_type, array($id));
189 return reset($entities);
190 }
191
192 /**
193 * A wrapper around entity_load() to return entities keyed by name key if existing.
194 *
195 * @param $entity_type
196 * The entity type to load, e.g. node or user.
197 * @param $names
198 * An array of entity names or ids, or FALSE to load all entities.
199 * @param $conditions
200 * (deprecated) An associative array of conditions on the base table, where
201 * the keys are the database fields and the values are the values those
202 * fields must have. Instead, it is preferable to use EntityFieldQuery to
203 * retrieve a list of entity IDs loadable by this function.
204 *
205 * @return
206 * An array of entity objects indexed by their names (or ids if the entity
207 * type has no name key).
208 *
209 * @see entity_load()
210 */
211 function entity_load_multiple_by_name($entity_type, $names = FALSE, $conditions = array()) {
212 $entities = entity_load($entity_type, $names, $conditions);
213 $info = entity_get_info($entity_type);
214 if (!isset($info['entity keys']['name'])) {
215 return $entities;
216 }
217 return entity_key_array_by_property($entities, $info['entity keys']['name']);
218 }
219
220 /**
221 * Permanently save an entity.
222 *
223 * In case of failures, an exception is thrown.
224 *
225 * @param $entity_type
226 * The type of the entity.
227 * @param $entity
228 * The entity to save.
229 *
230 * @return
231 * For entity types provided by the CRUD API, SAVED_NEW or SAVED_UPDATED is
232 * returned depending on the operation performed. If there is no information
233 * how to save the entity, FALSE is returned.
234 *
235 * @see entity_type_supports()
236 */
237 function entity_save($entity_type, $entity) {
238 $info = entity_get_info($entity_type);
239 if (method_exists($entity, 'save')) {
240 return $entity->save();
241 }
242 elseif (isset($info['save callback'])) {
243 $info['save callback']($entity);
244 }
245 elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
246 return entity_get_controller($entity_type)->save($entity);
247 }
248 else {
249 return FALSE;
250 }
251 }
252
253 /**
254 * Permanently delete the given entity.
255 *
256 * In case of failures, an exception is thrown.
257 *
258 * @param $entity_type
259 * The type of the entity.
260 * @param $id
261 * The uniform identifier of the entity to delete.
262 *
263 * @return
264 * FALSE, if there were no information how to delete the entity.
265 *
266 * @see entity_type_supports()
267 */
268 function entity_delete($entity_type, $id) {
269 return entity_delete_multiple($entity_type, array($id));
270 }
271
272 /**
273 * Permanently delete multiple entities.
274 *
275 * @param $entity_type
276 * The type of the entity.
277 * @param $ids
278 * An array of entity ids of the entities to delete. In case the entity makes
279 * use of a name key, both the names or numeric ids may be passed.
280 * @return
281 * FALSE if the given entity type isn't compatible to the CRUD API.
282 */
283 function entity_delete_multiple($entity_type, $ids) {
284 $info = entity_get_info($entity_type);
285 if (isset($info['deletion callback'])) {
286 foreach ($ids as $id) {
287 $info['deletion callback']($id);
288 }
289 }
290 elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
291 entity_get_controller($entity_type)->delete($ids);
292 }
293 else {
294 return FALSE;
295 }
296 }
297
298 /**
299 * Loads an entity revision.
300 *
301 * @param $entity_type
302 * The type of the entity.
303 * @param $revision_id
304 * The id of the revision to load.
305 *
306 * @return
307 * The entity object, or FALSE if there is no entity with the given revision
308 * id.
309 */
310 function entity_revision_load($entity_type, $revision_id) {
311 $info = entity_get_info($entity_type);
312 if (!empty($info['entity keys']['revision'])) {
313 $entity_revisions = entity_load($entity_type, FALSE, array($info['entity keys']['revision'] => $revision_id));
314 return reset($entity_revisions);
315 }
316 return FALSE;
317 }
318
319 /**
320 * Deletes an entity revision.
321 *
322 * @param $entity_type
323 * The type of the entity.
324 * @param $revision_id
325 * The revision ID to delete.
326 *
327 * @return
328 * TRUE if the entity revision could be deleted, FALSE otherwise.
329 */
330 function entity_revision_delete($entity_type, $revision_id) {
331 $info = entity_get_info($entity_type);
332 if (isset($info['revision deletion callback'])) {
333 return $info['revision deletion callback']($revision_id, $entity_type);
334 }
335 elseif (in_array('EntityAPIControllerRevisionableInterface', class_implements($info['controller class']))) {
336 return entity_get_controller($entity_type)->deleteRevision($revision_id);
337 }
338 return FALSE;
339 }
340
341 /**
342 * Checks whether the given entity is the default revision.
343 *
344 * Note that newly created entities will always be created in default revision,
345 * thus TRUE is returned for not yet saved entities.
346 *
347 * @param $entity_type
348 * The type of the entity.
349 * @param $entity
350 * The entity object to check.
351 *
352 * @return boolean
353 * A boolean indicating whether the entity is in default revision is returned.
354 * If the entity is not revisionable or is new, TRUE is returned.
355 *
356 * @see entity_revision_set_default()
357 */
358 function entity_revision_is_default($entity_type, $entity) {
359 $info = entity_get_info($entity_type);
360 if (empty($info['entity keys']['revision'])) {
361 return TRUE;
362 }
363 // Newly created entities will always be created in default revision.
364 if (!empty($entity->is_new) || empty($entity->{$info['entity keys']['id']})) {
365 return TRUE;
366 }
367 if (in_array('EntityAPIControllerRevisionableInterface', class_implements($info['controller class']))) {
368 $key = !empty($info['entity keys']['default revision']) ? $info['entity keys']['default revision'] : 'default_revision';
369 return !empty($entity->$key);
370 }
371 else {
372 // Else, just load the default entity and compare the ID. Usually, the
373 // entity should be already statically cached anyway.
374 $default = entity_load_single($entity_type, $entity->{$info['entity keys']['id']});
375 return $default->{$info['entity keys']['revision']} == $entity->{$info['entity keys']['revision']};
376 }
377 }
378
379 /**
380 * Sets a given entity revision as default revision.
381 *
382 * Note that the default revision flag will only be supported by entity types
383 * based upon the EntityAPIController, i.e. implementing the
384 * EntityAPIControllerRevisionableInterface.
385 *
386 * @param $entity_type
387 * The type of the entity.
388 * @param $entity
389 * The entity revision to update.
390 *
391 * @see entity_revision_is_default()
392 */
393 function entity_revision_set_default($entity_type, $entity) {
394 $info = entity_get_info($entity_type);
395 if (!empty($info['entity keys']['revision'])) {
396 $key = !empty($info['entity keys']['default revision']) ? $info['entity keys']['default revision'] : 'default_revision';
397 $entity->$key = TRUE;
398 }
399 }
400
401 /**
402 * Create a new entity object.
403 *
404 * @param $entity_type
405 * The type of the entity.
406 * @param $values
407 * An array of values to set, keyed by property name. If the entity type has
408 * bundles the bundle key has to be specified.
409 * @return
410 * A new instance of the entity type or FALSE if there is no information for
411 * the given entity type.
412 *
413 * @see entity_type_supports()
414 */
415 function entity_create($entity_type, array $values) {
416 $info = entity_get_info($entity_type);
417 if (isset($info['creation callback'])) {
418 return $info['creation callback']($values, $entity_type);
419 }
420 elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
421 return entity_get_controller($entity_type)->create($values);
422 }
423 return FALSE;
424 }
425
426 /**
427 * Exports an entity.
428 *
429 * Note: Currently, this only works for entity types provided with the entity
430 * CRUD API.
431 *
432 * @param $entity_type
433 * The type of the entity.
434 * @param $entity
435 * The entity to export.
436 * @param $prefix
437 * An optional prefix for each line.
438 * @return
439 * The exported entity as serialized string. The format is determined by the
440 * respective entity controller, e.g. it is JSON for the EntityAPIController.
441 * The output is suitable for entity_import().
442 */
443 function entity_export($entity_type, $entity, $prefix = '') {
444 if (method_exists($entity, 'export')) {
445 return $entity->export($prefix);
446 }
447 $info = entity_get_info($entity_type);
448 if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
449 return entity_get_controller($entity_type)->export($entity, $prefix);
450 }
451 }
452
453 /**
454 * Imports an entity.
455 *
456 * Note: Currently, this only works for entity types provided with the entity
457 * CRUD API.
458 *
459 * @param $entity_type
460 * The type of the entity.
461 * @param string $export
462 * The string containing the serialized entity as produced by
463 * entity_export().
464 * @return
465 * The imported entity object not yet saved.
466 */
467 function entity_import($entity_type, $export) {
468 $info = entity_get_info($entity_type);
469 if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
470 return entity_get_controller($entity_type)->import($export);
471 }
472 }
473
474 /**
475 * Checks whether an entity type is fieldable.
476 *
477 * @param $entity_type
478 * The type of the entity.
479 *
480 * @return
481 * TRUE if the entity type is fieldable, FALSE otherwise.
482 */
483 function entity_type_is_fieldable($entity_type) {
484 $info = entity_get_info($entity_type);
485 return !empty($info['fieldable']);
486 }
487
488 /**
489 * Builds a structured array representing the entity's content.
490 *
491 * The content built for the entity will vary depending on the $view_mode
492 * parameter.
493 *
494 * Note: Currently, this only works for entity types provided with the entity
495 * CRUD API.
496 *
497 * @param $entity_type
498 * The type of the entity.
499 * @param $entity
500 * An entity object.
501 * @param $view_mode
502 * A view mode as used by this entity type, e.g. 'full', 'teaser'...
503 * @param $langcode
504 * (optional) A language code to use for rendering. Defaults to the global
505 * content language of the current request.
506 * @return
507 * The renderable array.
508 */
509 function entity_build_content($entity_type, $entity, $view_mode = 'full', $langcode = NULL) {
510 $info = entity_get_info($entity_type);
511 if (method_exists($entity, 'buildContent')) {
512 return $entity->buildContent($view_mode, $langcode);
513 }
514 elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
515 return entity_get_controller($entity_type)->buildContent($entity, $view_mode, $langcode);
516 }
517 }
518
519 /**
520 * Returns the entity identifier, i.e. the entities name or numeric id.
521 *
522 * Unlike entity_extract_ids() this function returns the name of the entity
523 * instead of the numeric id, in case the entity type has specified a name key.
524 *
525 * @param $entity_type
526 * The type of the entity.
527 * @param $entity
528 * An entity object.
529 *
530 * @see entity_extract_ids()
531 */
532 function entity_id($entity_type, $entity) {
533 if (method_exists($entity, 'identifier')) {
534 return $entity->identifier();
535 }
536 $info = entity_get_info($entity_type);
537 $key = isset($info['entity keys']['name']) ? $info['entity keys']['name'] : $info['entity keys']['id'];
538 return isset($entity->$key) ? $entity->$key : NULL;
539 }
540
541 /**
542 * Generate an array for rendering the given entities.
543 *
544 * Entities being viewed, are generally expected to be fully-loaded entity
545 * objects, thus have their name or id key set. However, it is possible to
546 * view a single entity without any id, e.g. for generating a preview during
547 * creation.
548 *
549 * @param $entity_type
550 * The type of the entity.
551 * @param $entities
552 * An array of entities to render.
553 * @param $view_mode
554 * A view mode as used by this entity type, e.g. 'full', 'teaser'...
555 * @param $langcode
556 * (optional) A language code to use for rendering. Defaults to the global
557 * content language of the current request.
558 * @param $page
559 * (optional) If set will control if the entity is rendered: if TRUE
560 * the entity will be rendered without its title, so that it can be embeded
561 * in another context. If FALSE the entity will be displayed with its title
562 * in a mode suitable for lists.
563 * If unset, the page mode will be enabled if the current path is the URI
564 * of the entity, as returned by entity_uri().
565 * This parameter is only supported for entities which controller is a
566 * EntityAPIControllerInterface.
567 * @return
568 * The renderable array, keyed by the entity type and by entity identifiers,
569 * for which the entity name is used if existing - see entity_id(). If there
570 * is no information on how to view an entity, FALSE is returned.
571 */
572 function entity_view($entity_type, $entities, $view_mode = 'full', $langcode = NULL, $page = NULL) {
573 $info = entity_get_info($entity_type);
574 if (isset($info['view callback'])) {
575 $entities = entity_key_array_by_property($entities, $info['entity keys']['id']);
576 return $info['view callback']($entities, $view_mode, $langcode, $entity_type);
577 }
578 elseif (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
579 return entity_get_controller($entity_type)->view($entities, $view_mode, $langcode, $page);
580 }
581 return FALSE;
582 }
583
584 /**
585 * Determines whether the given user has access to an entity.
586 *
587 * @param $op
588 * The operation being performed. One of 'view', 'update', 'create' or
589 * 'delete'.
590 * @param $entity_type
591 * The entity type of the entity to check for.
592 * @param $entity
593 * Optionally an entity to check access for. If no entity is given, it will be
594 * determined whether access is allowed for all entities of the given type.
595 * @param $account
596 * The user to check for. Leave it to NULL to check for the global user.
597 *
598 * @return boolean
599 * Whether access is allowed or not. If the entity type does not specify any
600 * access information, NULL is returned.
601 *
602 * @see entity_type_supports()
603 */
604 function entity_access($op, $entity_type, $entity = NULL, $account = NULL) {
605 if (($info = entity_get_info()) && isset($info[$entity_type]['access callback'])) {
606 return $info[$entity_type]['access callback']($op, $entity, $account, $entity_type);
607 }
608 }
609
610 /**
611 * Gets the edit form for any entity.
612 *
613 * This helper makes use of drupal_get_form() and the regular form builder
614 * function of the entity type to retrieve and process the form as usual.
615 *
616 * In order to use this helper to show an entity add form, the new entity object
617 * can be created via entity_create() or entity_property_values_create_entity().
618 *
619 * @param $entity_type
620 * The type of the entity.
621 * @param $entity
622 * The entity to show the edit form for.
623 * @return
624 * The renderable array of the form. If there is no entity form or missing
625 * metadata, FALSE is returned.
626 *
627 * @see entity_type_supports()
628 */
629 function entity_form($entity_type, $entity) {
630 $info = entity_get_info($entity_type);
631 if (isset($info['form callback'])) {
632 return $info['form callback']($entity, $entity_type);
633 }
634 // If there is an UI controller, the providing module has to implement the
635 // entity form using entity_ui_get_form().
636 elseif (entity_ui_controller($entity_type)) {
637 return entity_metadata_form_entity_ui($entity, $entity_type);
638 }
639 return FALSE;
640 }
641
642 /**
643 * Converts an array of entities to be keyed by the values of a given property.
644 *
645 * @param array $entities
646 * The array of entities to convert.
647 * @param $property
648 * The name of entity property, by which the array should be keyed. To get
649 * reasonable results, the property has to have unique values.
650 *
651 * @return array
652 * The same entities in the same order, but keyed by their $property values.
653 */
654 function entity_key_array_by_property(array $entities, $property) {
655 $ret = array();
656 foreach ($entities as $entity) {
657 $key = isset($entity->$property) ? $entity->$property : NULL;
658 $ret[$key] = $entity;
659 }
660 return $ret;
661 }
662
663 /**
664 * Get the entity info for the entity types provided via the entity CRUD API.
665 *
666 * @return
667 * An array in the same format as entity_get_info(), containing the entities
668 * whose controller class implements the EntityAPIControllerInterface.
669 */
670 function entity_crud_get_info() {
671 $types = array();
672 foreach (entity_get_info() as $type => $info) {
673 if (isset($info['controller class']) && in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
674 $types[$type] = $info;
675 }
676 }
677 return $types;
678 }
679
680 /**
681 * Checks if a given entity has a certain exportable status.
682 *
683 * @param $entity_type
684 * The type of the entity.
685 * @param $entity
686 * The entity to check the status on.
687 * @param $status
688 * The constant status like ENTITY_CUSTOM, ENTITY_IN_CODE, ENTITY_OVERRIDDEN
689 * or ENTITY_FIXED.
690 *
691 * @return
692 * TRUE if the entity has the status, FALSE otherwise.
693 */
694 function entity_has_status($entity_type, $entity, $status) {
695 $info = entity_get_info($entity_type);
696 $status_key = empty($info['entity keys']['status']) ? 'status' : $info['entity keys']['status'];
697 return isset($entity->{$status_key}) && ($entity->{$status_key} & $status) == $status;
698 }
699
700 /**
701 * Export a variable. Copied from ctools.
702 *
703 * This is a replacement for var_export(), allowing us to more nicely
704 * format exports. It will recurse down into arrays and will try to
705 * properly export bools when it can.
706 */
707 function entity_var_export($var, $prefix = '') {
708 if (is_array($var)) {
709 if (empty($var)) {
710 $output = 'array()';
711 }
712 else {
713 $output = "array(\n";
714 foreach ($var as $key => $value) {
715 $output .= " '$key' => " . entity_var_export($value, ' ') . ",\n";
716 }
717 $output .= ')';
718 }
719 }
720 elseif (is_bool($var)) {
721 $output = $var ? 'TRUE' : 'FALSE';
722 }
723 else {
724 $output = var_export($var, TRUE);
725 }
726
727 if ($prefix) {
728 $output = str_replace("\n", "\n$prefix", $output);
729 }
730 return $output;
731 }
732
733 /**
734 * Export a variable in pretty formatted JSON.
735 */
736 function entity_var_json_export($var, $prefix = '') {
737 if (is_array($var) && $var) {
738 // Defines whether we use a JSON array or object.
739 $use_array = ($var == array_values($var));
740 $output = $use_array ? "[" : "{";
741
742 foreach ($var as $key => $value) {
743 if ($use_array) {
744 $values[] = entity_var_json_export($value, ' ');
745 }
746 else {
747 $values[] = entity_var_json_export((string) $key, ' ') . ' : ' . entity_var_json_export($value, ' ');
748 }
749 }
750 // Use several lines for long content. However for objects with a single
751 // entry keep the key in the first line.
752 if (strlen($content = implode(', ', $values)) > 70 && ($use_array || count($values) > 1)) {
753 $output .= "\n " . implode(",\n ", $values) . "\n";
754 }
755 elseif (strpos($content, "\n") !== FALSE) {
756 $output .= " " . $content . "\n";
757 }
758 else {
759 $output .= " " . $content . ' ';
760 }
761 $output .= $use_array ? ']' : '}';
762 }
763 else {
764 $output = drupal_json_encode($var);
765 }
766
767 if ($prefix) {
768 $output = str_replace("\n", "\n$prefix", $output);
769 }
770 return $output;
771 }
772
773 /**
774 * Rebuild the default entities provided in code.
775 *
776 * Exportable entities provided in code get saved to the database once a module
777 * providing defaults in code is activated. This allows module and entity_load()
778 * to easily deal with exportable entities just by relying on the database.
779 *
780 * The defaults get rebuilt if the cache is cleared or new modules providing
781 * defaults are enabled, such that the defaults in the database are up to date.
782 * A default entity gets updated with the latest defaults in code during rebuild
783 * as long as the default has not been overridden. Once a module providing
784 * defaults is disabled, its default entities get removed from the database
785 * unless they have been overridden. In that case the overridden entity is left
786 * in the database, but its status gets updated to 'custom'.
787 *
788 * @param $entity_types
789 * (optional) If specified, only the defaults of the given entity types are
790 * rebuilt.
791 */
792 function entity_defaults_rebuild($entity_types = NULL) {
793 if (!isset($entity_types)) {
794 $entity_types = array();
795 foreach (entity_crud_get_info() as $type => $info) {
796 if (!empty($info['exportable'])) {
797 $entity_types[] = $type;
798 }
799 };
800 }
801 foreach ($entity_types as $type) {
802 _entity_defaults_rebuild($type);
803 }
804 }
805
806 /**
807 * Actually rebuild the defaults of a given entity type.
808 */
809 function _entity_defaults_rebuild($entity_type) {
810 if (lock_acquire('entity_rebuild_' . $entity_type)) {
811 $info = entity_get_info($entity_type);
812 $hook = isset($info['export']['default hook']) ? $info['export']['default hook'] : 'default_' . $entity_type;
813 $keys = $info['entity keys'] + array('module' => 'module', 'status' => 'status', 'name' => $info['entity keys']['id']);
814
815 // Check for the existence of the module and status columns.
816 if (!in_array($keys['status'], $info['schema_fields_sql']['base table']) || !in_array($keys['module'], $info['schema_fields_sql']['base table'])) {
817 trigger_error("Missing database columns for the exportable entity $entity_type as defined by entity_exportable_schema_fields(). Update the according module and run update.php!", E_USER_WARNING);
818 return;
819 }
820
821 // Invoke the hook and collect default entities.
822 $entities = array();
823 foreach (module_implements($hook) as $module) {
824 foreach ((array) module_invoke($module, $hook) as $name => $entity) {
825 $entity->{$keys['name']} = $name;
826 $entity->{$keys['module']} = $module;
827 $entities[$name] = $entity;
828 }
829 }
830 drupal_alter($hook, $entities);
831
832 // Check for defaults that disappeared.
833 $existing_defaults = entity_load_multiple_by_name($entity_type, FALSE, array($keys['status'] => array(ENTITY_OVERRIDDEN, ENTITY_IN_CODE, ENTITY_FIXED)));
834
835 foreach ($existing_defaults as $name => $entity) {
836 if (empty($entities[$name])) {
837 $entity->is_rebuild = TRUE;
838 if (entity_has_status($entity_type, $entity, ENTITY_OVERRIDDEN)) {
839 $entity->{$keys['status']} = ENTITY_CUSTOM;
840 entity_save($entity_type, $entity);
841 }
842 else {
843 entity_delete($entity_type, $name);
844 }
845 unset($entity->is_rebuild);
846 }
847 }
848
849 // Load all existing entities.
850 $existing_entities = entity_load_multiple_by_name($entity_type, array_keys($entities));
851
852 foreach ($existing_entities as $name => $entity) {
853 if (entity_has_status($entity_type, $entity, ENTITY_CUSTOM)) {
854 // If the entity already exists but is not yet marked as overridden, we
855 // have to update the status.
856 if (!entity_has_status($entity_type, $entity, ENTITY_OVERRIDDEN)) {
857 $entity->{$keys['status']} |= ENTITY_OVERRIDDEN;
858 $entity->{$keys['module']} = $entities[$name]->{$keys['module']};
859 $entity->is_rebuild = TRUE;
860 entity_save($entity_type, $entity);
861 unset($entity->is_rebuild);
862 }
863
864 // The entity is overridden, so we do not need to save the default.
865 unset($entities[$name]);
866 }
867 }
868
869 // Save defaults.
870 $originals = array();
871 foreach ($entities as $name => $entity) {
872 if (!empty($existing_entities[$name])) {
873 // Make sure we are updating the existing default.
874 $entity->{$keys['id']} = $existing_entities[$name]->{$keys['id']};
875 unset($entity->is_new);
876 }
877 // Pre-populate $entity->original as we already have it. So we avoid
878 // loading it again.
879 $entity->original = !empty($existing_entities[$name]) ? $existing_entities[$name] : FALSE;
880 // Keep original entities for hook_{entity_type}_defaults_rebuild()
881 // implementations.
882 $originals[$name] = $entity->original;
883
884 $entity->{$keys['status']} |= ENTITY_IN_CODE;
885 $entity->is_rebuild = TRUE;
886 entity_save($entity_type, $entity);
887 unset($entity->is_rebuild);
888 }
889
890 // Invoke an entity type-specific hook so modules may apply changes, e.g.
891 // efficiently rebuild caches.
892 module_invoke_all($entity_type . '_defaults_rebuild', $entities, $originals);
893
894 lock_release('entity_rebuild_' . $entity_type);
895 }
896 }
897
898 /**
899 * Implements hook_modules_enabled().
900 */
901 function entity_modules_enabled($modules) {
902 foreach (_entity_modules_get_default_types($modules) as $type) {
903 _entity_defaults_rebuild($type);
904 }
905 }
906
907 /**
908 * Implements hook_modules_disabled().
909 */
910 function entity_modules_disabled($modules) {
911 foreach (_entity_modules_get_default_types($modules) as $entity_type) {
912 $info = entity_get_info($entity_type);
913
914 // Do nothing if the module providing the entity type has been disabled too.
915 if (isset($info['module']) && in_array($info['module'], $modules)) {
916 return;
917 }
918
919 $keys = $info['entity keys'] + array('module' => 'module', 'status' => 'status', 'name' => $info['entity keys']['id']);
920 // Remove entities provided in code by one of the disabled modules.
921 $query = new EntityFieldQuery();
922 $query->entityCondition('entity_type', $entity_type, '=')
923 ->propertyCondition($keys['module'], $modules, 'IN')
924 ->propertyCondition($keys['status'], array(ENTITY_IN_CODE, ENTITY_FIXED), 'IN');
925 $result = $query->execute();
926 if (isset($result[$entity_type])) {
927 $entities = entity_load($entity_type, array_keys($result[$entity_type]));
928 entity_delete_multiple($entity_type, array_keys($entities));
929 }
930
931 // Update overridden entities to be now custom.
932 $query = new EntityFieldQuery();
933 $query->entityCondition('entity_type', $entity_type, '=')
934 ->propertyCondition($keys['module'], $modules, 'IN')
935 ->propertyCondition($keys['status'], ENTITY_OVERRIDDEN, '=');
936 $result = $query->execute();
937 if (isset($result[$entity_type])) {
938 foreach (entity_load($entity_type, array_keys($result[$entity_type])) as $name => $entity) {
939 $entity->{$keys['status']} = ENTITY_CUSTOM;
940 $entity->{$keys['module']} = NULL;
941 entity_save($entity_type, $entity);
942 }
943 }
944
945 // Rebuild the remaining defaults so any alterations of the disabled modules
946 // are gone.
947 _entity_defaults_rebuild($entity_type);
948 }
949 }
950
951 /**
952 * Gets all entity types for which defaults are provided by the $modules.
953 */
954 function _entity_modules_get_default_types($modules) {
955 $types = array();
956 foreach (entity_crud_get_info() as $entity_type => $info) {
957 if (!empty($info['exportable'])) {
958 $hook = isset($info['export']['default hook']) ? $info['export']['default hook'] : 'default_' . $entity_type;
959 foreach ($modules as $module) {
960 if (module_hook($module, $hook) || module_hook($module, $hook . '_alter')) {
961 $types[] = $entity_type;
962 }
963 }
964 }
965 }
966 return $types;
967 }
968
969 /**
970 * Defines schema fields required for exportable entities.
971 *
972 * Warning: Do not call this function in your module's hook_schema()
973 * implementation or update functions. It is not safe to call functions of
974 * dependencies at this point. Instead of calling the function, just copy over
975 * the content.
976 * For more details see the issue http://drupal.org/node/1122812.
977 */
978 function entity_exportable_schema_fields($module_col = 'module', $status_col = 'status') {
979 return array(
980 $status_col => array(
981 'type' => 'int',
982 'not null' => TRUE,
983 // Set the default to ENTITY_CUSTOM without using the constant as it is
984 // not safe to use it at this point.
985 'default' => 0x01,
986 'size' => 'tiny',
987 'description' => 'The exportable status of the entity.',
988 ),
989 $module_col => array(
990 'description' => 'The name of the providing module if the entity has been defined in code.',
991 'type' => 'varchar',
992 'length' => 255,
993 'not null' => FALSE,
994 ),
995 );
996 }
997
998 /**
999 * Implements hook_flush_caches().
1000 */
1001 function entity_flush_caches() {
1002 entity_property_info_cache_clear();
1003 // Re-build defaults in code, however skip it on the admin modules page. In
1004 // case of enabling or disabling modules we already rebuild defaults in
1005 // entity_modules_enabled() and entity_modules_disabled(), so we do not need
1006 // to do it again.
1007 if (current_path() != 'admin/modules/list/confirm') {
1008 entity_defaults_rebuild();
1009 }
1010 }
1011
1012 /**
1013 * Implements hook_theme().
1014 */
1015 function entity_theme() {
1016 // Build a pattern in the form of "(type1|type2|...)(\.|__)" such that all
1017 // templates starting with an entity type or named like the entity type
1018 // are found.
1019 // This has to match the template suggestions provided in
1020 // template_preprocess_entity().
1021 $types = array_keys(entity_crud_get_info());
1022 $pattern = '(' . implode('|', $types) . ')(\.|__)';
1023
1024 return array(
1025 'entity_status' => array(
1026 'variables' => array('status' => NULL, 'html' => TRUE),
1027 'file' => 'theme/entity.theme.inc',
1028 ),
1029 'entity' => array(
1030 'render element' => 'elements',
1031 'template' => 'entity',
1032 'pattern' => $pattern,
1033 'path' => drupal_get_path('module', 'entity') . '/theme',
1034 'file' => 'entity.theme.inc',
1035 ),
1036 'entity_property' => array(
1037 'render element' => 'elements',
1038 'file' => 'theme/entity.theme.inc',
1039 ),
1040 'entity_ui_overview_item' => array(
1041 'variables' => array('label' => NULL, 'entity_type' => NULL, 'url' => FALSE, 'name' => FALSE),
1042 'file' => 'includes/entity.ui.inc'
1043 ),
1044 );
1045 }
1046
1047 /**
1048 * Label callback that refers to the entity classes label method.
1049 */
1050 function entity_class_label($entity) {
1051 return $entity->label();
1052 }
1053
1054 /**
1055 * URI callback that refers to the entity classes uri method.
1056 */
1057 function entity_class_uri($entity) {
1058 return $entity->uri();
1059 }
1060
1061 /**
1062 * Implements hook_file_download_access() for entity types provided by the CRUD API.
1063 */
1064 function entity_file_download_access($field, $entity_type, $entity) {
1065 $info = entity_get_info($entity_type);
1066 if (in_array('EntityAPIControllerInterface', class_implements($info['controller class']))) {
1067 return entity_access('view', $entity_type, $entity);
1068 }
1069 }
1070
1071 /**
1072 * Determines the UI controller class for a given entity type.
1073 *
1074 * @return EntityDefaultUIController
1075 * If a type is given, the controller for the given entity type. Else an array
1076 * of all enabled UI controllers keyed by entity type is returned.
1077 */
1078 function entity_ui_controller($type = NULL) {
1079 $static = &drupal_static(__FUNCTION__);
1080
1081 if (!isset($type)) {
1082 // Invoke the function for each type to ensure we have fully populated the
1083 // static variable.
1084 foreach (entity_get_info() as $entity_type => $info) {
1085 entity_ui_controller($entity_type);
1086 }
1087 return array_filter($static);
1088 }
1089
1090 if (!isset($static[$type])) {
1091 $info = entity_get_info($type);
1092 $class = isset($info['admin ui']['controller class']) ? $info['admin ui']['controller class'] : 'EntityDefaultUIController';
1093 $static[$type] = (isset($info['admin ui']['path']) && $class) ? new $class($type, $info) : FALSE;
1094 }
1095
1096 return $static[$type];
1097 }
1098
1099 /**
1100 * Implements hook_menu().
1101 *
1102 * @see EntityDefaultUIController::hook_menu()
1103 */
1104 function entity_menu() {
1105 $items = array();
1106 foreach (entity_ui_controller() as $controller) {
1107 $items += $controller->hook_menu();
1108 }
1109 return $items;
1110 }
1111
1112 /**
1113 * Implements hook_forms().
1114 *
1115 * @see EntityDefaultUIController::hook_forms()
1116 * @see entity_ui_get_form()
1117 */
1118 function entity_forms($form_id, $args) {
1119 // For efficiency only invoke an entity types controller, if a form of it is
1120 // requested. Thus if the first (overview and operation form) or the third
1121 // argument (edit form) is an entity type name, add in the types forms.
1122 if (isset($args[0]) && is_string($args[0]) && entity_get_info($args[0])) {
1123 $type = $args[0];
1124 }
1125 elseif (isset($args[2]) && is_string($args[2]) && entity_get_info($args[2])) {
1126 $type = $args[2];
1127 }
1128 if (isset($type) && $controller = entity_ui_controller($type)) {
1129 return $controller->hook_forms();
1130 }
1131 }
1132
1133 /**
1134 * A wrapper around drupal_get_form() that helps building entity forms.
1135 *
1136 * This function may be used by entities to build their entity form. It has to
1137 * be used instead of calling drupal_get_form().
1138 * Entity forms built with this helper receive useful defaults suiting for
1139 * editing a single entity, whereas the special cases of adding and cloning
1140 * of entities are supported too.
1141 *
1142 * While this function is intended to be used to get entity forms for entities
1143 * using the entity ui controller, it may be used for entity types not using
1144 * the ui controller too.
1145 *
1146 * @param $entity_type
1147 * The entity type for which to get the form.
1148 * @param $entity
1149 * The entity for which to return the form.
1150 * If $op is 'add' the entity has to be either initialized before calling this
1151 * function, or NULL may be passed. If NULL is passed, an entity will be
1152 * initialized with empty values using entity_create(). Thus entities, for
1153 * which this is problematic have to care to pass in an initialized entity.
1154 * @param $op
1155 * (optional) One of 'edit', 'add' or 'clone'. Defaults to edit.
1156 * @param $form_state
1157 * (optional) A pre-populated form state, e.g. to add in form include files.
1158 * See entity_metadata_form_entity_ui().
1159 *
1160 * @return
1161 * The fully built and processed form, ready to be rendered.
1162 *
1163 * @see EntityDefaultUIController::hook_forms()
1164 * @see entity_ui_form_submit_build_entity()
1165 */
1166 function entity_ui_get_form($entity_type, $entity, $op = 'edit', $form_state = array()) {
1167 if (isset($entity)) {
1168 list(, , $bundle) = entity_extract_ids($entity_type, $entity);
1169 }
1170 $form_id = (!isset($bundle) || $bundle == $entity_type) ? $entity_type . '_form' : $entity_type . '_edit_' . $bundle . '_form';
1171
1172 if (!isset($entity) && $op == 'add') {
1173 $entity = entity_create($entity_type, array());
1174 }
1175
1176 // Do not use drupal_get_form(), but invoke drupal_build_form() ourself so
1177 // we can prepulate the form state.
1178 $form_state['wrapper_callback'] = 'entity_ui_main_form_defaults';
1179 $form_state['entity_type'] = $entity_type;
1180 form_load_include($form_state, 'inc', 'entity', 'includes/entity.ui');
1181
1182 // Handle cloning. We cannot do that in the wrapper callback as it is too late
1183 // for changing arguments.
1184 if ($op == 'clone') {
1185 $entity = entity_ui_clone_entity($entity_type, $entity);
1186 }
1187
1188 // We don't pass the entity type as first parameter, as the implementing
1189 // module knows the type anyway. However, in order to allow for efficient
1190 // hook_forms() implementiations we append the entity type as last argument,
1191 // which the module implementing the form constructor may safely ignore.
1192 // @see entity_forms()
1193 $form_state['build_info']['args'] = array($entity, $op, $entity_type);
1194 return drupal_build_form($form_id, $form_state);
1195 }
1196
1197 /**
1198 * Helper for using i18n_string().
1199 *
1200 * @param $name
1201 * Textgroup and context glued with ':'.
1202 * @param $default
1203 * String in default language. Default language may or may not be English.
1204 * @param $langcode
1205 * (optional) The code of a certain language to translate the string into.
1206 * Defaults to the i18n_string() default, i.e. the current language.
1207 *
1208 * @see i18n_string()
1209 */
1210 function entity_i18n_string($name, $default, $langcode = NULL) {
1211 return function_exists('i18n_string') ? i18n_string($name, $default, array('langcode' => $langcode)) : $default;
1212 }
1213
1214 /**
1215 * Implements hook_views_api().
1216 */
1217 function entity_views_api() {
1218 return array(
1219 'api' => '3.0-alpha1',
1220 'path' => drupal_get_path('module', 'entity') . '/views',
1221 );
1222 }
1223
1224 /**
1225 * Implements hook_field_extra_fields().
1226 */
1227 function entity_field_extra_fields() {
1228 // Invoke specified controllers for entity types provided by the CRUD API.
1229 $items = array();
1230 foreach (entity_crud_get_info() as $type => $info) {
1231 if (!empty($info['extra fields controller class'])) {
1232 $items = array_merge_recursive($items, entity_get_extra_fields_controller($type)->fieldExtraFields());
1233 }
1234 }
1235 return $items;
1236 }
1237
1238 /**
1239 * Gets the extra field controller class for a given entity type.
1240 *
1241 * @return EntityExtraFieldsControllerInterface|false
1242 * The controller for the given entity type or FALSE if none is specified.
1243 */
1244 function entity_get_extra_fields_controller($type = NULL) {
1245 $static = &drupal_static(__FUNCTION__);
1246
1247 if (!isset($static[$type])) {
1248 $static[$type] = FALSE;
1249 $info = entity_get_info($type);
1250 if (!empty($info['extra fields controller class'])) {
1251 $static[$type] = new $info['extra fields controller class']($type);
1252 }
1253 }
1254 return $static[$type];
1255 }
1256
1257 /**
1258 * Returns a property wrapper for the given data.
1259 *
1260 * If an entity is wrapped, the wrapper can be used to retrieve further wrappers
1261 * for the entitity properties. For that the wrapper support chaining, e.g. you
1262 * can use a node wrapper to get the node authors mail address:
1263 *
1264 * @code
1265 * echo $wrappedNode->author->mail->value();
1266 * @endcode
1267 *
1268 * @param $type
1269 * The type of the passed data.
1270 * @param $data
1271 * The data to wrap. It may be set to NULL, so the wrapper can be used
1272 * without any data for getting information about properties.
1273 * @param $info
1274 * (optional) Specify additional information for the passed data:
1275 * - langcode: (optional) If the data is language specific, its langauge
1276 * code. Defaults to NULL, what means language neutral.
1277 * - bundle: (optional) If an entity is wrapped but not passed, use this key
1278 * to specify the bundle to return a wrapper for.
1279 * - property info: (optional) May be used to use a wrapper with an arbitrary
1280 * data structure (type 'struct'). Use this key for specifying info about
1281 * properties in the same structure as used by hook_entity_property_info().
1282 * - property info alter: (optional) A callback for altering the property
1283 * info before it is utilized by the wrapper.
1284 * - property defaults: (optional) An array of defaults for the info of
1285 * each property of the wrapped data item.
1286 * @return EntityMetadataWrapper
1287 * Dependend on the passed data the right wrapper is returned.
1288 */
1289 function entity_metadata_wrapper($type, $data = NULL, array $info = array()) {
1290 if ($type == 'entity' || (($entity_info = entity_get_info()) && isset($entity_info[$type]))) {
1291 // If the passed entity is the global $user, we load the user object by only
1292 // passing on the user id. The global user is not a fully loaded entity.
1293 if ($type == 'user' && is_object($data) && $data == $GLOBALS['user']) {
1294 $data = $data->uid;
1295 }
1296 return new EntityDrupalWrapper($type, $data, $info);
1297 }
1298 elseif ($type == 'list' || entity_property_list_extract_type($type)) {
1299 return new EntityListWrapper($type, $data, $info);
1300 }
1301 elseif (isset($info['property info'])) {
1302 return new EntityStructureWrapper($type, $data, $info);
1303 }
1304 else {
1305 return new EntityValueWrapper($type, $data, $info);
1306 }
1307 }
1308
1309 /**
1310 * Returns a metadata wrapper for accessing site-wide properties.
1311 *
1312 * Although there is no 'site' entity or such, modules may provide info about
1313 * site-wide properties using hook_entity_property_info(). This function returns
1314 * a wrapper for making use of this properties.
1315 *
1316 * @return EntityMetadataWrapper
1317 * A wrapper for accessing site-wide properties.
1318 *
1319 * @see entity_metadata_system_entity_property_info()
1320 */
1321 function entity_metadata_site_wrapper() {
1322 $site_info = entity_get_property_info('site');
1323 $info['property info'] = $site_info['properties'];
1324 return entity_metadata_wrapper('site', FALSE, $info);
1325 }
1326
1327 /**
1328 * Implements hook_module_implements_alter().
1329 *
1330 * Moves the hook_entity_info_alter() implementation to the bottom so it is
1331 * invoked after all modules relying on the entity API.
1332 * That way we ensure to run last and clear the field-info cache after the
1333 * others added in their bundle information.
1334 *
1335 * @see entity_entity_info_alter()
1336 */
1337 function entity_module_implements_alter(&$implementations, $hook) {
1338 if ($hook == 'entity_info_alter') {
1339 // Move our hook implementation to the bottom.
1340 $group = $implementations['entity'];
1341 unset($implementations['entity']);
1342 $implementations['entity'] = $group;
1343 }
1344 }
1345
1346 /**
1347 * Implements hook_entity_info_alter().
1348 *
1349 * @see entity_module_implements_alter()
1350 */
1351 function entity_entity_info_alter(&$entity_info) {
1352 _entity_info_add_metadata($entity_info);
1353
1354 // Populate a default value for the 'configuration' key of all entity types.
1355 foreach ($entity_info as $type => $info) {
1356 if (!isset($info['configuration'])) {
1357 $entity_info[$type]['configuration'] = !empty($info['exportable']);
1358 }
1359 }
1360 }
1361
1362 /**
1363 * Adds metadata and callbacks for core entities to the entity info.
1364 */
1365 function _entity_info_add_metadata(&$entity_info) {
1366 // Set plural labels.
1367 $entity_info['node']['plural label'] = t('Nodes');
1368 $entity_info['user']['plural label'] = t('Users');
1369 $entity_info['file']['plural label'] = t('Files');
1370
1371 // Set descriptions.
1372 $entity_info['node']['description'] = t('Nodes represent the main site content items.');
1373 $entity_info['user']['description'] = t('Users who have created accounts on your site.');
1374 $entity_info['file']['description'] = t('Uploaded file.');
1375
1376 // Set access callbacks.
1377 $entity_info['node']['access callback'] = 'entity_metadata_no_hook_node_access';
1378 $entity_info['user']['access callback'] = 'entity_metadata_user_access';
1379 // File entity has it's own entity_access function.
1380 if (!module_exists('file_entity')) {
1381 $entity_info['file']['access callback'] = 'entity_metadata_file_access';
1382 }
1383
1384 // CRUD function callbacks.
1385 $entity_info['node']['creation callback'] = 'entity_metadata_create_node';
1386 $entity_info['node']['save callback'] = 'node_save';
1387 $entity_info['node']['deletion callback'] = 'node_delete';
1388 $entity_info['node']['revision deletion callback'] = 'node_revision_delete';
1389 $entity_info['user']['creation callback'] = 'entity_metadata_create_object';
1390 $entity_info['user']['save callback'] = 'entity_metadata_user_save';
1391 $entity_info['user']['deletion callback'] = 'user_delete';
1392 $entity_info['file']['save callback'] = 'file_save';
1393 $entity_info['file']['deletion callback'] = 'entity_metadata_delete_file';
1394
1395 // Form callbacks.
1396 $entity_info['node']['form callback'] = 'entity_metadata_form_node';
1397 $entity_info['user']['form callback'] = 'entity_metadata_form_user';
1398
1399 // View callbacks.
1400 $entity_info['node']['view callback'] = 'entity_metadata_view_node';
1401 $entity_info['user']['view callback'] = 'entity_metadata_view_single';
1402
1403 if (module_exists('comment')) {
1404 $entity_info['comment']['plural label'] = t('Comments');
1405 $entity_info['comment']['description'] = t('Remark or note that refers to a node.');
1406 $entity_info['comment']['access callback'] = 'entity_metadata_comment_access';
1407 $entity_info['comment']['creation callback'] = 'entity_metadata_create_comment';
1408 $entity_info['comment']['save callback'] = 'comment_save';
1409 $entity_info['comment']['deletion callback'] = 'comment_delete';
1410 $entity_info['comment']['view callback'] = 'entity_metadata_view_comment';
1411 $entity_info['comment']['form callback'] = 'entity_metadata_form_comment';
1412 }
1413 if (module_exists('taxonomy')) {
1414 $entity_info['taxonomy_term']['plural label'] = t('Taxonomy terms');
1415 $entity_info['taxonomy_term']['description'] = t('Taxonomy terms are used for classifying content.');
1416 $entity_info['taxonomy_term']['access callback'] = 'entity_metadata_taxonomy_access';
1417 $entity_info['taxonomy_term']['creation callback'] = 'entity_metadata_create_object';
1418 $entity_info['taxonomy_term']['save callback'] = 'taxonomy_term_save';
1419 $entity_info['taxonomy_term']['deletion callback'] = 'taxonomy_term_delete';
1420 $entity_info['taxonomy_term']['view callback'] = 'entity_metadata_view_single';
1421 $entity_info['taxonomy_term']['form callback'] = 'entity_metadata_form_taxonomy_term';
1422
1423 $entity_info['taxonomy_vocabulary']['plural label'] = t('Taxonomy vocabularies');
1424 $entity_info['taxonomy_vocabulary']['description'] = t('Vocabularies contain related taxonomy terms, which are used for classifying content.');
1425 $entity_info['taxonomy_vocabulary']['access callback'] = 'entity_metadata_taxonomy_access';
1426 $entity_info['taxonomy_vocabulary']['creation callback'] = 'entity_metadata_create_object';
1427 $entity_info['taxonomy_vocabulary']['save callback'] = 'taxonomy_vocabulary_save';
1428 $entity_info['taxonomy_vocabulary']['deletion callback'] = 'taxonomy_vocabulary_delete';
1429 $entity_info['taxonomy_vocabulary']['form callback'] = 'entity_metadata_form_taxonomy_vocabulary';
1430 // Token type mapping.
1431 $entity_info['taxonomy_term']['token type'] = 'term';
1432 $entity_info['taxonomy_vocabulary']['token type'] = 'vocabulary';
1433 }
1434 }
1435
1436 /**
1437 * Implements hook_ctools_plugin_directory().
1438 */
1439 function entity_ctools_plugin_directory($module, $plugin) {
1440 if ($module == 'ctools' && $plugin == 'content_types') {
1441 return 'ctools/content_types';
1442 }
1443 }