Project

General

Profile

Paste
Download (11.8 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / field / tests / field_test.field.inc @ c7768a53

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
        'foreign_key_name' => 'shape',
33
      ),
34
      'instance_settings' => array(),
35
      'default_widget' => 'test_field_widget',
36
      'default_formatter' => 'field_test_default',
37
    ),
38
    'hidden_test_field' => array(
39
      'no_ui' => TRUE,
40
      'label' => t('Hidden from UI test field'),
41
      'description' => t('Dummy hidden field type used for tests.'),
42
      'settings' => array(),
43
      'instance_settings' => array(),
44
      'default_widget' => 'test_field_widget',
45
      'default_formatter' => 'field_test_default',
46
    ),
47
  );
48
}
49

    
50
/**
51
 * Implements hook_field_update_forbid().
52
 */
53
function field_test_field_update_forbid($field, $prior_field, $has_data) {
54
  if ($field['type'] == 'test_field' && $field['settings']['unchangeable'] != $prior_field['settings']['unchangeable']) {
55
    throw new FieldException("field_test 'unchangeable' setting cannot be changed'");
56
  }
57
}
58

    
59
/**
60
 * Implements hook_field_load().
61
 */
62
function field_test_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
63
  $args = func_get_args();
64
  field_test_memorize(__FUNCTION__, $args);
65

    
66
  foreach ($items as $id => $item) {
67
    // To keep the test non-intrusive, only act for instances with the
68
    // test_hook_field_load setting explicitly set to TRUE.
69
    if ($instances[$id]['settings']['test_hook_field_load']) {
70
      foreach ($item as $delta => $value) {
71
        // Don't add anything on empty values.
72
        if ($value) {
73
          $items[$id][$delta]['additional_key'] = 'additional_value';
74
        }
75
      }
76
    }
77
  }
78
}
79

    
80
/**
81
 * Implements hook_field_insert().
82
 */
83
function field_test_field_insert($entity_type, $entity, $field, $instance, $items) {
84
  $args = func_get_args();
85
  field_test_memorize(__FUNCTION__, $args);
86
}
87

    
88
/**
89
 * Implements hook_field_update().
90
 */
91
function field_test_field_update($entity_type, $entity, $field, $instance, $items) {
92
  $args = func_get_args();
93
  field_test_memorize(__FUNCTION__, $args);
94
}
95

    
96
/**
97
 * Implements hook_field_delete().
98
 */
99
function field_test_field_delete($entity_type, $entity, $field, $instance, $items) {
100
  $args = func_get_args();
101
  field_test_memorize(__FUNCTION__, $args);
102
}
103

    
104
/**
105
 * Implements hook_field_validate().
106
 *
107
 * Possible error codes:
108
 * - 'field_test_invalid': The value is invalid.
109
 */
110
function field_test_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
111
  $args = func_get_args();
112
  field_test_memorize(__FUNCTION__, $args);
113

    
114
  foreach ($items as $delta => $item) {
115
    if ($item['value'] == -1) {
116
      $errors[$field['field_name']][$langcode][$delta][] = array(
117
        'error' => 'field_test_invalid',
118
        'message' => t('%name does not accept the value -1.', array('%name' => $instance['label'])),
119
      );
120
    }
121
  }
122
}
123

    
124
/**
125
 * Implements hook_field_is_empty().
126
 */
127
function field_test_field_is_empty($item, $field) {
128
  return empty($item['value']);
129
}
130

    
131
/**
132
 * Implements hook_field_settings_form().
133
 */
134
function field_test_field_settings_form($field, $instance, $has_data) {
135
  $settings = $field['settings'];
136

    
137
  $form['test_field_setting'] = array(
138
    '#type' => 'textfield',
139
    '#title' => t('Field test field setting'),
140
    '#default_value' => $settings['test_field_setting'],
141
    '#required' => FALSE,
142
    '#description' => t('A dummy form element to simulate field setting.'),
143
  );
144

    
145
  return $form;
146
}
147

    
148
/**
149
 * Implements hook_field_instance_settings_form().
150
 */
151
function field_test_field_instance_settings_form($field, $instance) {
152
  $settings = $instance['settings'];
153

    
154
  $form['test_instance_setting'] = array(
155
    '#type' => 'textfield',
156
    '#title' => t('Field test field instance setting'),
157
    '#default_value' => $settings['test_instance_setting'],
158
    '#required' => FALSE,
159
    '#description' => t('A dummy form element to simulate field instance setting.'),
160
  );
161

    
162
  return $form;
163
}
164

    
165
/**
166
 * Implements hook_field_widget_info().
167
 */
168
function field_test_field_widget_info() {
169
  return array(
170
    'test_field_widget' => array(
171
      'label' => t('Test field'),
172
      'field types' => array('test_field', 'hidden_test_field'),
173
      'settings' => array('test_widget_setting' => 'dummy test string'),
174
    ),
175
    'test_field_widget_multiple' => array(
176
      'label' => t('Test field 1'),
177
      'field types' => array('test_field'),
178
      'settings' => array('test_widget_setting_multiple' => 'dummy test string'),
179
      'behaviors' => array(
180
        'multiple values' => FIELD_BEHAVIOR_CUSTOM,
181
      ),
182
    ),
183
  );
184
}
185

    
186
/**
187
 * Implements hook_field_widget_form().
188
 */
189
function field_test_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
190
  switch ($instance['widget']['type']) {
191
    case 'test_field_widget':
192
      $element += array(
193
        '#type' => 'textfield',
194
        '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : '',
195
      );
196
      return array('value' => $element);
197

    
198
    case 'test_field_widget_multiple':
199
      $values = array();
200
      foreach ($items as $delta => $value) {
201
        $values[] = $value['value'];
202
      }
203
      $element += array(
204
        '#type' => 'textfield',
205
        '#default_value' => implode(', ', $values),
206
        '#element_validate' => array('field_test_widget_multiple_validate'),
207
      );
208
      return $element;
209
  }
210
}
211

    
212
/**
213
 * Form element validation handler for 'test_field_widget_multiple' widget.
214
 */
215
function field_test_widget_multiple_validate($element, &$form_state) {
216
  $values = array_map('trim', explode(',', $element['#value']));
217
  $items = array();
218
  foreach ($values as $value) {
219
    $items[] = array('value' => $value);
220
  }
221
  form_set_value($element, $items, $form_state);
222
}
223

    
224
/**
225
 * Implements hook_field_widget_error().
226
 */
227
function field_test_field_widget_error($element, $error, $form, &$form_state) {
228
  // @todo No easy way to differenciate widget types, we should receive it as a
229
  // parameter.
230
  if (isset($element['value'])) {
231
    // Widget is test_field_widget.
232
    $error_element = $element['value'];
233
  }
234
  else {
235
    // Widget is test_field_widget_multiple.
236
    $error_element = $element;
237
  }
238

    
239
  form_error($error_element, $error['message']);
240
}
241

    
242
/**
243
 * Implements hook_field_widget_settings_form().
244
 */
245
function field_test_field_widget_settings_form($field, $instance) {
246
  $widget = $instance['widget'];
247
  $settings = $widget['settings'];
248

    
249
  $form['test_widget_setting'] = array(
250
    '#type' => 'textfield',
251
    '#title' => t('Field test field widget setting'),
252
    '#default_value' => $settings['test_widget_setting'],
253
    '#required' => FALSE,
254
    '#description' => t('A dummy form element to simulate field widget setting.'),
255
  );
256

    
257
  return $form;
258
}
259

    
260
/**
261
 * Implements hook_field_formatter_info().
262
 */
263
function field_test_field_formatter_info() {
264
  return array(
265
    'field_test_default' => array(
266
      'label' => t('Default'),
267
      'description' => t('Default formatter'),
268
      'field types' => array('test_field'),
269
      'settings' => array(
270
        'test_formatter_setting' => 'dummy test string',
271
      ),
272
    ),
273
    'field_test_multiple' => array(
274
      'label' => t('Multiple'),
275
      'description' => t('Multiple formatter'),
276
      'field types' => array('test_field'),
277
      'settings' => array(
278
        'test_formatter_setting_multiple' => 'dummy test string',
279
      ),
280
    ),
281
    'field_test_with_prepare_view' => array(
282
      'label' => t('Tests hook_field_formatter_prepare_view()'),
283
      'field types' => array('test_field'),
284
      'settings' => array(
285
        'test_formatter_setting_additional' => 'dummy test string',
286
      ),
287
    ),
288
  );
289
}
290

    
291
/**
292
 * Implements hook_field_formatter_settings_form().
293
 */
294
function field_test_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
295
  $display = $instance['display'][$view_mode];
296
  $settings = $display['settings'];
297

    
298
  $element = array();
299

    
300
  // The name of the setting depends on the formatter type.
301
  $map = array(
302
    'field_test_default' => 'test_formatter_setting',
303
    'field_test_multiple' => 'test_formatter_setting_multiple',
304
    'field_test_with_prepare_view' => 'test_formatter_setting_additional',
305
  );
306

    
307
  if (isset($map[$display['type']])) {
308
    $name = $map[$display['type']];
309

    
310
    $element[$name] = array(
311
      '#title' => t('Setting'),
312
      '#type' => 'textfield',
313
      '#size' => 20,
314
      '#default_value' => $settings[$name],
315
      '#required' => TRUE,
316
    );
317
  }
318

    
319
  return $element;
320
}
321

    
322
/**
323
 * Implements hook_field_formatter_settings_summary().
324
 */
325
function field_test_field_formatter_settings_summary($field, $instance, $view_mode) {
326
  $display = $instance['display'][$view_mode];
327
  $settings = $display['settings'];
328

    
329
  $summary = '';
330

    
331
  // The name of the setting depends on the formatter type.
332
  $map = array(
333
    'field_test_default' => 'test_formatter_setting',
334
    'field_test_multiple' => 'test_formatter_setting_multiple',
335
    'field_test_with_prepare_view' => 'test_formatter_setting_additional',
336
  );
337

    
338
  if (isset($map[$display['type']])) {
339
    $name = $map[$display['type']];
340
    $summary = t('@setting: @value', array('@setting' => $name, '@value' => $settings[$name]));
341
  }
342

    
343
  return $summary;
344
}
345

    
346
/**
347
 * Implements hook_field_formatter_prepare_view().
348
 */
349
function field_test_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
350
  foreach ($items as $id => $item) {
351
    // To keep the test non-intrusive, only act on the
352
    // 'field_test_with_prepare_view' formatter.
353
    if ($displays[$id]['type'] == 'field_test_with_prepare_view') {
354
      foreach ($item as $delta => $value) {
355
        // Don't add anything on empty values.
356
        if ($value) {
357
          $items[$id][$delta]['additional_formatter_value'] = $value['value'] + 1;
358
        }
359
      }
360
    }
361
  }
362
}
363

    
364
/**
365
 * Implements hook_field_formatter_view().
366
 */
367
function field_test_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
368
  $element = array();
369
  $settings = $display['settings'];
370

    
371
  switch ($display['type']) {
372
    case 'field_test_default':
373
      foreach ($items as $delta => $item) {
374
        $element[$delta] = array('#markup' => $settings['test_formatter_setting'] . '|' . $item['value']);
375
      }
376
      break;
377

    
378
    case 'field_test_with_prepare_view':
379
      foreach ($items as $delta => $item) {
380
        $element[$delta] = array('#markup' => $settings['test_formatter_setting_additional'] . '|' . $item['value'] . '|' . $item['additional_formatter_value']);
381
      }
382
      break;
383

    
384
    case 'field_test_multiple':
385
      if (!empty($items)) {
386
        $array = array();
387
        foreach ($items as $delta => $item) {
388
          $array[] = $delta . ':' . $item['value'];
389
        }
390
        $element[0] = array('#markup' => $settings['test_formatter_setting_multiple'] . '|' . implode('|', $array));
391
      }
392
      break;
393
  }
394

    
395
  return $element;
396
}
397

    
398
/**
399
 * Sample 'default value' callback.
400
 */
401
function field_test_default_value($entity_type, $entity, $field, $instance) {
402
  return array(array('value' => 99));
403
}
404

    
405
/**
406
 * Implements hook_field_access().
407
 */
408
function field_test_field_access($op, $field, $entity_type, $entity, $account) {
409
  if ($field['field_name'] == "field_no_{$op}_access") {
410
    return FALSE;
411
  }
412
  return TRUE;
413
}