6d03855c6c5e0ce149b32e0b50725180eb8ef269
[project/drupal.git] / modules / field / tests / field_test.field.inc
1 <?php
2
3 /**
4 * @file
5 * Defines a field type and its formatters and widgets.
6 */
7
8 /**
9 * Implements hook_field_info().
10 */
11 function field_test_field_info() {
12 return array(
13 'test_field' => array(
14 'label' => t('Test field'),
15 'description' => t('Dummy field type used for tests.'),
16 'settings' => array(
17 'test_field_setting' => 'dummy test string',
18 'changeable' => 'a changeable field setting',
19 'unchangeable' => 'an unchangeable field setting',
20 ),
21 'instance_settings' => array(
22 'test_instance_setting' => 'dummy test string',
23 'test_hook_field_load' => FALSE,
24 ),
25 'default_widget' => 'test_field_widget',
26 'default_formatter' => 'field_test_default',
27 ),
28 'shape' => array(
29 'label' => t('Shape'),
30 'description' => t('Another dummy field type.'),
31 'settings' => array(),
32 'instance_settings' => array(),
33 'default_widget' => 'test_field_widget',
34 'default_formatter' => 'field_test_default',
35 ),
36 'hidden_test_field' => array(
37 'no_ui' => TRUE,
38 'label' => t('Hidden from UI test field'),
39 'description' => t('Dummy hidden field type used for tests.'),
40 'settings' => array(),
41 'instance_settings' => array(),
42 'default_widget' => 'test_field_widget',
43 'default_formatter' => 'field_test_default',
44 ),
45 );
46 }
47
48 /**
49 * Implements hook_field_update_forbid().
50 */
51 function field_test_field_update_forbid($field, $prior_field, $has_data) {
52 if ($field['type'] == 'test_field' && $field['settings']['unchangeable'] != $prior_field['settings']['unchangeable']) {
53 throw new FieldException("field_test 'unchangeable' setting cannot be changed'");
54 }
55 }
56
57 /**
58 * Implements hook_field_load().
59 */
60 function field_test_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
61 foreach ($items as $id => $item) {
62 // To keep the test non-intrusive, only act for instances with the
63 // test_hook_field_load setting explicitly set to TRUE.
64 if ($instances[$id]['settings']['test_hook_field_load']) {
65 foreach ($item as $delta => $value) {
66 // Don't add anything on empty values.
67 if ($value) {
68 $items[$id][$delta]['additional_key'] = 'additional_value';
69 }
70 }
71 }
72 }
73 }
74
75 /**
76 * Implements hook_field_validate().
77 *
78 * Possible error codes:
79 * - 'field_test_invalid': The value is invalid.
80 */
81 function field_test_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
82 foreach ($items as $delta => $item) {
83 if ($item['value'] == -1) {
84 $errors[$field['field_name']][$langcode][$delta][] = array(
85 'error' => 'field_test_invalid',
86 'message' => t('%name does not accept the value -1.', array('%name' => $instance['label'])),
87 );
88 }
89 }
90 }
91
92 /**
93 * Implements hook_field_is_empty().
94 */
95 function field_test_field_is_empty($item, $field) {
96 return empty($item['value']);
97 }
98
99 /**
100 * Implements hook_field_settings_form().
101 */
102 function field_test_field_settings_form($field, $instance, $has_data) {
103 $settings = $field['settings'];
104
105 $form['test_field_setting'] = array(
106 '#type' => 'textfield',
107 '#title' => t('Field test field setting'),
108 '#default_value' => $settings['test_field_setting'],
109 '#required' => FALSE,
110 '#description' => t('A dummy form element to simulate field setting.'),
111 );
112
113 return $form;
114 }
115
116 /**
117 * Implements hook_field_instance_settings_form().
118 */
119 function field_test_field_instance_settings_form($field, $instance) {
120 $settings = $instance['settings'];
121
122 $form['test_instance_setting'] = array(
123 '#type' => 'textfield',
124 '#title' => t('Field test field instance setting'),
125 '#default_value' => $settings['test_instance_setting'],
126 '#required' => FALSE,
127 '#description' => t('A dummy form element to simulate field instance setting.'),
128 );
129
130 return $form;
131 }
132
133 /**
134 * Implements hook_field_widget_info().
135 */
136 function field_test_field_widget_info() {
137 return array(
138 'test_field_widget' => array(
139 'label' => t('Test field'),
140 'field types' => array('test_field', 'hidden_test_field'),
141 'settings' => array('test_widget_setting' => 'dummy test string'),
142 ),
143 'test_field_widget_multiple' => array(
144 'label' => t('Test field 1'),
145 'field types' => array('test_field'),
146 'settings' => array('test_widget_setting_multiple' => 'dummy test string'),
147 'behaviors' => array(
148 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
149 ),
150 ),
151 );
152 }
153
154 /**
155 * Implements hook_field_widget_form().
156 */
157 function field_test_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
158 switch ($instance['widget']['type']) {
159 case 'test_field_widget':
160 $element += array(
161 '#type' => 'textfield',
162 '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
163 );
164 return array('value' => $element);
165
166 case 'test_field_widget_multiple':
167 $values = array();
168 foreach ($items as $delta => $value) {
169 $values[] = $value['value'];
170 }
171 $element += array(
172 '#type' => 'textfield',
173 '#default_value' => implode(', ', $values),
174 '#element_validate' => array('field_test_widget_multiple_validate'),
175 );
176 return $element;
177 }
178 }
179
180 /**
181 * Form element validation handler for 'test_field_widget_multiple' widget.
182 */
183 function field_test_widget_multiple_validate($element, &$form_state) {
184 $values = array_map('trim', explode(',', $element['#value']));
185 $items = array();
186 foreach ($values as $value) {
187 $items[] = array('value' => $value);
188 }
189 form_set_value($element, $items, $form_state);
190 }
191
192 /**
193 * Implements hook_field_widget_error().
194 */
195 function field_test_field_widget_error($element, $error, $form, &$form_state) {
196 // @todo No easy way to differenciate widget types, we should receive it as a
197 // parameter.
198 if (isset($element['value'])) {
199 // Widget is test_field_widget.
200 $error_element = $element['value'];
201 }
202 else {
203 // Widget is test_field_widget_multiple.
204 $error_element = $element;
205 }
206
207 form_error($error_element, $error['message']);
208 }
209
210 /**
211 * Implements hook_field_widget_settings_form().
212 */
213 function field_test_field_widget_settings_form($field, $instance) {
214 $widget = $instance['widget'];
215 $settings = $widget['settings'];
216
217 $form['test_widget_setting'] = array(
218 '#type' => 'textfield',
219 '#title' => t('Field test field widget setting'),
220 '#default_value' => $settings['test_widget_setting'],
221 '#required' => FALSE,
222 '#description' => t('A dummy form element to simulate field widget setting.'),
223 );
224
225 return $form;
226 }
227
228 /**
229 * Implements hook_field_formatter_info().
230 */
231 function field_test_field_formatter_info() {
232 return array(
233 'field_test_default' => array(
234 'label' => t('Default'),
235 'description' => t('Default formatter'),
236 'field types' => array('test_field'),
237 'settings' => array(
238 'test_formatter_setting' => 'dummy test string',
239 ),
240 ),
241 'field_test_multiple' => array(
242 'label' => t('Multiple'),
243 'description' => t('Multiple formatter'),
244 'field types' => array('test_field'),
245 'settings' => array(
246 'test_formatter_setting_multiple' => 'dummy test string',
247 ),
248 ),
249 'field_test_with_prepare_view' => array(
250 'label' => t('Tests hook_field_formatter_prepare_view()'),
251 'field types' => array('test_field'),
252 'settings' => array(
253 'test_formatter_setting_additional' => 'dummy test string',
254 ),
255 ),
256 );
257 }
258
259 /**
260 * Implements hook_field_formatter_settings_form().
261 */
262 function field_test_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
263 $display = $instance['display'][$view_mode];
264 $settings = $display['settings'];
265
266 $element = array();
267
268 // The name of the setting depends on the formatter type.
269 $map = array(
270 'field_test_default' => 'test_formatter_setting',
271 'field_test_multiple' => 'test_formatter_setting_multiple',
272 'field_test_with_prepare_view' => 'test_formatter_setting_additional',
273 );
274
275 if (isset($map[$display['type']])) {
276 $name = $map[$display['type']];
277
278 $element[$name] = array(
279 '#title' => t('Setting'),
280 '#type' => 'textfield',
281 '#size' => 20,
282 '#default_value' => $settings[$name],
283 '#required' => TRUE,
284 );
285 }
286
287 return $element;
288 }
289
290 /**
291 * Implements hook_field_formatter_settings_summary().
292 */
293 function field_test_field_formatter_settings_summary($field, $instance, $view_mode) {
294 $display = $instance['display'][$view_mode];
295 $settings = $display['settings'];
296
297 $summary = '';
298
299 // The name of the setting depends on the formatter type.
300 $map = array(
301 'field_test_default' => 'test_formatter_setting',
302 'field_test_multiple' => 'test_formatter_setting_multiple',
303 'field_test_with_prepare_view' => 'test_formatter_setting_additional',
304 );
305
306 if (isset($map[$display['type']])) {
307 $name = $map[$display['type']];
308 $summary = t('@setting: @value', array('@setting' => $name, '@value' => $settings[$name]));
309 }
310
311 return $summary;
312 }
313
314 /**
315 * Implements hook_field_formatter_prepare_view().
316 */
317 function field_test_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
318 foreach ($items as $id => $item) {
319 // To keep the test non-intrusive, only act on the
320 // 'field_test_with_prepare_view' formatter.
321 if ($displays[$id]['type'] == 'field_test_with_prepare_view') {
322 foreach ($item as $delta => $value) {
323 // Don't add anything on empty values.
324 if ($value) {
325 $items[$id][$delta]['additional_formatter_value'] = $value['value'] + 1;
326 }
327 }
328 }
329 }
330 }
331
332 /**
333 * Implements hook_field_formatter_view().
334 */
335 function field_test_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
336 $element = array();
337 $settings = $display['settings'];
338
339 switch ($display['type']) {
340 case 'field_test_default':
341 foreach ($items as $delta => $item) {
342 $element[$delta] = array('#markup' => $settings['test_formatter_setting'] . '|' . $item['value']);
343 }
344 break;
345
346 case 'field_test_with_prepare_view':
347 foreach ($items as $delta => $item) {
348 $element[$delta] = array('#markup' => $settings['test_formatter_setting_additional'] . '|' . $item['value'] . '|' . $item['additional_formatter_value']);
349 }
350 break;
351
352 case 'field_test_multiple':
353 if (!empty($items)) {
354 $array = array();
355 foreach ($items as $delta => $item) {
356 $array[] = $delta . ':' . $item['value'];
357 }
358 $element[0] = array('#markup' => $settings['test_formatter_setting_multiple'] . '|' . implode('|', $array));
359 }
360 break;
361 }
362
363 return $element;
364 }
365
366 /**
367 * Sample 'default value' callback.
368 */
369 function field_test_default_value($entity_type, $entity, $field, $instance) {
370 return array(array('value' => 99));
371 }
372
373 /**
374 * Implements hook_field_access().
375 */
376 function field_test_field_access($op, $field, $entity_type, $entity, $account) {
377 if ($field['field_name'] == "field_no_{$op}_access") {
378 return FALSE;
379 }
380 return TRUE;
381 }