/[drupal]/drupal/modules/field/field.info.inc
ViewVC logotype

Contents of /drupal/modules/field/field.info.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.26 - (show annotations) (download) (as text)
Sat Oct 31 16:06:35 2009 UTC (3 weeks, 4 days ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10
Changes since 1.25: +10 -63 lines
File MIME type: text/x-php
- Patch #606994 by yched: move entity handling out of Field API.
1 <?php
2 // $Id: field.info.inc,v 1.25 2009/10/25 02:51:28 webchick Exp $
3
4 /**
5 * @file
6 * Field Info API, providing information about available fields and field types.
7 */
8
9 /**
10 * @defgroup field_info Field Info API
11 * @{
12 * Obtain information about Field API configuration.
13 *
14 * The Field Info API exposes information about field types, fields,
15 * instances, bundles, widget types, display formatters, behaviors,
16 * and settings defined by or with the Field API.
17 */
18
19 /**
20 * Clear the field info cache without clearing the field data cache.
21 *
22 * This is useful when deleted fields or instances are purged. We
23 * need to remove the purged records, but no actual field data items
24 * are affected.
25 */
26 function field_info_cache_clear() {
27 // @todo: Remove this when field_attach_*_bundle() bundle management
28 // functions are moved to the entity API.
29 drupal_static_reset('entity_get_info');
30 cache_clear_all('entity_info', 'cache');
31
32 _field_info_collate_types(TRUE);
33 drupal_static_reset('field_build_modes');
34 _field_info_collate_fields(TRUE);
35 }
36
37 /**
38 * Collate all information on field types, widget types and related structures.
39 *
40 * @param $reset
41 * If TRUE, clear the cache. The information will be rebuilt from the database
42 * next time it is needed. Defaults to FALSE.
43 * @return
44 * If $reset is TRUE, nothing.
45 * If $reset is FALSE, an array containing the following elements:
46 *
47 * field types: array of hook_field_info() results, keyed by field_type.
48 * * label, description, settings, instance_settings, default_widget,
49 * default_formatter, behaviors: from hook_field_info()
50 * * module: the module that exposes the field type
51 *
52 * widget types: array of hook_field_widget_info() results, keyed by
53 * widget_type.
54 * * label, field types, settings, behaviors: from hook_field_widget_info()
55 * * module: module that exposes the widget type
56 *
57 * formatter types: array of hook_field_formatter_info() results, keyed by
58 * formatter_type.
59 * * label, field types, behaviors: from hook_field_formatter_info()
60 * * module: module that exposes the formatter type
61 */
62 function _field_info_collate_types($reset = FALSE) {
63 static $info;
64
65 if ($reset) {
66 $info = NULL;
67 cache_clear_all('field_info_types', 'cache_field');
68 return;
69 }
70
71 if (!isset($info)) {
72 if ($cached = cache_get('field_info_types', 'cache_field')) {
73 $info = $cached->data;
74 }
75 else {
76 $info = array(
77 'field types' => array(),
78 'widget types' => array(),
79 'formatter types' => array(),
80 'storage types' => array(),
81 );
82
83 // Populate field types.
84 foreach (module_implements('field_info') as $module) {
85 $field_types = (array) module_invoke($module, 'field_info');
86 foreach ($field_types as $name => $field_info) {
87 // Provide defaults.
88 $field_info += array(
89 'settings' => array(),
90 'instance_settings' => array(),
91 );
92 $info['field types'][$name] = $field_info;
93 $info['field types'][$name]['module'] = $module;
94 }
95 }
96 drupal_alter('field_info', $info['field types']);
97
98 // Populate widget types.
99 foreach (module_implements('field_widget_info') as $module) {
100 $widget_types = (array) module_invoke($module, 'field_widget_info');
101 foreach ($widget_types as $name => $widget_info) {
102 // Provide defaults.
103 $widget_info += array(
104 'settings' => array(),
105 );
106 $info['widget types'][$name] = $widget_info;
107 $info['widget types'][$name]['module'] = $module;
108 }
109 }
110 drupal_alter('field_widget_info', $info['widget types']);
111
112 // Populate formatter types.
113 foreach (module_implements('field_formatter_info') as $module) {
114 $formatter_types = (array) module_invoke($module, 'field_formatter_info');
115 foreach ($formatter_types as $name => $formatter_info) {
116 // Provide defaults.
117 $formatter_info += array(
118 'settings' => array(),
119 );
120 $info['formatter types'][$name] = $formatter_info;
121 $info['formatter types'][$name]['module'] = $module;
122 }
123 }
124 drupal_alter('field_formatter_info', $info['formatter types']);
125
126 // Populate storage types.
127 foreach (module_implements('field_storage_info') as $module) {
128 $storage_types = (array) module_invoke($module, 'field_storage_info');
129 foreach ($storage_types as $name => $storage_info) {
130 // Provide defaults.
131 $storage_info += array(
132 'settings' => array(),
133 );
134 $info['storage types'][$name] = $storage_info;
135 $info['storage types'][$name]['module'] = $module;
136 }
137 }
138 drupal_alter('field_storage_info', $info['storage types']);
139
140 cache_set('field_info_types', $info, 'cache_field');
141 }
142 }
143
144 return $info;
145 }
146
147 /**
148 * Collate all information on existing fields and instances.
149 *
150 * @param $reset
151 * If TRUE, clear the cache. The information will be rebuilt from the
152 * database next time it is needed. Defaults to FALSE.
153 * @return
154 * If $reset is TRUE, nothing.
155 * If $reset is FALSE, an array containing the following elements:
156 * - fields: Array of existing fields, keyed by field name. This entry only
157 * lists non-deleted fields. Each field has an additional element,
158 * 'bundles', which is an array of all non-deleted instances to which the
159 * field is assigned.
160 * - fields_id: Array of existing fields, keyed by field id. This entry lists
161 * both deleted and non-deleted fields. The bundles element is the same as
162 * for 'fields'.
163 * - instances: Array of existing instances, keyed by object type, bundle
164 * name and field name. This entry only lists non-deleted instances.
165 */
166 function _field_info_collate_fields($reset = FALSE) {
167 static $info;
168
169 if ($reset) {
170 $info = NULL;
171 cache_clear_all('field_info_fields', 'cache_field');
172 return;
173 }
174
175 if (!isset($info)) {
176 if ($cached = cache_get('field_info_fields', 'cache_field')) {
177 $definitions = $cached->data;
178 }
179 else {
180 $definitions = array(
181 'field_ids' => field_read_fields(array(), array('include_deleted' => 1)),
182 'instances' => field_read_instances(),
183 );
184 cache_set('field_info_fields', $definitions, 'cache_field');
185 }
186
187 // Populate 'field_ids' with all fields.
188 $info['field_ids'] = array();
189 foreach ($definitions['field_ids'] as $key => $field) {
190 $info['field_ids'][$key] = $definitions['field_ids'][$key] = _field_info_prepare_field($field);
191 }
192
193 // Populate 'fields' only with non-deleted fields.
194 $info['fields'] = array();
195 foreach ($info['field_ids'] as $field) {
196 if (!$field['deleted']) {
197 $info['fields'][$field['field_name']] = $field;
198 }
199 }
200
201 // Populate 'instances'. Only non-deleted instances are considered.
202 $info['instances'] = array();
203 foreach (field_info_bundles() as $obj_type => $bundles) {
204 foreach ($bundles as $bundle => $bundle_info) {
205 $info['instances'][$obj_type][$bundle] = array();
206 }
207 }
208 foreach ($definitions['instances'] as $instance) {
209 $field = $info['fields'][$instance['field_name']];
210 $instance = _field_info_prepare_instance($instance, $field);
211 $info['instances'][$instance['object_type']][$instance['bundle']][$instance['field_name']] = $instance;
212 // Enrich field definitions with the list of bundles where they have
213 // instances. NOTE: Deleted fields in $info['field_ids'] are not
214 // enriched because all of their instances are deleted, too, and
215 // are thus not in $definitions['instances'].
216 $info['fields'][$instance['field_name']]['bundles'][$instance['object_type']][] = $instance['bundle'];
217 $info['field_ids'][$instance['field_id']]['bundles'][$instance['object_type']][] = $instance['bundle'];
218
219 // Add storage details.
220 $details = (array) module_invoke($field['storage']['module'], 'field_storage_details', $field, $instance);
221 drupal_alter('field_storage_details', $details, $field, $instance);
222 $info['instances'][$instance['object_type']][$instance['bundle']][$instance['field_name']]['storage_details'] = $details;
223 }
224 }
225
226 return $info;
227 }
228
229 /**
230 * Prepare a field definition for the current run-time context.
231 *
232 * Since the field was last saved or updated, new field settings can be
233 * expected.
234 *
235 * @param $field
236 * The raw field structure as read from the database.
237 */
238 function _field_info_prepare_field($field) {
239 // Make sure all expected field settings are present.
240 $field['settings'] += field_info_field_settings($field['type']);
241 $field['storage']['settings'] += field_info_storage_settings($field['storage']['type']);
242
243 return $field;
244 }
245
246 /**
247 * Prepare an instance definition for the current run-time context.
248 *
249 * Since the instance was last saved or updated, a number of things might have
250 * changed: widgets or formatters disabled, new settings expected, new build
251 * modes added...
252 *
253 * @param $instance
254 * The raw instance structure as read from the database.
255 * @param $field
256 * The field structure for the instance.
257 */
258 function _field_info_prepare_instance($instance, $field) {
259 $field_type = field_info_field_types($field['type']);
260
261 // Make sure all expected instance settings are present.
262 $instance['settings'] += field_info_instance_settings($field['type']);
263
264 // Set a default value for the instance.
265 if (field_behaviors_widget('default value', $instance) == FIELD_BEHAVIOR_DEFAULT && !isset($instance['default_value'])) {
266 $instance['default_value'] = NULL;
267 }
268
269 // Fallback to default widget if widget type is not available.
270 if (!field_info_widget_types($instance['widget']['type'])) {
271 $instance['widget']['type'] = $field_type['default_widget'];
272 }
273 // Make sure all expected widget settings are present.
274 $instance['widget']['settings'] += field_info_widget_settings($instance['widget']['type']);
275
276 foreach ($instance['display'] as $build_mode => $display) {
277 if ($display['type'] != 'hidden') {
278 // Fallback to default formatter if formatter type is not available.
279 if (!field_info_formatter_types($instance['display'][$build_mode]['type'])) {
280 $instance['display'][$build_mode]['type'] = $field_type['default_formatter'];
281 }
282 // Make sure all expected formatter settings are present.
283 $instance['display'][$build_mode]['settings'] += field_info_formatter_settings($instance['display'][$build_mode]['type']);
284 }
285 }
286
287 // Fallback to 'full' display settings for unspecified build modes.
288 foreach (field_build_modes($instance['object_type']) as $build_mode => $label) {
289 if (!isset($instance['display'][$build_mode])) {
290 $instance['display'][$build_mode] = $instance['display']['full'];
291 }
292 }
293
294 return $instance;
295 }
296
297 /**
298 * Helper function for determining the behavior of a widget
299 * with respect to a given operation.
300 *
301 * @param $op
302 * The name of the operation.
303 * Currently supported: 'default value', 'multiple values'.
304 * @param $instance
305 * The field instance array.
306 * @return
307 * FIELD_BEHAVIOR_NONE - do nothing for this operation.
308 * FIELD_BEHAVIOR_CUSTOM - use the widget's callback function.
309 * FIELD_BEHAVIOR_DEFAULT - use field.module default behavior.
310 */
311 function field_behaviors_widget($op, $instance) {
312 $info = field_info_widget_types($instance['widget']['type']);
313 return isset($info['behaviors'][$op]) ? $info['behaviors'][$op] : FIELD_BEHAVIOR_DEFAULT;
314 }
315
316 /**
317 * Helper function for determining the behavior of a formatter
318 * with respect to a given operation.
319 *
320 * @param $op
321 * The name of the operation.
322 * Currently supported: 'multiple values'
323 * @param $display
324 * The $instance['display'][$build_mode] array.
325 * @return
326 * FIELD_BEHAVIOR_NONE - do nothing for this operation.
327 * FIELD_BEHAVIOR_CUSTOM - use the formatter's callback function.
328 * FIELD_BEHAVIOR_DEFAULT - use field module default behavior.
329 */
330 function field_behaviors_formatter($op, $display) {
331 $info = field_info_formatter_types($display['type']);
332 return isset($info['behaviors'][$op]) ? $info['behaviors'][$op] : FIELD_BEHAVIOR_DEFAULT;
333 }
334
335 /**
336 * Return hook_field_info() data.
337 *
338 * @param $field_type
339 * (optional) A field type name. If ommitted, all field types will be
340 * returned.
341 * @return
342 * Either a field type description, as provided by hook_field_info(), or an
343 * array of all existing field types, keyed by field type name.
344 */
345 function field_info_field_types($field_type = NULL) {
346 $info = _field_info_collate_types();
347 $field_types = $info['field types'];
348 if ($field_type) {
349 if (isset($field_types[$field_type])) {
350 return $field_types[$field_type];
351 }
352 }
353 else {
354 return $field_types;
355 }
356 }
357
358 /**
359 * Return hook_field_widget_info() data.
360 *
361 * @param $widget_type
362 * (optional) A widget type name. If ommitted, all widget types will be
363 * returned.
364 * @return
365 * Either a widget type description, as provided by
366 * hook_field_widget_info(), or an array of all existing widget types, keyed
367 * by widget type name.
368 */
369 function field_info_widget_types($widget_type = NULL) {
370 $info = _field_info_collate_types();
371 $widget_types = $info['widget types'];
372 if ($widget_type) {
373 if (isset($widget_types[$widget_type])) {
374 return $widget_types[$widget_type];
375 }
376 }
377 else {
378 return $widget_types;
379 }
380 }
381
382 /**
383 * Return hook_field_formatter_info() data.
384 *
385 * @param $formatter_type
386 * (optional) A formatter type name. If ommitted, all formatter types will be
387 * returned.
388 * @return
389 * Either a formatter type description, as provided by
390 * hook_field_formatter_info(), or an array of all existing formatter types,
391 * keyed by formatter type name.
392 */
393 function field_info_formatter_types($formatter_type = NULL) {
394 $info = _field_info_collate_types();
395 $formatter_types = $info['formatter types'];
396 if ($formatter_type) {
397 if (isset($formatter_types[$formatter_type])) {
398 return $formatter_types[$formatter_type];
399 }
400 }
401 else {
402 return $formatter_types;
403 }
404 }
405
406 /**
407 * Return hook_field_storage_info() data.
408 *
409 * @param $storage_type
410 * (optional) A storage type name. If ommitted, all storage types will be
411 * returned.
412 * @return
413 * Either a storage type description, as provided by
414 * hook_field_storage_info(), or an array of all existing storage types,
415 * keyed by storage type name.
416 */
417 function field_info_storage_types($storage_type = NULL) {
418 $info = _field_info_collate_types();
419 $storage_types = $info['storage types'];
420 if ($storage_type) {
421 if (isset($storage_types[$storage_type])) {
422 return $storage_types[$storage_type];
423 }
424 }
425 else {
426 return $storage_types;
427 }
428 }
429
430 /**
431 * Return information about existing bundles.
432 *
433 * @param $obj_type
434 * The type of object; e.g. 'node' or 'user'.
435 * @return
436 * An array of bundles for the $obj_type keyed by bundle name,
437 * or, if no $obj_type was provided, the array of all existing bundles,
438 * keyed by object type.
439 */
440 function field_info_bundles($obj_type = NULL) {
441 $info = entity_get_info();
442
443 if ($obj_type) {
444 return isset($info[$obj_type]['bundles']) ? $info[$obj_type]['bundles'] : array();
445 }
446
447 $bundles = array();
448 foreach ($info as $type => $entity_info) {
449 $bundles[$type] = $entity_info['bundles'];
450 }
451 return $bundles;
452 }
453
454 /**
455 * Return array of all field data, keyed by field name.
456 *
457 * @param $bundle_type
458 * (optional) The bundle type on which to filter the list of fields. In the
459 * case of nodes, this is the node type.
460 * @param $field
461 * (optional) A field array or name on which to filter the list.
462 * @param $field_type
463 * (optional) A field type on which to filter the list.
464 * @return
465 * An array of Field objects. Each Field object has an additional
466 * property, bundles, which is an array of all the bundles to which
467 * this field belongs.
468 */
469 function field_info_fields($bundle_type = NULL, $field = NULL, $field_type = NULL) {
470 // Build the list of fields to be used for retrieval.
471 if (isset($field)) {
472 if (is_string($field)) {
473 $field = field_info_field($field);
474 }
475 $fields = array($field['field_name'] => $field);
476 }
477 elseif (isset($bundle_type)) {
478 $instances = field_info_instances($bundle_type);
479 $fields = array();
480 foreach ($instances as $field_name => $instance) {
481 $fields[$field_name] = field_info_field($field_name);
482 }
483 }
484 else {
485 $info = _field_info_collate_fields();
486 $fields = $info['fields'];
487 }
488
489 // If a field type was given, filter the list down to fields of that type.
490 if (isset($field_type)) {
491 foreach ($fields as $key => $field) {
492 if ($field['type'] != $field_type) {
493 unset($fields[$key]);
494 }
495 }
496 }
497
498 return $fields;
499 }
500
501 /**
502 * Return data about an individual field.
503 *
504 * @param $field_name
505 * The name of the field to retrieve. $field_name can only refer to a
506 * non-deleted field.
507 * @return
508 * The named field object, or NULL. The Field object has an additional
509 * property, bundles, which is an array of all the bundles to which
510 * this field belongs.
511 */
512 function field_info_field($field_name) {
513 $info = _field_info_collate_fields();
514 if (isset($info['fields'][$field_name])) {
515 return $info['fields'][$field_name];
516 }
517 }
518
519 /**
520 * Return data about an individual field by its id.
521 *
522 * @param $field_id
523 * The id of the field to retrieve. $field_id can refer to a
524 * deleted field.
525 * @return
526 * The named field object, or NULL. The Field object has an additional
527 * property, bundles, which is an array of all the bundles to which
528 * this field belongs.
529 */
530 function field_info_field_by_id($field_id) {
531 $info = _field_info_collate_fields();
532 if (isset($info['field_ids'][$field_id])) {
533 return $info['field_ids'][$field_id];
534 }
535 }
536
537 /**
538 * Retrieve instances.
539 *
540 * @param $obj_type
541 * The object type for which to return instances.
542 * @param $bundle_name
543 * The bundle name for which to return instances.
544 * @return
545 * If $obj_type is not set, return all instances keyed by object type and
546 * bundle name. If $obj_type is set, return all instances for that object
547 * type, keyed by bundle name. If $obj_type and $bundle_name are set, return
548 * all instances for that bundle.
549 */
550 function field_info_instances($obj_type = NULL, $bundle_name = NULL) {
551 $info = _field_info_collate_fields();
552 if (!isset($obj_type)) {
553 return $info['instances'];
554 }
555 if (!isset($bundle_name)) {
556 return $info['instances'][$obj_type];
557 }
558 if (isset($info['instances'][$obj_type][$bundle_name])) {
559 return $info['instances'][$obj_type][$bundle_name];
560 }
561 return array();
562 }
563
564 /**
565 * Return an array of instance data for a specific field and bundle.
566 *
567 * @param $obj_type
568 * The object type for the instance.
569 * @param $field_name
570 * The field name for the instance.
571 * @param $bundle_name
572 * The bundle name for the instance.
573 */
574 function field_info_instance($obj_type, $field_name, $bundle_name) {
575 $info = _field_info_collate_fields();
576 if (isset($info['instances'][$obj_type][$bundle_name][$field_name])) {
577 return $info['instances'][$obj_type][$bundle_name][$field_name];
578 }
579 }
580
581 /**
582 * Return a field type's default settings.
583 *
584 * @param $type
585 * A field type name.
586 * @return
587 * The field type's default settings, as provided by hook_field_info(), or an
588 * empty array.
589 */
590 function field_info_field_settings($type) {
591 $info = field_info_field_types($type);
592 return isset($info['settings']) ? $info['settings'] : array();
593 }
594
595 /**
596 * Return a field type's default instance settings.
597 *
598 * @param $type
599 * A field type name.
600 * @return
601 * The field type's default instance settings, as provided by
602 * hook_field_info(), or an empty array.
603 */
604 function field_info_instance_settings($type) {
605 $info = field_info_field_types($type);
606 return isset($info['instance_settings']) ? $info['instance_settings'] : array();
607 }
608
609 /**
610 * Return a field widget's default settings.
611 *
612 * @param $type
613 * A widget type name.
614 * @return
615 * The widget type's default settings, as provided by
616 * hook_field_widget_info(), or an empty array.
617 */
618 function field_info_widget_settings($type) {
619 $info = field_info_widget_types($type);
620 return isset($info['settings']) ? $info['settings'] : array();
621 }
622
623 /**
624 * Return a field formatter's default settings.
625 *
626 * @param $type
627 * A field formatter type name.
628 * @return
629 * The formatter type's default settings, as provided by
630 * hook_field_formatter_info(), or an empty array.
631 */
632 function field_info_formatter_settings($type) {
633 $info = field_info_formatter_types($type);
634 return isset($info['settings']) ? $info['settings'] : array();
635 }
636
637 /**
638 * Return a field formatter's default settings.
639 *
640 * @param $type
641 * A field storage type name.
642 * @return
643 * The storage type's default settings, as provided by
644 * hook_field_storage_info(), or an empty array.
645 */
646 function field_info_storage_settings($type) {
647 $info = field_info_storage_types($type);
648 return isset($info['settings']) ? $info['settings'] : array();
649 }
650
651 /**
652 * @} End of "defgroup field_info"
653 */

  ViewVC Help
Powered by ViewVC 1.1.2