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

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

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


Revision 1.22 - (show annotations) (download) (as text)
Sat Oct 31 16:06:35 2009 UTC (3 weeks, 6 days ago) by dries
Branch: MAIN
CVS Tags: DRUPAL-7-0-UNSTABLE-10
Changes since 1.21: +2 -2 lines
File MIME type: text/x-php
- Patch #606994 by yched: move entity handling out of Field API.
1 <?php
2 // $Id: field.default.inc,v 1.21 2009/10/16 03:21:23 webchick Exp $
3
4 /**
5 * @file
6 * Default 'implementations' of hook_field_*(): common field housekeeping.
7 *
8 * Those implementations are special, as field.module does not define any field
9 * types. Those functions take care of default stuff common to all field types.
10 * They are called through the _field_invoke_default() iterator, generally in
11 * the corresponding field_attach_[operation]() function.
12 */
13
14 function field_default_extract_form_values($obj_type, $object, $field, $instance, $langcode, &$items, $form, &$form_state) {
15 $field_name = $field['field_name'];
16
17 if (isset($form_state['values'][$field_name][$langcode])) {
18 $items = $form_state['values'][$field_name][$langcode];
19 // Remove the 'value' of the 'add more' button.
20 unset($items[$field_name . '_add_more']);
21 }
22 }
23
24 function field_default_submit($obj_type, $object, $field, $instance, $langcode, &$items, $form, &$form_state) {
25 $field_name = $field['field_name'];
26
27 // Reorder items to account for drag-n-drop reordering.
28 if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_DEFAULT) {
29 $items = _field_sort_items($field, $items);
30 }
31
32 // Filter out empty values.
33 $items = field_set_empty($field, $items);
34 }
35
36 /**
37 * Default field 'insert' operation.
38 *
39 * Insert default value if no $object->$field_name entry was provided.
40 * This can happen with programmatic saves, or on form-based creation where
41 * the current user doesn't have 'edit' permission for the field.
42 */
43 function field_default_insert($obj_type, $object, $field, $instance, $langcode, &$items) {
44 // _field_invoke() populates $items with an empty array if the $object has no
45 // entry for the field, so we check on the $object itself.
46 // We also check that the current field translation is actually defined before
47 // assigning it a default value. This way we ensure that only the intended
48 // languages get a default value. Otherwise we could have default values for
49 // not yet open languages.
50 if (empty($object) || !property_exists($object, $field['field_name']) ||
51 (isset($object->{$field['field_name']}[$langcode]) && count($object->{$field['field_name']}[$langcode]) == 0)) {
52 $items = field_get_default_value($obj_type, $object, $field, $instance, $langcode);
53 }
54 }
55
56 /**
57 * Invoke hook_field_formatter_prepare_view() on the relavant formatters.
58 */
59 function field_default_prepare_view($obj_type, $objects, $field, $instances, $langcode, &$items, $options, $build_mode) {
60 // Group objects, instances and items by formatter module.
61 $modules = array();
62 foreach ($instances as $id => $instance) {
63 $module = $instance['display'][$build_mode]['module'];
64 $modules[$module] = $module;
65 $grouped_objects[$module][$id] = $objects[$id];
66 $grouped_instances[$module][$id] = $instance;
67 // hook_field_formatter_prepare_view() alters $items by reference.
68 $grouped_items[$module][$id] = &$items[$id];
69 }
70
71 foreach ($modules as $module) {
72 // Invoke hook_field_formatter_prepare_view().
73 $function = $module . '_field_formatter_prepare_view';
74 if (function_exists($function)) {
75 $function($obj_type, $grouped_objects[$module], $field, $grouped_instances[$module], $langcode, $grouped_items[$module], $build_mode);
76 }
77 }
78 }
79
80 /**
81 * Default field 'view' operation.
82 *
83 * @see field_attach_view()
84 */
85 function field_default_view($obj_type, $object, $field, $instance, $langcode, $items, $build_mode) {
86 list($id, $vid, $bundle) = entity_extract_ids($obj_type, $object);
87
88 $addition = array();
89 $display = $instance['display'][$build_mode];
90
91 if ($display['type'] !== 'hidden') {
92 $theme = 'field_formatter_' . $display['type'];
93 $single = (field_behaviors_formatter('multiple values', $display) == FIELD_BEHAVIOR_DEFAULT);
94
95 $label_display = $display['label'];
96 if ($build_mode == 'search_index') {
97 $label_display = 'hidden';
98 }
99
100 $info = array(
101 '#field_name' => $field['field_name'],
102 '#object_type' => $obj_type,
103 '#bundle' => $bundle,
104 '#object' => $object,
105 );
106
107 $element = $info + array(
108 '#theme' => 'field',
109 '#weight' => $display['weight'],
110 '#title' => check_plain(t($instance['label'])),
111 '#access' => field_access('view', $field, $obj_type, $object),
112 '#label_display' => $label_display,
113 '#build_mode' => $build_mode,
114 '#language' => $langcode,
115 '#formatter_single' => $single,
116 'items' => array(),
117 );
118
119 // Fill-in items.
120 foreach ($items as $delta => $item) {
121 $element['items'][$delta] = array(
122 '#item' => $item,
123 '#weight' => $delta,
124 );
125 }
126
127 // Append formatter information either on each item ('single-value' formatter)
128 // or at the upper 'items' level ('multiple-value' formatter)
129 $format_info = $info + array(
130 '#theme' => $theme,
131 '#formatter' => $display['type'],
132 '#settings' => $display['settings'],
133 );
134
135 if ($single) {
136 foreach ($items as $delta => $item) {
137 $element['items'][$delta] += $format_info;
138 $element['items'][$delta]['#item']['#delta'] = $delta;
139 }
140 }
141 else {
142 $element['items'] += $format_info;
143 }
144
145 $addition = array($field['field_name'] => $element);
146 }
147
148 return $addition;
149 }
150
151 function field_default_prepare_translation($obj_type, $object, $field, $instance, $langcode, &$items) {
152 $addition = array();
153 if (isset($object->translation_source->$field['field_name'])) {
154 $addition[$field['field_name']] = $object->translation_source->$field['field_name'];
155 }
156 return $addition;
157 }

  ViewVC Help
Powered by ViewVC 1.1.2