Projet

Général

Profil

Paste
Télécharger (96,6 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / field / field.api.php @ 4444412d

1
<?php
2
/**
3
 * @file
4
 * Hooks provided by the Field module.
5
 */
6

    
7
/**
8
 * @addtogroup hooks
9
 * @{
10
 */
11

    
12
/**
13
 * Exposes "pseudo-field" components on fieldable entities.
14
 *
15
 * Field UI's "Manage fields" and "Manage display" pages let users re-order
16
 * fields, but also non-field components. For nodes, these include the title,
17
 * poll choices, and other elements exposed by modules through hook_form() or
18
 * hook_form_alter().
19
 *
20
 * Fieldable entities or modules that want to have their components supported
21
 * should expose them using this hook. The user-defined settings (weight,
22
 * visible) are automatically applied on rendered forms and displayed
23
 * entities in a #pre_render callback added by field_attach_form() and
24
 * field_attach_view().
25
 *
26
 * @see _field_extra_fields_pre_render()
27
 * @see hook_field_extra_fields_alter()
28
 *
29
 * @return
30
 *   A nested array of 'pseudo-field' elements. Each list is nested within the
31
 *   following keys: entity type, bundle name, context (either 'form' or
32
 *   'display'). The keys are the name of the elements as appearing in the
33
 *   renderable array (either the entity form or the displayed entity). The
34
 *   value is an associative array:
35
 *   - label: The human readable name of the element.
36
 *   - description: A short description of the element contents.
37
 *   - weight: The default weight of the element.
38
 *   - edit: (optional) String containing markup (normally a link) used as the
39
 *     element's 'edit' operation in the administration interface. Only for
40
 *     'form' context.
41
 *   - delete: (optional) String containing markup (normally a link) used as the
42
 *     element's 'delete' operation in the administration interface. Only for
43
 *     'form' context.
44
 *
45
 * @ingroup field_types
46
 */
47
function hook_field_extra_fields() {
48
  $extra['node']['poll'] = array(
49
    'form' => array(
50
      'choice_wrapper' => array(
51
        'label' => t('Poll choices'),
52
        'description' => t('Poll choices'),
53
        'weight' => -4,
54
      ),
55
      'settings' => array(
56
        'label' => t('Poll settings'),
57
        'description' => t('Poll module settings'),
58
        'weight' => -3,
59
      ),
60
    ),
61
    'display' => array(
62
      'poll_view_voting' => array(
63
        'label' => t('Poll vote'),
64
        'description' => t('Poll vote'),
65
        'weight' => 0,
66
      ),
67
      'poll_view_results' => array(
68
        'label' => t('Poll results'),
69
        'description' => t('Poll results'),
70
        'weight' => 0,
71
      ),
72
    )
73
  );
74

    
75
  return $extra;
76
}
77

    
78
/**
79
 * Alter "pseudo-field" components on fieldable entities.
80
 *
81
 * @param $info
82
 *   The associative array of 'pseudo-field' components.
83
 *
84
 * @see hook_field_extra_fields()
85
 *
86
 * @ingroup field_types
87
 */
88
function hook_field_extra_fields_alter(&$info) {
89
  // Force node title to always be at the top of the list by default.
90
  foreach (node_type_get_types() as $bundle) {
91
    if (isset($info['node'][$bundle->type]['form']['title'])) {
92
      $info['node'][$bundle->type]['form']['title']['weight'] = -20;
93
    }
94
  }
95
}
96

    
97
/**
98
 * @defgroup field_types Field Types API
99
 * @{
100
 * Define field types.
101
 *
102
 * In the Field API, each field has a type, which determines what kind of data
103
 * (integer, string, date, etc.) the field can hold, which settings it provides,
104
 * and so on. The data type(s) accepted by a field are defined in
105
 * hook_field_schema(); other basic properties of a field are defined in
106
 * hook_field_info(). The other hooks below are called by the Field Attach API
107
 * to perform field-type-specific actions.
108
 *
109
 * The Field Types API also defines two kinds of pluggable handlers: widgets
110
 * and formatters. @link field_widget Widgets @endlink specify how the field
111
 * appears in edit forms, while @link field_formatter formatters @endlink
112
 * specify how the field appears in displayed entities.
113
 *
114
 * A third kind of pluggable handlers, storage backends, is defined by the
115
 * @link field_storage Field Storage API @endlink.
116
 *
117
 * See @link field Field API @endlink for information about the other parts of
118
 * the Field API.
119
 */
120

    
121
/**
122
 * Define Field API field types.
123
 *
124
 * Along with this hook, you also need to implement other hooks. See
125
 * @link field_types Field Types API @endlink for more information.
126
 *
127
 * @return
128
 *   An array whose keys are field type names and whose values are arrays
129
 *   describing the field type, with the following key/value pairs:
130
 *   - label: The human-readable name of the field type.
131
 *   - description: A short description for the field type.
132
 *   - settings: An array whose keys are the names of the settings available
133
 *     for the field type, and whose values are the default values for those
134
 *     settings.
135
 *   - instance_settings: An array whose keys are the names of the settings
136
 *     available for instances of the field type, and whose values are the
137
 *     default values for those settings. Instance-level settings can have
138
 *     different values on each field instance, and thus allow greater
139
 *     flexibility than field-level settings. It is recommended to put settings
140
 *     at the instance level whenever possible. Notable exceptions: settings
141
 *     acting on the schema definition, or settings that Views needs to use
142
 *     across field instances (for example, the list of allowed values).
143
 *   - default_widget: The machine name of the default widget to be used by
144
 *     instances of this field type, when no widget is specified in the
145
 *     instance definition. This widget must be available whenever the field
146
 *     type is available (i.e. provided by the field type module, or by a module
147
 *     the field type module depends on).
148
 *   - default_formatter: The machine name of the default formatter to be used
149
 *     by instances of this field type, when no formatter is specified in the
150
 *     instance definition. This formatter must be available whenever the field
151
 *     type is available (i.e. provided by the field type module, or by a module
152
 *     the field type module depends on).
153
 *   - no_ui: (optional) A boolean specifying that users should not be allowed
154
 *     to create fields and instances of this field type through the UI. Such
155
 *     fields can only be created programmatically with field_create_field()
156
 *     and field_create_instance(). Defaults to FALSE.
157
 *
158
 * @see hook_field_info_alter()
159
 */
160
function hook_field_info() {
161
  return array(
162
    'text' => array(
163
      'label' => t('Text'),
164
      'description' => t('This field stores varchar text in the database.'),
165
      'settings' => array('max_length' => 255),
166
      'instance_settings' => array('text_processing' => 0),
167
      'default_widget' => 'text_textfield',
168
      'default_formatter' => 'text_default',
169
    ),
170
    'text_long' => array(
171
      'label' => t('Long text'),
172
      'description' => t('This field stores long text in the database.'),
173
      'settings' => array('max_length' => ''),
174
      'instance_settings' => array('text_processing' => 0),
175
      'default_widget' => 'text_textarea',
176
      'default_formatter' => 'text_default',
177
    ),
178
    'text_with_summary' => array(
179
      'label' => t('Long text and summary'),
180
      'description' => t('This field stores long text in the database along with optional summary text.'),
181
      'settings' => array('max_length' => ''),
182
      'instance_settings' => array('text_processing' => 1, 'display_summary' => 0),
183
      'default_widget' => 'text_textarea_with_summary',
184
      'default_formatter' => 'text_summary_or_trimmed',
185
    ),
186
  );
187
}
188

    
189
/**
190
 * Perform alterations on Field API field types.
191
 *
192
 * @param $info
193
 *   Array of information on field types exposed by hook_field_info()
194
 *   implementations.
195
 */
196
function hook_field_info_alter(&$info) {
197
  // Add a setting to all field types.
198
  foreach ($info as $field_type => $field_type_info) {
199
    $info[$field_type]['settings'] += array(
200
      'mymodule_additional_setting' => 'default value',
201
    );
202
  }
203

    
204
  // Change the default widget for fields of type 'foo'.
205
  if (isset($info['foo'])) {
206
    $info['foo']['default widget'] = 'mymodule_widget';
207
  }
208
}
209

    
210
/**
211
 * Define the Field API schema for a field structure.
212
 *
213
 * This is invoked when a field is created, in order to obtain the database
214
 * schema from the module that defines the field's type.
215
 *
216
 * This hook must be defined in the module's .install file for it to be detected
217
 * during installation and upgrade.
218
 *
219
 * @param $field
220
 *   A field structure.
221
 *
222
 * @return
223
 *   An associative array with the following keys:
224
 *   - columns: An array of Schema API column specifications, keyed by column
225
 *     name. This specifies what comprises a value for a given field. For
226
 *     example, a value for a number field is simply 'value', while a value for
227
 *     a formatted text field is the combination of 'value' and 'format'. It is
228
 *     recommended to avoid having the column definitions depend on field
229
 *     settings when possible. No assumptions should be made on how storage
230
 *     engines internally use the original column name to structure their
231
 *     storage.
232
 *   - indexes: (optional) An array of Schema API indexes definitions. Only
233
 *     columns that appear in the 'columns' array are allowed. Those indexes
234
 *     will be used as default indexes. Callers of field_create_field() can
235
 *     specify additional indexes, or, at their own risk, modify the default
236
 *     indexes specified by the field-type module. Some storage engines might
237
 *     not support indexes.
238
 *   - foreign keys: (optional) An array of Schema API foreign keys
239
 *     definitions.
240
 */
241
function hook_field_schema($field) {
242
  if ($field['type'] == 'text_long') {
243
    $columns = array(
244
      'value' => array(
245
        'type' => 'text',
246
        'size' => 'big',
247
        'not null' => FALSE,
248
      ),
249
    );
250
  }
251
  else {
252
    $columns = array(
253
      'value' => array(
254
        'type' => 'varchar',
255
        'length' => $field['settings']['max_length'],
256
        'not null' => FALSE,
257
      ),
258
    );
259
  }
260
  $columns += array(
261
    'format' => array(
262
      'type' => 'varchar',
263
      'length' => 255,
264
      'not null' => FALSE,
265
    ),
266
  );
267
  return array(
268
    'columns' => $columns,
269
    'indexes' => array(
270
      'format' => array('format'),
271
    ),
272
    'foreign keys' => array(
273
      'format' => array(
274
        'table' => 'filter_format',
275
        'columns' => array('format' => 'format'),
276
      ),
277
    ),
278
  );
279
}
280

    
281
/**
282
 * Define custom load behavior for this module's field types.
283
 *
284
 * Unlike most other field hooks, this hook operates on multiple entities. The
285
 * $entities, $instances and $items parameters are arrays keyed by entity ID.
286
 * For performance reasons, information for all available entity should be
287
 * loaded in a single query where possible.
288
 *
289
 * Note that the changes made to the field values get cached by the field cache
290
 * for subsequent loads. You should never use this hook to load fieldable
291
 * entities, since this is likely to cause infinite recursions when
292
 * hook_field_load() is run on those as well. Use
293
 * hook_field_formatter_prepare_view() instead.
294
 *
295
 * Make changes or additions to field values by altering the $items parameter by
296
 * reference. There is no return value.
297
 *
298
 * @param $entity_type
299
 *   The type of $entity.
300
 * @param $entities
301
 *   Array of entities being loaded, keyed by entity ID.
302
 * @param $field
303
 *   The field structure for the operation.
304
 * @param $instances
305
 *   Array of instance structures for $field for each entity, keyed by entity
306
 *   ID.
307
 * @param $langcode
308
 *   The language code associated with $items.
309
 * @param $items
310
 *   Array of field values already loaded for the entities, keyed by entity ID.
311
 *   Store your changes in this parameter (passed by reference).
312
 * @param $age
313
 *   FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
314
 *   FIELD_LOAD_REVISION to load the version indicated by each entity.
315
 */
316
function hook_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
317
  // Sample code from text.module: precompute sanitized strings so they are
318
  // stored in the field cache.
319
  foreach ($entities as $id => $entity) {
320
    foreach ($items[$id] as $delta => $item) {
321
      // Only process items with a cacheable format, the rest will be handled
322
      // by formatters if needed.
323
      if (empty($instances[$id]['settings']['text_processing']) || filter_format_allowcache($item['format'])) {
324
        $items[$id][$delta]['safe_value'] = isset($item['value']) ? _text_sanitize($instances[$id], $langcode, $item, 'value') : '';
325
        if ($field['type'] == 'text_with_summary') {
326
          $items[$id][$delta]['safe_summary'] = isset($item['summary']) ? _text_sanitize($instances[$id], $langcode, $item, 'summary') : '';
327
        }
328
      }
329
    }
330
  }
331
}
332

    
333
/**
334
 * Prepare field values prior to display.
335
 *
336
 * This hook is invoked before the field values are handed to formatters
337
 * for display, and runs before the formatters' own
338
 * hook_field_formatter_prepare_view().
339
 *
340
 * Unlike most other field hooks, this hook operates on multiple entities. The
341
 * $entities, $instances and $items parameters are arrays keyed by entity ID.
342
 * For performance reasons, information for all available entities should be
343
 * loaded in a single query where possible.
344
 *
345
 * Make changes or additions to field values by altering the $items parameter by
346
 * reference. There is no return value.
347
 *
348
 * @param $entity_type
349
 *   The type of $entity.
350
 * @param $entities
351
 *   Array of entities being displayed, keyed by entity ID.
352
 * @param $field
353
 *   The field structure for the operation.
354
 * @param $instances
355
 *   Array of instance structures for $field for each entity, keyed by entity
356
 *   ID.
357
 * @param $langcode
358
 *   The language associated to $items.
359
 * @param $items
360
 *   $entity->{$field['field_name']}, or an empty array if unset.
361
 */
362
function hook_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {
363
  // Sample code from image.module: if there are no images specified at all,
364
  // use the default image.
365
  foreach ($entities as $id => $entity) {
366
    if (empty($items[$id]) && $field['settings']['default_image']) {
367
      if ($file = file_load($field['settings']['default_image'])) {
368
        $items[$id][0] = (array) $file + array(
369
          'is_default' => TRUE,
370
          'alt' => '',
371
          'title' => '',
372
        );
373
      }
374
    }
375
  }
376
}
377

    
378
/**
379
 * Validate this module's field data.
380
 *
381
 * If there are validation problems, add to the $errors array (passed by
382
 * reference). There is no return value.
383
 *
384
 * @param $entity_type
385
 *   The type of $entity.
386
 * @param $entity
387
 *   The entity for the operation.
388
 * @param $field
389
 *   The field structure for the operation.
390
 * @param $instance
391
 *   The instance structure for $field on $entity's bundle.
392
 * @param $langcode
393
 *   The language associated with $items.
394
 * @param $items
395
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
396
 * @param $errors
397
 *   The array of errors (keyed by field name, language code, and delta) that
398
 *   have already been reported for the entity. The function should add its
399
 *   errors to this array. Each error is an associative array with the following
400
 *   keys and values:
401
 *   - error: An error code (should be a string prefixed with the module name).
402
 *   - message: The human readable message to be displayed.
403
 */
404
function hook_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
405
  foreach ($items as $delta => $item) {
406
    if (!empty($item['value'])) {
407
      if (!empty($field['settings']['max_length']) && drupal_strlen($item['value']) > $field['settings']['max_length']) {
408
        $errors[$field['field_name']][$langcode][$delta][] = array(
409
          'error' => 'text_max_length',
410
          'message' => t('%name: the value may not be longer than %max characters.', array('%name' => $instance['label'], '%max' => $field['settings']['max_length'])),
411
        );
412
      }
413
    }
414
  }
415
}
416

    
417
/**
418
 * Define custom presave behavior for this module's field types.
419
 *
420
 * Make changes or additions to field values by altering the $items parameter by
421
 * reference. There is no return value.
422
 *
423
 * @param $entity_type
424
 *   The type of $entity.
425
 * @param $entity
426
 *   The entity for the operation.
427
 * @param $field
428
 *   The field structure for the operation.
429
 * @param $instance
430
 *   The instance structure for $field on $entity's bundle.
431
 * @param $langcode
432
 *   The language associated with $items.
433
 * @param $items
434
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
435
 */
436
function hook_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
437
  if ($field['type'] == 'number_decimal') {
438
    // Let PHP round the value to ensure consistent behavior across storage
439
    // backends.
440
    foreach ($items as $delta => $item) {
441
      if (isset($item['value'])) {
442
        $items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
443
      }
444
    }
445
  }
446
}
447

    
448
/**
449
 * Define custom insert behavior for this module's field data.
450
 *
451
 * This hook is invoked from field_attach_insert() on the module that defines a
452
 * field, during the process of inserting an entity object (node, taxonomy term,
453
 * etc.). It is invoked just before the data for this field on the particular
454
 * entity object is inserted into field storage. Only field modules that are
455
 * storing or tracking information outside the standard field storage mechanism
456
 * need to implement this hook.
457
 *
458
 * @param $entity_type
459
 *   The type of $entity.
460
 * @param $entity
461
 *   The entity for the operation.
462
 * @param $field
463
 *   The field structure for the operation.
464
 * @param $instance
465
 *   The instance structure for $field on $entity's bundle.
466
 * @param $langcode
467
 *   The language associated with $items.
468
 * @param $items
469
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
470
 *
471
 * @see hook_field_update()
472
 * @see hook_field_delete()
473
 */
474
function hook_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
475
  if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node' && $entity->status) {
476
    $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created', ));
477
    foreach ($items as $item) {
478
      $query->values(array(
479
        'nid' => $entity->nid,
480
        'tid' => $item['tid'],
481
        'sticky' => $entity->sticky,
482
        'created' => $entity->created,
483
      ));
484
    }
485
    $query->execute();
486
  }
487
}
488

    
489
/**
490
 * Define custom update behavior for this module's field data.
491
 *
492
 * This hook is invoked from field_attach_update() on the module that defines a
493
 * field, during the process of updating an entity object (node, taxonomy term,
494
 * etc.). It is invoked just before the data for this field on the particular
495
 * entity object is updated into field storage. Only field modules that are
496
 * storing or tracking information outside the standard field storage mechanism
497
 * need to implement this hook.
498
 *
499
 * @param $entity_type
500
 *   The type of $entity.
501
 * @param $entity
502
 *   The entity for the operation.
503
 * @param $field
504
 *   The field structure for the operation.
505
 * @param $instance
506
 *   The instance structure for $field on $entity's bundle.
507
 * @param $langcode
508
 *   The language associated with $items.
509
 * @param $items
510
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
511
 *
512
 * @see hook_field_insert()
513
 * @see hook_field_delete()
514
 */
515
function hook_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
516
  if (variable_get('taxonomy_maintain_index_table', TRUE) && $field['storage']['type'] == 'field_sql_storage' && $entity_type == 'node') {
517
    $first_call = &drupal_static(__FUNCTION__, array());
518

    
519
    // We don't maintain data for old revisions, so clear all previous values
520
    // from the table. Since this hook runs once per field, per object, make
521
    // sure we only wipe values once.
522
    if (!isset($first_call[$entity->nid])) {
523
      $first_call[$entity->nid] = FALSE;
524
      db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute();
525
    }
526
    // Only save data to the table if the node is published.
527
    if ($entity->status) {
528
      $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
529
      foreach ($items as $item) {
530
        $query->values(array(
531
          'nid' => $entity->nid,
532
          'tid' => $item['tid'],
533
          'sticky' => $entity->sticky,
534
          'created' => $entity->created,
535
        ));
536
      }
537
      $query->execute();
538
    }
539
  }
540
}
541

    
542
/**
543
 * Update the storage information for a field.
544
 *
545
 * This is invoked on the field's storage module from field_update_field(),
546
 * before the new field information is saved to the database. The field storage
547
 * module should update its storage tables to agree with the new field
548
 * information. If there is a problem, the field storage module should throw an
549
 * exception.
550
 *
551
 * @param $field
552
 *   The updated field structure to be saved.
553
 * @param $prior_field
554
 *   The previously-saved field structure.
555
 * @param $has_data
556
 *   TRUE if the field has data in storage currently.
557
 */
558
function hook_field_storage_update_field($field, $prior_field, $has_data) {
559
  if (!$has_data) {
560
    // There is no data. Re-create the tables completely.
561
    $prior_schema = _field_sql_storage_schema($prior_field);
562
    foreach ($prior_schema as $name => $table) {
563
      db_drop_table($name, $table);
564
    }
565
    $schema = _field_sql_storage_schema($field);
566
    foreach ($schema as $name => $table) {
567
      db_create_table($name, $table);
568
    }
569
  }
570
  else {
571
    // There is data. See field_sql_storage_field_storage_update_field() for
572
    // an example of what to do to modify the schema in place, preserving the
573
    // old data as much as possible.
574
  }
575
  drupal_get_schema(NULL, TRUE);
576
}
577

    
578
/**
579
 * Define custom delete behavior for this module's field data.
580
 *
581
 * This hook is invoked from field_attach_delete() on the module that defines a
582
 * field, during the process of deleting an entity object (node, taxonomy term,
583
 * etc.). It is invoked just before the data for this field on the particular
584
 * entity object is deleted from field storage. Only field modules that are
585
 * storing or tracking information outside the standard field storage mechanism
586
 * need to implement this hook.
587
 *
588
 * @param $entity_type
589
 *   The type of $entity.
590
 * @param $entity
591
 *   The entity for the operation.
592
 * @param $field
593
 *   The field structure for the operation.
594
 * @param $instance
595
 *   The instance structure for $field on $entity's bundle.
596
 * @param $langcode
597
 *   The language associated with $items.
598
 * @param $items
599
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
600
 *
601
 * @see hook_field_insert()
602
 * @see hook_field_update()
603
 */
604
function hook_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
605
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
606
  foreach ($items as $delta => $item) {
607
    // For hook_file_references(), remember that this is being deleted.
608
    $item['file_field_name'] = $field['field_name'];
609
    // Pass in the ID of the object that is being removed so all references can
610
    // be counted in hook_file_references().
611
    $item['file_field_type'] = $entity_type;
612
    $item['file_field_id'] = $id;
613
    file_field_delete_file($item, $field, $entity_type, $id);
614
  }
615
}
616

    
617
/**
618
 * Define custom revision delete behavior for this module's field types.
619
 *
620
 * This hook is invoked just before the data is deleted from field storage
621
 * in field_attach_delete_revision(), and will only be called for fieldable
622
 * types that are versioned.
623
 *
624
 * @param $entity_type
625
 *   The type of $entity.
626
 * @param $entity
627
 *   The entity for the operation.
628
 * @param $field
629
 *   The field structure for the operation.
630
 * @param $instance
631
 *   The instance structure for $field on $entity's bundle.
632
 * @param $langcode
633
 *   The language associated with $items.
634
 * @param $items
635
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
636
 */
637
function hook_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
638
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
639
  foreach ($items as $delta => $item) {
640
    // For hook_file_references, remember that this file is being deleted.
641
    $item['file_field_name'] = $field['field_name'];
642
    if (file_field_delete_file($item, $field, $entity_type, $id)) {
643
      $items[$delta] = NULL;
644
    }
645
  }
646
}
647

    
648
/**
649
 * Define custom prepare_translation behavior for this module's field types.
650
 *
651
 * @param $entity_type
652
 *   The type of $entity.
653
 * @param $entity
654
 *   The entity for the operation.
655
 * @param $field
656
 *   The field structure for the operation.
657
 * @param $instance
658
 *   The instance structure for $field on $entity's bundle.
659
 * @param $langcode
660
 *   The language associated to $items.
661
 * @param $items
662
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
663
 * @param $source_entity
664
 *   The source entity from which field values are being copied.
665
 * @param $source_langcode
666
 *   The source language from which field values are being copied.
667
 *
668
 * @ingroup field_language
669
 */
670
function hook_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
671
  // If the translating user is not permitted to use the assigned text format,
672
  // we must not expose the source values.
673
  $field_name = $field['field_name'];
674
  $formats = filter_formats();
675
  $format_id = $source_entity->{$field_name}[$source_langcode][0]['format'];
676
  if (!filter_access($formats[$format_id])) {
677
    $items = array();
678
  }
679
}
680

    
681
/**
682
 * Define what constitutes an empty item for a field type.
683
 *
684
 * @param $item
685
 *   An item that may or may not be empty.
686
 * @param $field
687
 *   The field to which $item belongs.
688
 *
689
 * @return
690
 *   TRUE if $field's type considers $item not to contain any data;
691
 *   FALSE otherwise.
692
 */
693
function hook_field_is_empty($item, $field) {
694
  if (empty($item['value']) && (string) $item['value'] !== '0') {
695
    return TRUE;
696
  }
697
  return FALSE;
698
}
699

    
700
/**
701
 * @} End of "defgroup field_types".
702
 */
703

    
704
/**
705
 * @defgroup field_widget Field Widget API
706
 * @{
707
 * Define Field API widget types.
708
 *
709
 * Field API widgets specify how fields are displayed in edit forms. Fields of a
710
 * given @link field_types field type @endlink may be edited using more than one
711
 * widget. In this case, the Field UI module allows the site builder to choose
712
 * which widget to use. Widget types are defined by implementing
713
 * hook_field_widget_info().
714
 *
715
 * Widgets are @link forms_api_reference.html Form API @endlink elements with
716
 * additional processing capabilities. Widget hooks are typically called by the
717
 * Field Attach API during the creation of the field form structure with
718
 * field_attach_form().
719
 *
720
 * @see field
721
 * @see field_types
722
 * @see field_formatter
723
 */
724

    
725
/**
726
 * Expose Field API widget types.
727
 *
728
 * @return
729
 *   An array describing the widget types implemented by the module.
730
 *   The keys are widget type names. To avoid name clashes, widget type
731
 *   names should be prefixed with the name of the module that exposes them.
732
 *   The values are arrays describing the widget type, with the following
733
 *   key/value pairs:
734
 *   - label: The human-readable name of the widget type.
735
 *   - description: A short description for the widget type.
736
 *   - field types: An array of field types the widget supports.
737
 *   - settings: An array whose keys are the names of the settings available
738
 *     for the widget type, and whose values are the default values for those
739
 *     settings.
740
 *   - behaviors: (optional) An array describing behaviors of the widget, with
741
 *     the following elements:
742
 *     - multiple values: One of the following constants:
743
 *       - FIELD_BEHAVIOR_DEFAULT: (default) If the widget allows the input of
744
 *         one single field value (most common case). The widget will be
745
 *         repeated for each value input.
746
 *       - FIELD_BEHAVIOR_CUSTOM: If one single copy of the widget can receive
747
 *         several field values. Examples: checkboxes, multiple select,
748
 *         comma-separated textfield.
749
 *     - default value: One of the following constants:
750
 *       - FIELD_BEHAVIOR_DEFAULT: (default) If the widget accepts default
751
 *         values.
752
 *       - FIELD_BEHAVIOR_NONE: if the widget does not support default values.
753
 *   - weight: (optional) An integer to determine the weight of this widget
754
 *     relative to other widgets in the Field UI when selecting a widget for a
755
 *     given field instance.
756
 *
757
 * @see hook_field_widget_info_alter()
758
 * @see hook_field_widget_form()
759
 * @see hook_field_widget_form_alter()
760
 * @see hook_field_widget_WIDGET_TYPE_form_alter()
761
 * @see hook_field_widget_error()
762
 * @see hook_field_widget_settings_form()
763
 */
764
function hook_field_widget_info() {
765
  return array(
766
    'text_textfield' => array(
767
      'label' => t('Text field'),
768
      'field types' => array('text'),
769
      'settings' => array('size' => 60),
770
      'behaviors' => array(
771
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
772
        'default value' => FIELD_BEHAVIOR_DEFAULT,
773
      ),
774
    ),
775
    'text_textarea' => array(
776
      'label' => t('Text area (multiple rows)'),
777
      'field types' => array('text_long'),
778
      'settings' => array('rows' => 5),
779
      'behaviors' => array(
780
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
781
        'default value' => FIELD_BEHAVIOR_DEFAULT,
782
      ),
783
    ),
784
    'text_textarea_with_summary' => array(
785
      'label' => t('Text area with a summary'),
786
      'field types' => array('text_with_summary'),
787
      'settings' => array('rows' => 20, 'summary_rows' => 5),
788
      'behaviors' => array(
789
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
790
        'default value' => FIELD_BEHAVIOR_DEFAULT,
791
      ),
792
      // As an advanced widget, force it to sink to the bottom of the choices.
793
      'weight' => 2,
794
    ),
795
  );
796
}
797

    
798
/**
799
 * Perform alterations on Field API widget types.
800
 *
801
 * @param $info
802
 *   Array of informations on widget types exposed by hook_field_widget_info()
803
 *   implementations.
804
 */
805
function hook_field_widget_info_alter(&$info) {
806
  // Add a setting to a widget type.
807
  $info['text_textfield']['settings'] += array(
808
    'mymodule_additional_setting' => 'default value',
809
  );
810

    
811
  // Let a new field type re-use an existing widget.
812
  $info['options_select']['field types'][] = 'my_field_type';
813
}
814

    
815
/**
816
 * Return the form for a single field widget.
817
 *
818
 * Field widget form elements should be based on the passed-in $element, which
819
 * contains the base form element properties derived from the field
820
 * configuration.
821
 *
822
 * Field API will set the weight, field name and delta values for each form
823
 * element. If there are multiple values for this field, the Field API will
824
 * invoke this hook as many times as needed.
825
 *
826
 * Note that, depending on the context in which the widget is being included
827
 * (regular entity form, field configuration form, advanced search form...),
828
 * the values for $field and $instance might be different from the "official"
829
 * definitions returned by field_info_field() and field_info_instance().
830
 * Examples: mono-value widget even if the field is multi-valued, non-required
831
 * widget even if the field is 'required'...
832
 *
833
 * Therefore, the FAPI element callbacks (such as #process, #element_validate,
834
 * #value_callback...) used by the widget cannot use the field_info_field()
835
 * or field_info_instance() functions to retrieve the $field or $instance
836
 * definitions they should operate on. The field_widget_field() and
837
 * field_widget_instance() functions should be used instead to fetch the
838
 * current working definitions from $form_state, where Field API stores them.
839
 *
840
 * Alternatively, hook_field_widget_form() can extract the needed specific
841
 * properties from $field and $instance and set them as ad-hoc
842
 * $element['#custom'] properties, for later use by its element callbacks.
843
 *
844
 * Other modules may alter the form element provided by this function using
845
 * hook_field_widget_form_alter().
846
 *
847
 * @param $form
848
 *   The form structure where widgets are being attached to. This might be a
849
 *   full form structure, or a sub-element of a larger form.
850
 * @param $form_state
851
 *   An associative array containing the current state of the form.
852
 * @param $field
853
 *   The field structure.
854
 * @param $instance
855
 *   The field instance.
856
 * @param $langcode
857
 *   The language associated with $items.
858
 * @param $items
859
 *   Array of default values for this field.
860
 * @param $delta
861
 *   The order of this item in the array of subelements (0, 1, 2, etc).
862
 * @param $element
863
 *   A form element array containing basic properties for the widget:
864
 *   - #entity_type: The name of the entity the field is attached to.
865
 *   - #bundle: The name of the field bundle the field is contained in.
866
 *   - #field_name: The name of the field.
867
 *   - #language: The language the field is being edited in.
868
 *   - #field_parents: The 'parents' space for the field in the form. Most
869
 *       widgets can simply overlook this property. This identifies the
870
 *       location where the field values are placed within
871
 *       $form_state['values'], and is used to access processing information
872
 *       for the field through the field_form_get_state() and
873
 *       field_form_set_state() functions.
874
 *   - #columns: A list of field storage columns of the field.
875
 *   - #title: The sanitized element label for the field instance, ready for
876
 *     output.
877
 *   - #description: The sanitized element description for the field instance,
878
 *     ready for output.
879
 *   - #required: A Boolean indicating whether the element value is required;
880
 *     for required multiple value fields, only the first widget's values are
881
 *     required.
882
 *   - #delta: The order of this item in the array of subelements; see $delta
883
 *     above.
884
 *
885
 * @return
886
 *   The form elements for a single widget for this field.
887
 *
888
 * @see field_widget_field()
889
 * @see field_widget_instance()
890
 * @see hook_field_widget_form_alter()
891
 * @see hook_field_widget_WIDGET_TYPE_form_alter()
892
 */
893
function hook_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
894
  $element += array(
895
    '#type' => $instance['widget']['type'],
896
    '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
897
  );
898
  return array('value' => $element);
899
}
900

    
901
/**
902
 * Alter forms for field widgets provided by other modules.
903
 *
904
 * @param $element
905
 *   The field widget form element as constructed by hook_field_widget_form().
906
 * @param $form_state
907
 *   An associative array containing the current state of the form.
908
 * @param $context
909
 *   An associative array containing the following key-value pairs, matching the
910
 *   arguments received by hook_field_widget_form():
911
 *   - form: The form structure to which widgets are being attached. This may be
912
 *     a full form structure, or a sub-element of a larger form.
913
 *   - field: The field structure.
914
 *   - instance: The field instance structure.
915
 *   - langcode: The language associated with $items.
916
 *   - items: Array of default values for this field.
917
 *   - delta: The order of this item in the array of subelements (0, 1, 2, etc).
918
 *
919
 * @see hook_field_widget_form()
920
 * @see hook_field_widget_WIDGET_TYPE_form_alter()
921
 */
922
function hook_field_widget_form_alter(&$element, &$form_state, $context) {
923
  // Add a css class to widget form elements for all fields of type mytype.
924
  if ($context['field']['type'] == 'mytype') {
925
    // Be sure not to overwrite existing attributes.
926
    $element['#attributes']['class'][] = 'myclass';
927
  }
928
}
929

    
930
/**
931
 * Alter widget forms for a specific widget provided by another module.
932
 *
933
 * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a
934
 * specific widget form, rather than using hook_field_widget_form_alter() and
935
 * checking the widget type.
936
 *
937
 * @param $element
938
 *   The field widget form element as constructed by hook_field_widget_form().
939
 * @param $form_state
940
 *   An associative array containing the current state of the form.
941
 * @param $context
942
 *   An associative array containing the following key-value pairs, matching the
943
 *   arguments received by hook_field_widget_form():
944
 *   - "form": The form structure where widgets are being attached to. This
945
 *     might be a full form structure, or a sub-element of a larger form.
946
 *   - "field": The field structure.
947
 *   - "instance": The field instance structure.
948
 *   - "langcode": The language associated with $items.
949
 *   - "items": Array of default values for this field.
950
 *   - "delta": The order of this item in the array of subelements (0, 1, 2,
951
 *     etc).
952
 *
953
 * @see hook_field_widget_form()
954
 * @see hook_field_widget_form_alter()
955
 */
956
function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context) {
957
  // Code here will only act on widgets of type WIDGET_TYPE.  For example,
958
  // hook_field_widget_mymodule_autocomplete_form_alter() will only act on
959
  // widgets of type 'mymodule_autocomplete'.
960
  $element['#autocomplete_path'] = 'mymodule/autocomplete_path';
961
}
962

    
963
/**
964
 * Alters the widget properties of a field instance before it gets displayed.
965
 *
966
 * Note that instead of hook_field_widget_properties_alter(), which is called
967
 * for all fields on all entity types,
968
 * hook_field_widget_properties_ENTITY_TYPE_alter() may be used to alter widget
969
 * properties for fields on a specific entity type only.
970
 *
971
 * This hook is called once per field per added or edit entity. If the result
972
 * of the hook involves reading from the database, it is highly recommended to
973
 * statically cache the information.
974
 *
975
 * @param $widget
976
 *   The instance's widget properties.
977
 * @param $context
978
 *   An associative array containing:
979
 *   - entity_type: The entity type; e.g., 'node' or 'user'.
980
 *   - entity: The entity object.
981
 *   - field: The field that the widget belongs to.
982
 *   - instance: The instance of the field.
983
 *
984
 * @see hook_field_widget_properties_ENTITY_TYPE_alter()
985
 */
986
function hook_field_widget_properties_alter(&$widget, $context) {
987
  // Change a widget's type according to the time of day.
988
  $field = $context['field'];
989
  if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_foo') {
990
    $time = date('H');
991
    $widget['type'] = $time < 12 ? 'widget_am' : 'widget_pm';
992
  }
993
}
994

    
995
/**
996
 * Flag a field-level validation error.
997
 *
998
 * @param $element
999
 *   An array containing the form element for the widget. The error needs to be
1000
 *   flagged on the right sub-element, according to the widget's internal
1001
 *   structure.
1002
 * @param $error
1003
 *   An associative array with the following key-value pairs, as returned by
1004
 *   hook_field_validate():
1005
 *   - error: the error code. Complex widgets might need to report different
1006
 *     errors to different form elements inside the widget.
1007
 *   - message: the human readable message to be displayed.
1008
 * @param $form
1009
 *   The form structure where field elements are attached to. This might be a
1010
 *   full form structure, or a sub-element of a larger form.
1011
 * @param $form_state
1012
 *   An associative array containing the current state of the form.
1013
 */
1014
function hook_field_widget_error($element, $error, $form, &$form_state) {
1015
  form_error($element, $error['message']);
1016
}
1017

    
1018

    
1019
/**
1020
 * @} End of "defgroup field_widget".
1021
 */
1022

    
1023

    
1024
/**
1025
 * @defgroup field_formatter Field Formatter API
1026
 * @{
1027
 * Define Field API formatter types.
1028
 *
1029
 * Field API formatters specify how fields are displayed when the entity to
1030
 * which the field is attached is displayed. Fields of a given
1031
 * @link field_types field type @endlink may be displayed using more than one
1032
 * formatter. In this case, the Field UI module allows the site builder to
1033
 * choose which formatter to use. Field formatters are defined by implementing
1034
 * hook_field_formatter_info().
1035
 *
1036
 * @see field
1037
 * @see field_types
1038
 * @see field_widget
1039
 */
1040

    
1041
/**
1042
 * Expose Field API formatter types.
1043
 *
1044
 * Formatters handle the display of field values. Formatter hooks are typically
1045
 * called by the Field Attach API field_attach_prepare_view() and
1046
 * field_attach_view() functions.
1047
 *
1048
 * @return
1049
 *   An array describing the formatter types implemented by the module.
1050
 *   The keys are formatter type names. To avoid name clashes, formatter type
1051
 *   names should be prefixed with the name of the module that exposes them.
1052
 *   The values are arrays describing the formatter type, with the following
1053
 *   key/value pairs:
1054
 *   - label: The human-readable name of the formatter type.
1055
 *   - description: A short description for the formatter type.
1056
 *   - field types: An array of field types the formatter supports.
1057
 *   - settings: An array whose keys are the names of the settings available
1058
 *     for the formatter type, and whose values are the default values for
1059
 *     those settings.
1060
 *
1061
 * @see hook_field_formatter_info_alter()
1062
 * @see hook_field_formatter_view()
1063
 * @see hook_field_formatter_prepare_view()
1064
 */
1065
function hook_field_formatter_info() {
1066
  return array(
1067
    'text_default' => array(
1068
      'label' => t('Default'),
1069
      'field types' => array('text', 'text_long', 'text_with_summary'),
1070
    ),
1071
    'text_plain' => array(
1072
      'label' => t('Plain text'),
1073
      'field types' => array('text', 'text_long', 'text_with_summary'),
1074
    ),
1075

    
1076
    // The text_trimmed formatter displays the trimmed version of the
1077
    // full element of the field. It is intended to be used with text
1078
    // and text_long fields. It also works with text_with_summary
1079
    // fields though the text_summary_or_trimmed formatter makes more
1080
    // sense for that field type.
1081
    'text_trimmed' => array(
1082
      'label' => t('Trimmed'),
1083
      'field types' => array('text', 'text_long', 'text_with_summary'),
1084
    ),
1085

    
1086
    // The 'summary or trimmed' field formatter for text_with_summary
1087
    // fields displays returns the summary element of the field or, if
1088
    // the summary is empty, the trimmed version of the full element
1089
    // of the field.
1090
    'text_summary_or_trimmed' => array(
1091
      'label' => t('Summary or trimmed'),
1092
      'field types' => array('text_with_summary'),
1093
    ),
1094
  );
1095
}
1096

    
1097
/**
1098
 * Perform alterations on Field API formatter types.
1099
 *
1100
 * @param $info
1101
 *   An array of information on formatter types exposed by
1102
 *   hook_field_formatter_info() implementations.
1103
 */
1104
function hook_field_formatter_info_alter(&$info) {
1105
  // Add a setting to a formatter type.
1106
  $info['text_default']['settings'] += array(
1107
    'mymodule_additional_setting' => 'default value',
1108
  );
1109

    
1110
  // Let a new field type re-use an existing formatter.
1111
  $info['text_default']['field types'][] = 'my_field_type';
1112
}
1113

    
1114
/**
1115
 * Allow formatters to load information for field values being displayed.
1116
 *
1117
 * This should be used when a formatter needs to load additional information
1118
 * from the database in order to render a field, for example a reference field
1119
 * which displays properties of the referenced entities such as name or type.
1120
 *
1121
 * This hook is called after the field type's own hook_field_prepare_view().
1122
 *
1123
 * Unlike most other field hooks, this hook operates on multiple entities. The
1124
 * $entities, $instances and $items parameters are arrays keyed by entity ID.
1125
 * For performance reasons, information for all available entities should be
1126
 * loaded in a single query where possible.
1127
 *
1128
 * @param $entity_type
1129
 *   The type of $entity.
1130
 * @param $entities
1131
 *   Array of entities being displayed, keyed by entity ID.
1132
 * @param $field
1133
 *   The field structure for the operation.
1134
 * @param $instances
1135
 *   Array of instance structures for $field for each entity, keyed by entity
1136
 *   ID.
1137
 * @param $langcode
1138
 *   The language the field values are to be shown in. If no language is
1139
 *   provided the current language is used.
1140
 * @param $items
1141
 *   Array of field values for the entities, keyed by entity ID.
1142
 * @param $displays
1143
 *   Array of display settings to use for each entity, keyed by entity ID.
1144
 *
1145
 * @return
1146
 *   Changes or additions to field values are done by altering the $items
1147
 *   parameter by reference.
1148
 */
1149
function hook_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
1150
  $tids = array();
1151

    
1152
  // Collect every possible term attached to any of the fieldable entities.
1153
  foreach ($entities as $id => $entity) {
1154
    foreach ($items[$id] as $delta => $item) {
1155
      // Force the array key to prevent duplicates.
1156
      $tids[$item['tid']] = $item['tid'];
1157
    }
1158
  }
1159

    
1160
  if ($tids) {
1161
    $terms = taxonomy_term_load_multiple($tids);
1162

    
1163
    // Iterate through the fieldable entities again to attach the loaded term
1164
    // data.
1165
    foreach ($entities as $id => $entity) {
1166
      $rekey = FALSE;
1167

    
1168
      foreach ($items[$id] as $delta => $item) {
1169
        // Check whether the taxonomy term field instance value could be loaded.
1170
        if (isset($terms[$item['tid']])) {
1171
          // Replace the instance value with the term data.
1172
          $items[$id][$delta]['taxonomy_term'] = $terms[$item['tid']];
1173
        }
1174
        // Otherwise, unset the instance value, since the term does not exist.
1175
        else {
1176
          unset($items[$id][$delta]);
1177
          $rekey = TRUE;
1178
        }
1179
      }
1180

    
1181
      if ($rekey) {
1182
        // Rekey the items array.
1183
        $items[$id] = array_values($items[$id]);
1184
      }
1185
    }
1186
  }
1187
}
1188

    
1189
/**
1190
 * Build a renderable array for a field value.
1191
 *
1192
 * @param $entity_type
1193
 *   The type of $entity.
1194
 * @param $entity
1195
 *   The entity being displayed.
1196
 * @param $field
1197
 *   The field structure.
1198
 * @param $instance
1199
 *   The field instance.
1200
 * @param $langcode
1201
 *   The language associated with $items.
1202
 * @param $items
1203
 *   Array of values for this field.
1204
 * @param $display
1205
 *   The display settings to use, as found in the 'display' entry of instance
1206
 *   definitions. The array notably contains the following keys and values;
1207
 *   - type: The name of the formatter to use.
1208
 *   - settings: The array of formatter settings.
1209
 *
1210
 * @return
1211
 *   A renderable array for the $items, as an array of child elements keyed
1212
 *   by numeric indexes starting from 0.
1213
 */
1214
function hook_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
1215
  $element = array();
1216
  $settings = $display['settings'];
1217

    
1218
  switch ($display['type']) {
1219
    case 'sample_field_formatter_simple':
1220
      // Common case: each value is displayed individually in a sub-element
1221
      // keyed by delta. The field.tpl.php template specifies the markup
1222
      // wrapping each value.
1223
      foreach ($items as $delta => $item) {
1224
        $element[$delta] = array('#markup' => $settings['some_setting'] . $item['value']);
1225
      }
1226
      break;
1227

    
1228
    case 'sample_field_formatter_themeable':
1229
      // More elaborate formatters can defer to a theme function for easier
1230
      // customization.
1231
      foreach ($items as $delta => $item) {
1232
        $element[$delta] = array(
1233
          '#theme' => 'mymodule_theme_sample_field_formatter_themeable',
1234
          '#data' => $item['value'],
1235
          '#some_setting' => $settings['some_setting'],
1236
        );
1237
      }
1238
      break;
1239

    
1240
    case 'sample_field_formatter_combined':
1241
      // Some formatters might need to display all values within a single piece
1242
      // of markup.
1243
      $rows = array();
1244
      foreach ($items as $delta => $item) {
1245
        $rows[] = array($delta, $item['value']);
1246
      }
1247
      $element[0] = array(
1248
        '#theme' => 'table',
1249
        '#header' => array(t('Delta'), t('Value')),
1250
        '#rows' => $rows,
1251
      );
1252
      break;
1253
  }
1254

    
1255
  return $element;
1256
}
1257

    
1258
/**
1259
 * @} End of "defgroup field_formatter".
1260
 */
1261

    
1262
/**
1263
 * @addtogroup field_attach
1264
 * @{
1265
 */
1266

    
1267
/**
1268
 * Act on field_attach_form().
1269
 *
1270
 * This hook is invoked after the field module has performed the operation.
1271
 * Implementing modules should alter the $form or $form_state parameters.
1272
 *
1273
 * @param $entity_type
1274
 *   The type of $entity; for example, 'node' or 'user'.
1275
 * @param $entity
1276
 *   The entity for which an edit form is being built.
1277
 * @param $form
1278
 *   The form structure where field elements are attached to. This might be a
1279
 *   full form structure, or a sub-element of a larger form. The
1280
 *   $form['#parents'] property can be used to identify the corresponding part
1281
 *   of $form_state['values']. Hook implementations that need to act on the
1282
 *   top-level properties of the global form (like #submit, #validate...) can
1283
 *   add a #process callback to the array received in the $form parameter, and
1284
 *   act on the $complete_form parameter in the process callback.
1285
 * @param $form_state
1286
 *   An associative array containing the current state of the form.
1287
 * @param $langcode
1288
 *   The language the field values are going to be entered in. If no language
1289
 *   is provided the default site language will be used.
1290
 */
1291
function hook_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
1292
  // Add a checkbox allowing a given field to be emptied.
1293
  // See hook_field_attach_submit() for the corresponding processing code.
1294
  $form['empty_field_foo'] = array(
1295
    '#type' => 'checkbox',
1296
    '#title' => t("Empty the 'field_foo' field"),
1297
  );
1298
}
1299

    
1300
/**
1301
 * Act on field_attach_load().
1302
 *
1303
 * This hook is invoked after the field module has performed the operation.
1304
 *
1305
 * Unlike other field_attach hooks, this hook accounts for 'multiple loads'.
1306
 * Instead of the usual $entity parameter, it accepts an array of entities,
1307
 * indexed by entity ID. For performance reasons, information for all available
1308
 * entities should be loaded in a single query where possible.
1309
 *
1310
 * The changes made to the entities' field values get cached by the field cache
1311
 * for subsequent loads.
1312
 *
1313
 * See field_attach_load() for details and arguments.
1314
 */
1315
function hook_field_attach_load($entity_type, $entities, $age, $options) {
1316
  // @todo Needs function body.
1317
}
1318

    
1319
/**
1320
 * Act on field_attach_validate().
1321
 *
1322
 * This hook is invoked after the field module has performed the operation.
1323
 *
1324
 * See field_attach_validate() for details and arguments.
1325
 *
1326
 * @param $entity_type
1327
 *   The type of $entity; e.g., 'node' or 'user'.
1328
 * @param $entity
1329
 *   The entity with fields to validate.
1330
 * @param array $errors
1331
 *   An associative array of errors keyed by field_name, language, delta.
1332
 */
1333
function hook_field_attach_validate($entity_type, $entity, &$errors) {
1334
  // @todo Needs function body.
1335
}
1336

    
1337
/**
1338
 * Act on field_attach_submit().
1339
 *
1340
 * This hook is invoked after the field module has performed the operation.
1341
 *
1342
 * @param $entity_type
1343
 *   The type of $entity; for example, 'node' or 'user'.
1344
 * @param $entity
1345
 *   The entity for which an edit form is being submitted. The incoming form
1346
 *   values have been extracted as field values of the $entity object.
1347
 * @param $form
1348
 *   The form structure where field elements are attached to. This might be a
1349
 *   full form structure, or a sub-part of a larger form. The $form['#parents']
1350
 *   property can be used to identify the corresponding part of
1351
 *   $form_state['values'].
1352
 * @param $form_state
1353
 *   An associative array containing the current state of the form.
1354
 */
1355
function hook_field_attach_submit($entity_type, $entity, $form, &$form_state) {
1356
  // Sample case of an 'Empty the field' checkbox added on the form, allowing
1357
  // a given field to be emptied.
1358
  $values = drupal_array_get_nested_value($form_state['values'], $form['#parents']);
1359
  if (!empty($values['empty_field_foo'])) {
1360
    unset($entity->field_foo);
1361
  }
1362
}
1363

    
1364
/**
1365
 * Act on field_attach_presave().
1366
 *
1367
 * This hook is invoked after the field module has performed the operation.
1368
 *
1369
 * See field_attach_presave() for details and arguments.
1370
 */
1371
function hook_field_attach_presave($entity_type, $entity) {
1372
  // @todo Needs function body.
1373
}
1374

    
1375
/**
1376
 * Act on field_attach_insert().
1377
 *
1378
 * This hook is invoked after the field module has performed the operation.
1379
 *
1380
 * See field_attach_insert() for details and arguments.
1381
 */
1382
function hook_field_attach_insert($entity_type, $entity) {
1383
  // @todo Needs function body.
1384
}
1385

    
1386
/**
1387
 * Act on field_attach_update().
1388
 *
1389
 * This hook is invoked after the field module has performed the operation.
1390
 *
1391
 * See field_attach_update() for details and arguments.
1392
 */
1393
function hook_field_attach_update($entity_type, $entity) {
1394
  // @todo Needs function body.
1395
}
1396

    
1397
/**
1398
 * Alter field_attach_preprocess() variables.
1399
 *
1400
 * This hook is invoked while preprocessing the field.tpl.php template file
1401
 * in field_attach_preprocess().
1402
 *
1403
 * @param $variables
1404
 *   The variables array is passed by reference and will be populated with field
1405
 *   values.
1406
 * @param $context
1407
 *   An associative array containing:
1408
 *   - entity_type: The type of $entity; for example, 'node' or 'user'.
1409
 *   - entity: The entity with fields to render.
1410
 *   - element: The structured array containing the values ready for rendering.
1411
 */
1412
function hook_field_attach_preprocess_alter(&$variables, $context) {
1413
  // @todo Needs function body.
1414
}
1415

    
1416
/**
1417
 * Act on field_attach_delete().
1418
 *
1419
 * This hook is invoked after the field module has performed the operation.
1420
 *
1421
 * See field_attach_delete() for details and arguments.
1422
 */
1423
function hook_field_attach_delete($entity_type, $entity) {
1424
  // @todo Needs function body.
1425
}
1426

    
1427
/**
1428
 * Act on field_attach_delete_revision().
1429
 *
1430
 * This hook is invoked after the field module has performed the operation.
1431
 *
1432
 * See field_attach_delete_revision() for details and arguments.
1433
 */
1434
function hook_field_attach_delete_revision($entity_type, $entity) {
1435
  // @todo Needs function body.
1436
}
1437

    
1438
/**
1439
 * Act on field_purge_data().
1440
 *
1441
 * This hook is invoked in field_purge_data() and allows modules to act on
1442
 * purging data from a single field pseudo-entity. For example, if a module
1443
 * relates data in the field with its own data, it may purge its own data
1444
 * during this process as well.
1445
 *
1446
 * @param $entity_type
1447
 *   The type of $entity; for example, 'node' or 'user'.
1448
 * @param $entity
1449
 *   The pseudo-entity whose field data is being purged.
1450
 * @param $field
1451
 *   The (possibly deleted) field whose data is being purged.
1452
 * @param $instance
1453
 *   The deleted field instance whose data is being purged.
1454
 *
1455
 * @see @link field_purge Field API bulk data deletion @endlink
1456
 * @see field_purge_data()
1457
 */
1458
function hook_field_attach_purge($entity_type, $entity, $field, $instance) {
1459
  // find the corresponding data in mymodule and purge it
1460
  if ($entity_type == 'node' && $field->field_name == 'my_field_name') {
1461
    mymodule_remove_mydata($entity->nid);
1462
  }
1463
}
1464

    
1465
/**
1466
 * Perform alterations on field_attach_view() or field_view_field().
1467
 *
1468
 * This hook is invoked after the field module has performed the operation.
1469
 *
1470
 * @param $output
1471
 *   The structured content array tree for all of the entity's fields.
1472
 * @param $context
1473
 *   An associative array containing:
1474
 *   - entity_type: The type of $entity; for example, 'node' or 'user'.
1475
 *   - entity: The entity with fields to render.
1476
 *   - view_mode: View mode; for example, 'full' or 'teaser'.
1477
 *   - display: Either a view mode string or an array of display settings. If
1478
 *     this hook is being invoked from field_attach_view(), the 'display'
1479
 *     element is set to the view mode string. If this hook is being invoked
1480
 *     from field_view_field(), this element is set to the $display argument
1481
 *     and the view_mode element is set to '_custom'. See field_view_field()
1482
 *     for more information on what its $display argument contains.
1483
 *   - language: The language code used for rendering.
1484
 */
1485
function hook_field_attach_view_alter(&$output, $context) {
1486
  // Append RDF term mappings on displayed taxonomy links.
1487
  foreach (element_children($output) as $field_name) {
1488
    $element = &$output[$field_name];
1489
    if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') {
1490
      foreach ($element['#items'] as $delta => $item) {
1491
        $term = $item['taxonomy_term'];
1492
        if (!empty($term->rdf_mapping['rdftype'])) {
1493
          $element[$delta]['#options']['attributes']['typeof'] = $term->rdf_mapping['rdftype'];
1494
        }
1495
        if (!empty($term->rdf_mapping['name']['predicates'])) {
1496
          $element[$delta]['#options']['attributes']['property'] = $term->rdf_mapping['name']['predicates'];
1497
        }
1498
      }
1499
    }
1500
  }
1501
}
1502

    
1503
/**
1504
 * Perform alterations on field_attach_prepare_translation().
1505
 *
1506
 * This hook is invoked after the field module has performed the operation.
1507
 *
1508
 * @param $entity
1509
 *   The entity being prepared for translation.
1510
 * @param $context
1511
 *   An associative array containing:
1512
 *   - entity_type: The type of $entity; e.g. 'node' or 'user'.
1513
 *   - langcode: The language the entity has to be translated in.
1514
 *   - source_entity: The entity holding the field values to be translated.
1515
 *   - source_langcode: The source language from which translate.
1516
 */
1517
function hook_field_attach_prepare_translation_alter(&$entity, $context) {
1518
  if ($context['entity_type'] == 'custom_entity_type') {
1519
    $entity->custom_field = $context['source_entity']->custom_field;
1520
  }
1521
}
1522

    
1523
/**
1524
 * Perform alterations on field_language() values.
1525
 *
1526
 * This hook is invoked to alter the array of display languages for the given
1527
 * entity.
1528
 *
1529
 * @param $display_language
1530
 *   A reference to an array of language codes keyed by field name.
1531
 * @param $context
1532
 *   An associative array containing:
1533
 *   - entity_type: The type of the entity to be displayed.
1534
 *   - entity: The entity with fields to render.
1535
 *   - langcode: The language code $entity has to be displayed in.
1536
 *
1537
 * @ingroup field_language
1538
 */
1539
function hook_field_language_alter(&$display_language, $context) {
1540
  // Do not apply core language fallback rules if they are disabled or if Locale
1541
  // is not registered as a translation handler.
1542
  if (variable_get('locale_field_language_fallback', TRUE) && field_has_translation_handler($context['entity_type'], 'locale')) {
1543
    locale_field_language_fallback($display_language, $context['entity'], $context['language']);
1544
  }
1545
}
1546

    
1547
/**
1548
 * Alter field_available_languages() values.
1549
 *
1550
 * This hook is invoked from field_available_languages() to allow modules to
1551
 * alter the array of available languages for the given field.
1552
 *
1553
 * @param $languages
1554
 *   A reference to an array of language codes to be made available.
1555
 * @param $context
1556
 *   An associative array containing:
1557
 *   - entity_type: The type of the entity the field is attached to.
1558
 *   - field: A field data structure.
1559
 *
1560
 * @ingroup field_language
1561
 */
1562
function hook_field_available_languages_alter(&$languages, $context) {
1563
  // Add an unavailable language.
1564
  $languages[] = 'xx';
1565

    
1566
  // Remove an available language.
1567
  $index = array_search('yy', $languages);
1568
  unset($languages[$index]);
1569
}
1570

    
1571
/**
1572
 * Act on field_attach_create_bundle().
1573
 *
1574
 * This hook is invoked after the field module has performed the operation.
1575
 *
1576
 * See field_attach_create_bundle() for details and arguments.
1577
 */
1578
function hook_field_attach_create_bundle($entity_type, $bundle) {
1579
  // When a new bundle is created, the menu needs to be rebuilt to add the
1580
  // Field UI menu item tabs.
1581
  variable_set('menu_rebuild_needed', TRUE);
1582
}
1583

    
1584
/**
1585
 * Act on field_attach_rename_bundle().
1586
 *
1587
 * This hook is invoked after the field module has performed the operation.
1588
 *
1589
 * See field_attach_rename_bundle() for details and arguments.
1590
 */
1591
function hook_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
1592
  // Update the extra weights variable with new information.
1593
  if ($bundle_old !== $bundle_new) {
1594
    $extra_weights = variable_get('field_extra_weights', array());
1595
    if (isset($info[$entity_type][$bundle_old])) {
1596
      $extra_weights[$entity_type][$bundle_new] = $extra_weights[$entity_type][$bundle_old];
1597
      unset($extra_weights[$entity_type][$bundle_old]);
1598
      variable_set('field_extra_weights', $extra_weights);
1599
    }
1600
  }
1601
}
1602

    
1603
/**
1604
 * Act on field_attach_delete_bundle.
1605
 *
1606
 * This hook is invoked after the field module has performed the operation.
1607
 *
1608
 * @param $entity_type
1609
 *   The type of entity; for example, 'node' or 'user'.
1610
 * @param $bundle
1611
 *   The name of the bundle that was just deleted.
1612
 * @param $instances
1613
 *   An array of all instances that existed for the bundle before it was
1614
 *   deleted.
1615
 */
1616
function hook_field_attach_delete_bundle($entity_type, $bundle, $instances) {
1617
  // Remove the extra weights variable information for this bundle.
1618
  $extra_weights = variable_get('field_extra_weights', array());
1619
  if (isset($extra_weights[$entity_type][$bundle])) {
1620
    unset($extra_weights[$entity_type][$bundle]);
1621
    variable_set('field_extra_weights', $extra_weights);
1622
  }
1623
}
1624

    
1625
/**
1626
 * @} End of "addtogroup field_attach".
1627
 */
1628

    
1629
/**
1630
 * @addtogroup field_storage
1631
 * @{
1632
 */
1633

    
1634
/**
1635
 * Expose Field API storage backends.
1636
 *
1637
 * @return
1638
 *   An array describing the storage backends implemented by the module.
1639
 *   The keys are storage backend names. To avoid name clashes, storage backend
1640
 *   names should be prefixed with the name of the module that exposes them.
1641
 *   The values are arrays describing the storage backend, with the following
1642
 *   key/value pairs:
1643
 *   - label: The human-readable name of the storage backend.
1644
 *   - description: A short description for the storage backend.
1645
 *   - settings: An array whose keys are the names of the settings available
1646
 *     for the storage backend, and whose values are the default values for
1647
 *     those settings.
1648
 */
1649
function hook_field_storage_info() {
1650
  return array(
1651
    'field_sql_storage' => array(
1652
      'label' => t('Default SQL storage'),
1653
      'description' => t('Stores fields in the local SQL database, using per-field tables.'),
1654
      'settings' => array(),
1655
    ),
1656
  );
1657
}
1658

    
1659
/**
1660
 * Perform alterations on Field API storage types.
1661
 *
1662
 * @param $info
1663
 *   Array of informations on storage types exposed by
1664
 *   hook_field_field_storage_info() implementations.
1665
 */
1666
function hook_field_storage_info_alter(&$info) {
1667
  // Add a setting to a storage type.
1668
  $info['field_sql_storage']['settings'] += array(
1669
    'mymodule_additional_setting' => 'default value',
1670
  );
1671
}
1672

    
1673
/**
1674
 * Reveal the internal details about the storage for a field.
1675
 *
1676
 * For example, an SQL storage module might return the Schema API structure for
1677
 * the table. A key/value storage module might return the server name,
1678
 * authentication credentials, and bin name.
1679
 *
1680
 * Field storage modules are not obligated to implement this hook. Modules
1681
 * that rely on these details must only use them for read operations.
1682
 *
1683
 * @param $field
1684
 *   A field structure.
1685
 *
1686
 * @return
1687
 *   An array of details.
1688
 *    - The first dimension is a store type (sql, solr, etc).
1689
 *    - The second dimension indicates the age of the values in the store
1690
 *      FIELD_LOAD_CURRENT or FIELD_LOAD_REVISION.
1691
 *    - Other dimensions are specific to the field storage module.
1692
 *
1693
 * @see hook_field_storage_details_alter()
1694
 */
1695
function hook_field_storage_details($field) {
1696
  $details = array();
1697

    
1698
  // Add field columns.
1699
  foreach ((array) $field['columns'] as $column_name => $attributes) {
1700
    $real_name = _field_sql_storage_columnname($field['field_name'], $column_name);
1701
    $columns[$column_name] = $real_name;
1702
  }
1703
  return array(
1704
    'sql' => array(
1705
      FIELD_LOAD_CURRENT => array(
1706
        _field_sql_storage_tablename($field) => $columns,
1707
      ),
1708
      FIELD_LOAD_REVISION => array(
1709
        _field_sql_storage_revision_tablename($field) => $columns,
1710
      ),
1711
    ),
1712
  );
1713
}
1714

    
1715
/**
1716
 * Perform alterations on Field API storage details.
1717
 *
1718
 * @param $details
1719
 *   An array of storage details for fields as exposed by
1720
 *   hook_field_storage_details() implementations.
1721
 * @param $field
1722
 *   A field structure.
1723
 *
1724
 * @see hook_field_storage_details()
1725
 */
1726
function hook_field_storage_details_alter(&$details, $field) {
1727
  if ($field['field_name'] == 'field_of_interest') {
1728
    $columns = array();
1729
    foreach ((array) $field['columns'] as $column_name => $attributes) {
1730
      $columns[$column_name] = $column_name;
1731
    }
1732
    $details['drupal_variables'] = array(
1733
      FIELD_LOAD_CURRENT => array(
1734
        'moon' => $columns,
1735
      ),
1736
      FIELD_LOAD_REVISION => array(
1737
        'mars' => $columns,
1738
      ),
1739
    );
1740
  }
1741
}
1742

    
1743
/**
1744
 * Load field data for a set of entities.
1745
 *
1746
 * This hook is invoked from field_attach_load() to ask the field storage
1747
 * module to load field data.
1748
 *
1749
 * Modules implementing this hook should load field values and add them to
1750
 * objects in $entities. Fields with no values should be added as empty
1751
 * arrays.
1752
 *
1753
 * @param $entity_type
1754
 *   The type of entity, such as 'node' or 'user'.
1755
 * @param $entities
1756
 *   The array of entity objects to add fields to, keyed by entity ID.
1757
 * @param $age
1758
 *   FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
1759
 *   FIELD_LOAD_REVISION to load the version indicated by each entity.
1760
 * @param $fields
1761
 *   An array listing the fields to be loaded. The keys of the array are field
1762
 *   IDs, and the values of the array are the entity IDs (or revision IDs,
1763
 *   depending on the $age parameter) to add each field to.
1764
 * @param $options
1765
 *   An associative array of additional options, with the following keys:
1766
 *   - deleted: If TRUE, deleted fields should be loaded as well as
1767
 *     non-deleted fields. If unset or FALSE, only non-deleted fields should be
1768
 *     loaded.
1769
 */
1770
function hook_field_storage_load($entity_type, $entities, $age, $fields, $options) {
1771
  $load_current = $age == FIELD_LOAD_CURRENT;
1772

    
1773
  foreach ($fields as $field_id => $ids) {
1774
    // By the time this hook runs, the relevant field definitions have been
1775
    // populated and cached in FieldInfo, so calling field_info_field_by_id()
1776
    // on each field individually is more efficient than loading all fields in
1777
    // memory upfront with field_info_field_by_ids().
1778
    $field = field_info_field_by_id($field_id);
1779
    $field_name = $field['field_name'];
1780
    $table = $load_current ? _field_sql_storage_tablename($field) : _field_sql_storage_revision_tablename($field);
1781

    
1782
    $query = db_select($table, 't')
1783
      ->fields('t')
1784
      ->condition('entity_type', $entity_type)
1785
      ->condition($load_current ? 'entity_id' : 'revision_id', $ids, 'IN')
1786
      ->condition('language', field_available_languages($entity_type, $field), 'IN')
1787
      ->orderBy('delta');
1788

    
1789
    if (empty($options['deleted'])) {
1790
      $query->condition('deleted', 0);
1791
    }
1792

    
1793
    $results = $query->execute();
1794

    
1795
    $delta_count = array();
1796
    foreach ($results as $row) {
1797
      if (!isset($delta_count[$row->entity_id][$row->language])) {
1798
        $delta_count[$row->entity_id][$row->language] = 0;
1799
      }
1800

    
1801
      if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED || $delta_count[$row->entity_id][$row->language] < $field['cardinality']) {
1802
        $item = array();
1803
        // For each column declared by the field, populate the item
1804
        // from the prefixed database column.
1805
        foreach ($field['columns'] as $column => $attributes) {
1806
          $column_name = _field_sql_storage_columnname($field_name, $column);
1807
          $item[$column] = $row->$column_name;
1808
        }
1809

    
1810
        // Add the item to the field values for the entity.
1811
        $entities[$row->entity_id]->{$field_name}[$row->language][] = $item;
1812
        $delta_count[$row->entity_id][$row->language]++;
1813
      }
1814
    }
1815
  }
1816
}
1817

    
1818
/**
1819
 * Write field data for an entity.
1820
 *
1821
 * This hook is invoked from field_attach_insert() and field_attach_update(),
1822
 * to ask the field storage module to save field data.
1823
 *
1824
 * @param $entity_type
1825
 *   The entity type of entity, such as 'node' or 'user'.
1826
 * @param $entity
1827
 *   The entity on which to operate.
1828
 * @param $op
1829
 *   FIELD_STORAGE_UPDATE when updating an existing entity,
1830
 *   FIELD_STORAGE_INSERT when inserting a new entity.
1831
 * @param $fields
1832
 *   An array listing the fields to be written. The keys and values of the
1833
 *   array are field IDs.
1834
 */
1835
function hook_field_storage_write($entity_type, $entity, $op, $fields) {
1836
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
1837
  if (!isset($vid)) {
1838
    $vid = $id;
1839
  }
1840

    
1841
  foreach ($fields as $field_id) {
1842
    $field = field_info_field_by_id($field_id);
1843
    $field_name = $field['field_name'];
1844
    $table_name = _field_sql_storage_tablename($field);
1845
    $revision_name = _field_sql_storage_revision_tablename($field);
1846

    
1847
    $all_languages = field_available_languages($entity_type, $field);
1848
    $field_languages = array_intersect($all_languages, array_keys((array) $entity->$field_name));
1849

    
1850
    // Delete and insert, rather than update, in case a value was added.
1851
    if ($op == FIELD_STORAGE_UPDATE) {
1852
      // Delete languages present in the incoming $entity->$field_name.
1853
      // Delete all languages if $entity->$field_name is empty.
1854
      $languages = !empty($entity->$field_name) ? $field_languages : $all_languages;
1855
      if ($languages) {
1856
        db_delete($table_name)
1857
          ->condition('entity_type', $entity_type)
1858
          ->condition('entity_id', $id)
1859
          ->condition('language', $languages, 'IN')
1860
          ->execute();
1861
        db_delete($revision_name)
1862
          ->condition('entity_type', $entity_type)
1863
          ->condition('entity_id', $id)
1864
          ->condition('revision_id', $vid)
1865
          ->condition('language', $languages, 'IN')
1866
          ->execute();
1867
      }
1868
    }
1869

    
1870
    // Prepare the multi-insert query.
1871
    $do_insert = FALSE;
1872
    $columns = array('entity_type', 'entity_id', 'revision_id', 'bundle', 'delta', 'language');
1873
    foreach ($field['columns'] as $column => $attributes) {
1874
      $columns[] = _field_sql_storage_columnname($field_name, $column);
1875
    }
1876
    $query = db_insert($table_name)->fields($columns);
1877
    $revision_query = db_insert($revision_name)->fields($columns);
1878

    
1879
    foreach ($field_languages as $langcode) {
1880
      $items = (array) $entity->{$field_name}[$langcode];
1881
      $delta_count = 0;
1882
      foreach ($items as $delta => $item) {
1883
        // We now know we have someting to insert.
1884
        $do_insert = TRUE;
1885
        $record = array(
1886
          'entity_type' => $entity_type,
1887
          'entity_id' => $id,
1888
          'revision_id' => $vid,
1889
          'bundle' => $bundle,
1890
          'delta' => $delta,
1891
          'language' => $langcode,
1892
        );
1893
        foreach ($field['columns'] as $column => $attributes) {
1894
          $record[_field_sql_storage_columnname($field_name, $column)] = isset($item[$column]) ? $item[$column] : NULL;
1895
        }
1896
        $query->values($record);
1897
        if (isset($vid)) {
1898
          $revision_query->values($record);
1899
        }
1900

    
1901
        if ($field['cardinality'] != FIELD_CARDINALITY_UNLIMITED && ++$delta_count == $field['cardinality']) {
1902
          break;
1903
        }
1904
      }
1905
    }
1906

    
1907
    // Execute the query if we have values to insert.
1908
    if ($do_insert) {
1909
      $query->execute();
1910
      $revision_query->execute();
1911
    }
1912
  }
1913
}
1914

    
1915
/**
1916
 * Delete all field data for an entity.
1917
 *
1918
 * This hook is invoked from field_attach_delete() to ask the field storage
1919
 * module to delete field data.
1920
 *
1921
 * @param $entity_type
1922
 *   The entity type of entity, such as 'node' or 'user'.
1923
 * @param $entity
1924
 *   The entity on which to operate.
1925
 * @param $fields
1926
 *   An array listing the fields to delete. The keys and values of the
1927
 *   array are field IDs.
1928
 */
1929
function hook_field_storage_delete($entity_type, $entity, $fields) {
1930
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
1931

    
1932
  foreach (field_info_instances($entity_type, $bundle) as $instance) {
1933
    if (isset($fields[$instance['field_id']])) {
1934
      $field = field_info_field_by_id($instance['field_id']);
1935
      field_sql_storage_field_storage_purge($entity_type, $entity, $field, $instance);
1936
    }
1937
  }
1938
}
1939

    
1940
/**
1941
 * Delete a single revision of field data for an entity.
1942
 *
1943
 * This hook is invoked from field_attach_delete_revision() to ask the field
1944
 * storage module to delete field revision data.
1945
 *
1946
 * Deleting the current (most recently written) revision is not
1947
 * allowed as has undefined results.
1948
 *
1949
 * @param $entity_type
1950
 *   The entity type of entity, such as 'node' or 'user'.
1951
 * @param $entity
1952
 *   The entity on which to operate. The revision to delete is
1953
 *   indicated by the entity's revision ID property, as identified by
1954
 *   hook_fieldable_info() for $entity_type.
1955
 * @param $fields
1956
 *   An array listing the fields to delete. The keys and values of the
1957
 *   array are field IDs.
1958
 */
1959
function hook_field_storage_delete_revision($entity_type, $entity, $fields) {
1960
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
1961

    
1962
  if (isset($vid)) {
1963
    foreach ($fields as $field_id) {
1964
      $field = field_info_field_by_id($field_id);
1965
      $revision_name = _field_sql_storage_revision_tablename($field);
1966
      db_delete($revision_name)
1967
        ->condition('entity_type', $entity_type)
1968
        ->condition('entity_id', $id)
1969
        ->condition('revision_id', $vid)
1970
        ->execute();
1971
    }
1972
  }
1973
}
1974

    
1975
/**
1976
 * Execute an EntityFieldQuery.
1977
 *
1978
 * This hook is called to find the entities having certain entity and field
1979
 * conditions and sort them in the given field order. If the field storage
1980
 * engine also handles property sorts and orders, it should unset those
1981
 * properties in the called object to signal that those have been handled.
1982
 *
1983
 * @param EntityFieldQuery $query
1984
 *   An EntityFieldQuery.
1985
 *
1986
 * @return
1987
 *   See EntityFieldQuery::execute() for the return values.
1988
 */
1989
function hook_field_storage_query($query) {
1990
  $groups = array();
1991
  if ($query->age == FIELD_LOAD_CURRENT) {
1992
    $tablename_function = '_field_sql_storage_tablename';
1993
    $id_key = 'entity_id';
1994
  }
1995
  else {
1996
    $tablename_function = '_field_sql_storage_revision_tablename';
1997
    $id_key = 'revision_id';
1998
  }
1999
  $table_aliases = array();
2000
  // Add tables for the fields used.
2001
  foreach ($query->fields as $key => $field) {
2002
    $tablename = $tablename_function($field);
2003
    // Every field needs a new table.
2004
    $table_alias = $tablename . $key;
2005
    $table_aliases[$key] = $table_alias;
2006
    if ($key) {
2007
      $select_query->join($tablename, $table_alias, "$table_alias.entity_type = $field_base_table.entity_type AND $table_alias.$id_key = $field_base_table.$id_key");
2008
    }
2009
    else {
2010
      $select_query = db_select($tablename, $table_alias);
2011
      $select_query->addTag('entity_field_access');
2012
      $select_query->addMetaData('base_table', $tablename);
2013
      $select_query->fields($table_alias, array('entity_type', 'entity_id', 'revision_id', 'bundle'));
2014
      $field_base_table = $table_alias;
2015
    }
2016
    if ($field['cardinality'] != 1) {
2017
      $select_query->distinct();
2018
    }
2019
  }
2020

    
2021
  // Add field conditions.
2022
  foreach ($query->fieldConditions as $key => $condition) {
2023
    $table_alias = $table_aliases[$key];
2024
    $field = $condition['field'];
2025
    // Add the specified condition.
2026
    $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $condition['column']);
2027
    $query->addCondition($select_query, $sql_field, $condition);
2028
    // Add delta / language group conditions.
2029
    foreach (array('delta', 'language') as $column) {
2030
      if (isset($condition[$column . '_group'])) {
2031
        $group_name = $condition[$column . '_group'];
2032
        if (!isset($groups[$column][$group_name])) {
2033
          $groups[$column][$group_name] = $table_alias;
2034
        }
2035
        else {
2036
          $select_query->where("$table_alias.$column = " . $groups[$column][$group_name] . ".$column");
2037
        }
2038
      }
2039
    }
2040
  }
2041

    
2042
  if (isset($query->deleted)) {
2043
    $select_query->condition("$field_base_table.deleted", (int) $query->deleted);
2044
  }
2045

    
2046
  // Is there a need to sort the query by property?
2047
  $has_property_order = FALSE;
2048
  foreach ($query->order as $order) {
2049
    if ($order['type'] == 'property') {
2050
      $has_property_order = TRUE;
2051
    }
2052
  }
2053

    
2054
  if ($query->propertyConditions || $has_property_order) {
2055
    if (empty($query->entityConditions['entity_type']['value'])) {
2056
      throw new EntityFieldQueryException('Property conditions and orders must have an entity type defined.');
2057
    }
2058
    $entity_type = $query->entityConditions['entity_type']['value'];
2059
    $entity_base_table = _field_sql_storage_query_join_entity($select_query, $entity_type, $field_base_table);
2060
    $query->entityConditions['entity_type']['operator'] = '=';
2061
    foreach ($query->propertyConditions as $property_condition) {
2062
      $query->addCondition($select_query, "$entity_base_table." . $property_condition['column'], $property_condition);
2063
    }
2064
  }
2065
  foreach ($query->entityConditions as $key => $condition) {
2066
    $query->addCondition($select_query, "$field_base_table.$key", $condition);
2067
  }
2068

    
2069
  // Order the query.
2070
  foreach ($query->order as $order) {
2071
    if ($order['type'] == 'entity') {
2072
      $key = $order['specifier'];
2073
      $select_query->orderBy("$field_base_table.$key", $order['direction']);
2074
    }
2075
    elseif ($order['type'] == 'field') {
2076
      $specifier = $order['specifier'];
2077
      $field = $specifier['field'];
2078
      $table_alias = $table_aliases[$specifier['index']];
2079
      $sql_field = "$table_alias." . _field_sql_storage_columnname($field['field_name'], $specifier['column']);
2080
      $select_query->orderBy($sql_field, $order['direction']);
2081
    }
2082
    elseif ($order['type'] == 'property') {
2083
      $select_query->orderBy("$entity_base_table." . $order['specifier'], $order['direction']);
2084
    }
2085
  }
2086

    
2087
  return $query->finishQuery($select_query, $id_key);
2088
}
2089

    
2090
/**
2091
 * Act on creation of a new field.
2092
 *
2093
 * This hook is invoked from field_create_field() to ask the field storage
2094
 * module to save field information and prepare for storing field instances.
2095
 * If there is a problem, the field storage module should throw an exception.
2096
 *
2097
 * @param $field
2098
 *   The field structure being created.
2099
 */
2100
function hook_field_storage_create_field($field) {
2101
  $schema = _field_sql_storage_schema($field);
2102
  foreach ($schema as $name => $table) {
2103
    db_create_table($name, $table);
2104
  }
2105
  drupal_get_schema(NULL, TRUE);
2106
}
2107

    
2108
/**
2109
 * Act on deletion of a field.
2110
 *
2111
 * This hook is invoked from field_delete_field() to ask the field storage
2112
 * module to mark all information stored in the field for deletion.
2113
 *
2114
 * @param $field
2115
 *   The field being deleted.
2116
 */
2117
function hook_field_storage_delete_field($field) {
2118
  // Mark all data associated with the field for deletion.
2119
  $field['deleted'] = 0;
2120
  $table = _field_sql_storage_tablename($field);
2121
  $revision_table = _field_sql_storage_revision_tablename($field);
2122
  db_update($table)
2123
    ->fields(array('deleted' => 1))
2124
    ->execute();
2125

    
2126
  // Move the table to a unique name while the table contents are being deleted.
2127
  $field['deleted'] = 1;
2128
  $new_table = _field_sql_storage_tablename($field);
2129
  $revision_new_table = _field_sql_storage_revision_tablename($field);
2130
  db_rename_table($table, $new_table);
2131
  db_rename_table($revision_table, $revision_new_table);
2132
  drupal_get_schema(NULL, TRUE);
2133
}
2134

    
2135
/**
2136
 * Act on deletion of a field instance.
2137
 *
2138
 * This hook is invoked from field_delete_instance() to ask the field storage
2139
 * module to mark all information stored for the field instance for deletion.
2140
 *
2141
 * @param $instance
2142
 *   The instance being deleted.
2143
 */
2144
function hook_field_storage_delete_instance($instance) {
2145
  $field = field_info_field($instance['field_name']);
2146
  $table_name = _field_sql_storage_tablename($field);
2147
  $revision_name = _field_sql_storage_revision_tablename($field);
2148
  db_update($table_name)
2149
    ->fields(array('deleted' => 1))
2150
    ->condition('entity_type', $instance['entity_type'])
2151
    ->condition('bundle', $instance['bundle'])
2152
    ->execute();
2153
  db_update($revision_name)
2154
    ->fields(array('deleted' => 1))
2155
    ->condition('entity_type', $instance['entity_type'])
2156
    ->condition('bundle', $instance['bundle'])
2157
    ->execute();
2158
}
2159

    
2160
/**
2161
 * Act before the storage backends load field data.
2162
 *
2163
 * This hook allows modules to load data before the Field Storage API,
2164
 * optionally preventing the field storage module from doing so.
2165
 *
2166
 * This lets 3rd party modules override, mirror, shard, or otherwise store a
2167
 * subset of fields in a different way than the current storage engine.
2168
 * Possible use cases include per-bundle storage, per-combo-field storage, etc.
2169
 *
2170
 * Modules implementing this hook should load field values and add them to
2171
 * objects in $entities. Fields with no values should be added as empty
2172
 * arrays. In addition, fields loaded should be added as keys to $skip_fields.
2173
 *
2174
 * @param $entity_type
2175
 *   The type of entity, such as 'node' or 'user'.
2176
 * @param $entities
2177
 *   The array of entity objects to add fields to, keyed by entity ID.
2178
 * @param $age
2179
 *   FIELD_LOAD_CURRENT to load the most recent revision for all fields, or
2180
 *   FIELD_LOAD_REVISION to load the version indicated by each entity.
2181
 * @param $skip_fields
2182
 *   An array keyed by field IDs whose data has already been loaded and
2183
 *   therefore should not be loaded again. Add a key to this array to indicate
2184
 *   that your module has already loaded a field.
2185
 * @param $options
2186
 *   An associative array of additional options, with the following keys:
2187
 *   - field_id: The field ID that should be loaded. If unset, all fields
2188
 *     should be loaded.
2189
 *   - deleted: If TRUE, deleted fields should be loaded as well as
2190
 *     non-deleted fields. If unset or FALSE, only non-deleted fields should be
2191
 *     loaded.
2192
 */
2193
function hook_field_storage_pre_load($entity_type, $entities, $age, &$skip_fields, $options) {
2194
  // @todo Needs function body.
2195
}
2196

    
2197
/**
2198
 * Act before the storage backends insert field data.
2199
 *
2200
 * This hook allows modules to store data before the Field Storage API,
2201
 * optionally preventing the field storage module from doing so.
2202
 *
2203
 * @param $entity_type
2204
 *   The type of $entity; for example, 'node' or 'user'.
2205
 * @param $entity
2206
 *   The entity with fields to save.
2207
 * @param $skip_fields
2208
 *   An array keyed by field IDs whose data has already been written and
2209
 *   therefore should not be written again. The values associated with these
2210
 *   keys are not specified.
2211
 * @return
2212
 *   Saved field IDs are set set as keys in $skip_fields.
2213
 */
2214
function hook_field_storage_pre_insert($entity_type, $entity, &$skip_fields) {
2215
  if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
2216
    $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
2217
    foreach ($entity->taxonomy_forums as $language) {
2218
      foreach ($language as $delta) {
2219
        $query->values(array(
2220
          'nid' => $entity->nid,
2221
          'title' => $entity->title,
2222
          'tid' => $delta['value'],
2223
          'sticky' => $entity->sticky,
2224
          'created' => $entity->created,
2225
          'comment_count' => 0,
2226
          'last_comment_timestamp' => $entity->created,
2227
        ));
2228
      }
2229
    }
2230
    $query->execute();
2231
  }
2232
}
2233

    
2234
/**
2235
 * Act before the storage backends update field data.
2236
 *
2237
 * This hook allows modules to store data before the Field Storage API,
2238
 * optionally preventing the field storage module from doing so.
2239
 *
2240
 * @param $entity_type
2241
 *   The type of $entity; for example, 'node' or 'user'.
2242
 * @param $entity
2243
 *   The entity with fields to save.
2244
 * @param $skip_fields
2245
 *   An array keyed by field IDs whose data has already been written and
2246
 *   therefore should not be written again. The values associated with these
2247
 *   keys are not specified.
2248
 * @return
2249
 *   Saved field IDs are set set as keys in $skip_fields.
2250
 */
2251
function hook_field_storage_pre_update($entity_type, $entity, &$skip_fields) {
2252
  $first_call = &drupal_static(__FUNCTION__, array());
2253

    
2254
  if ($entity_type == 'node' && $entity->status && _forum_node_check_node_type($entity)) {
2255
    // We don't maintain data for old revisions, so clear all previous values
2256
    // from the table. Since this hook runs once per field, per entity, make
2257
    // sure we only wipe values once.
2258
    if (!isset($first_call[$entity->nid])) {
2259
      $first_call[$entity->nid] = FALSE;
2260
      db_delete('forum_index')->condition('nid', $entity->nid)->execute();
2261
    }
2262
    // Only save data to the table if the node is published.
2263
    if ($entity->status) {
2264
      $query = db_insert('forum_index')->fields(array('nid', 'title', 'tid', 'sticky', 'created', 'comment_count', 'last_comment_timestamp'));
2265
      foreach ($entity->taxonomy_forums as $language) {
2266
        foreach ($language as $delta) {
2267
          $query->values(array(
2268
            'nid' => $entity->nid,
2269
            'title' => $entity->title,
2270
            'tid' => $delta['value'],
2271
            'sticky' => $entity->sticky,
2272
            'created' => $entity->created,
2273
            'comment_count' => 0,
2274
            'last_comment_timestamp' => $entity->created,
2275
          ));
2276
        }
2277
      }
2278
      $query->execute();
2279
      // The logic for determining last_comment_count is fairly complex, so
2280
      // call _forum_update_forum_index() too.
2281
      _forum_update_forum_index($entity->nid);
2282
    }
2283
  }
2284
}
2285

    
2286
/**
2287
 * @} End of "addtogroup field_storage
2288
 */
2289

    
2290
/**
2291
 * Returns the maximum weight for the entity components handled by the module.
2292
 *
2293
 * Field API takes care of fields and 'extra_fields'. This hook is intended for
2294
 * third-party modules adding other entity components (e.g. field_group).
2295
 *
2296
 * @param $entity_type
2297
 *   The type of entity; e.g. 'node' or 'user'.
2298
 * @param $bundle
2299
 *   The bundle name.
2300
 * @param $context
2301
 *   The context for which the maximum weight is requested. Either 'form', or
2302
 *   the name of a view mode.
2303
 *
2304
 * @return
2305
 *   The maximum weight of the entity's components, or NULL if no components
2306
 *   were found.
2307
 *
2308
 * @ingroup field_info
2309
 */
2310
function hook_field_info_max_weight($entity_type, $bundle, $context) {
2311
  $weights = array();
2312

    
2313
  foreach (my_module_entity_additions($entity_type, $bundle, $context) as $addition) {
2314
    $weights[] = $addition['weight'];
2315
  }
2316

    
2317
  return $weights ? max($weights) : NULL;
2318
}
2319

    
2320
/**
2321
 * @addtogroup field_types
2322
 * @{
2323
 */
2324

    
2325
/**
2326
 * Alters the display settings of a field before it gets displayed.
2327
 *
2328
 * Note that instead of hook_field_display_alter(), which is called for all
2329
 * fields on all entity types, hook_field_display_ENTITY_TYPE_alter() may be
2330
 * used to alter display settings for fields on a specific entity type only.
2331
 *
2332
 * This hook is called once per field per displayed entity. If the result of the
2333
 * hook involves reading from the database, it is highly recommended to
2334
 * statically cache the information.
2335
 *
2336
 * @param $display
2337
 *   The display settings that will be used to display the field values, as
2338
 *   found in the 'display' key of $instance definitions.
2339
 * @param $context
2340
 *   An associative array containing:
2341
 *   - entity_type: The entity type; e.g., 'node' or 'user'.
2342
 *   - field: The field being rendered.
2343
 *   - instance: The instance being rendered.
2344
 *   - entity: The entity being rendered.
2345
 *   - view_mode: The view mode, e.g. 'full', 'teaser'...
2346
 *
2347
 * @see hook_field_display_ENTITY_TYPE_alter()
2348
 */
2349
function hook_field_display_alter(&$display, $context) {
2350
  // Leave field labels out of the search index.
2351
  // Note: The check against $context['entity_type'] == 'node' could be avoided
2352
  // by using hook_field_display_node_alter() instead of
2353
  // hook_field_display_alter(), resulting in less function calls when
2354
  // rendering non-node entities.
2355
  if ($context['entity_type'] == 'node' && $context['view_mode'] == 'search_index') {
2356
    $display['label'] = 'hidden';
2357
  }
2358
}
2359

    
2360
/**
2361
 * Alters the display settings of a field on a given entity type before it gets displayed.
2362
 *
2363
 * Modules can implement hook_field_display_ENTITY_TYPE_alter() to alter display
2364
 * settings for fields on a specific entity type, rather than implementing
2365
 * hook_field_display_alter().
2366
 *
2367
 * This hook is called once per field per displayed entity. If the result of the
2368
 * hook involves reading from the database, it is highly recommended to
2369
 * statically cache the information.
2370
 *
2371
 * @param $display
2372
 *   The display settings that will be used to display the field values, as
2373
 *   found in the 'display' key of $instance definitions.
2374
 * @param $context
2375
 *   An associative array containing:
2376
 *   - entity_type: The entity type; e.g., 'node' or 'user'.
2377
 *   - field: The field being rendered.
2378
 *   - instance: The instance being rendered.
2379
 *   - entity: The entity being rendered.
2380
 *   - view_mode: The view mode, e.g. 'full', 'teaser'...
2381
 *
2382
 * @see hook_field_display_alter()
2383
 */
2384
function hook_field_display_ENTITY_TYPE_alter(&$display, $context) {
2385
  // Leave field labels out of the search index.
2386
  if ($context['view_mode'] == 'search_index') {
2387
    $display['label'] = 'hidden';
2388
  }
2389
}
2390

    
2391
/**
2392
 * @} End of "addtogroup field_types
2393
 */
2394

    
2395
/**
2396
 * Alters the display settings of pseudo-fields before an entity is displayed.
2397
 *
2398
 * This hook is called once per displayed entity. If the result of the hook
2399
 * involves reading from the database, it is highly recommended to statically
2400
 * cache the information.
2401
 *
2402
 * @param $displays
2403
 *   An array of display settings for the pseudo-fields in the entity, keyed
2404
 *   by pseudo-field names.
2405
 * @param $context
2406
 *   An associative array containing:
2407
 *   - entity_type: The entity type; e.g., 'node' or 'user'.
2408
 *   - bundle: The bundle name.
2409
 *   - view_mode: The view mode, e.g. 'full', 'teaser'...
2410
 *
2411
 * @ingroup field_types
2412
 */
2413
function hook_field_extra_fields_display_alter(&$displays, $context) {
2414
  if ($context['entity_type'] == 'taxonomy_term' && $context['view_mode'] == 'full') {
2415
    $displays['description']['visible'] = FALSE;
2416
  }
2417
}
2418

    
2419
/**
2420
 * Alters the widget properties of a field instance on a given entity type
2421
 * before it gets displayed.
2422
 *
2423
 * Modules can implement hook_field_widget_properties_ENTITY_TYPE_alter() to
2424
 * alter the widget properties for fields on a specific entity type, rather than
2425
 * implementing hook_field_widget_properties_alter().
2426
 *
2427
 * This hook is called once per field per displayed widget entity. If the result
2428
 * of the hook involves reading from the database, it is highly recommended to
2429
 * statically cache the information.
2430
 *
2431
 * @param $widget
2432
 *   The instance's widget properties.
2433
 * @param $context
2434
 *   An associative array containing:
2435
 *   - entity_type: The entity type; e.g., 'node' or 'user'.
2436
 *   - entity: The entity object.
2437
 *   - field: The field that the widget belongs to.
2438
 *   - instance: The instance of the field.
2439
 *
2440
 * @see hook_field_widget_properties_alter()
2441
 *
2442
 * @ingroup field_widget
2443
 */
2444
function hook_field_widget_properties_ENTITY_TYPE_alter(&$widget, $context) {
2445
  // Change a widget's type according to the time of day.
2446
  $field = $context['field'];
2447
  if ($field['field_name'] == 'field_foo') {
2448
    $time = date('H');
2449
    $widget['type'] = $time < 12 ? 'widget_am' : 'widget_pm';
2450
  }
2451
}
2452

    
2453
/**
2454
 * @addtogroup field_crud
2455
 * @{
2456
 */
2457

    
2458
/**
2459
 * Act on a field being created.
2460
 *
2461
 * This hook is invoked from field_create_field() after the field is created, to
2462
 * allow modules to act on field creation.
2463
 *
2464
 * @param $field
2465
 *   The field just created.
2466
 */
2467
function hook_field_create_field($field) {
2468
  // @todo Needs function body.
2469
}
2470

    
2471
/**
2472
 * Act on a field instance being created.
2473
 *
2474
 * This hook is invoked from field_create_instance() after the instance record
2475
 * is saved, so it cannot be used to modify the instance itself.
2476
 *
2477
 * @param $instance
2478
 *   The instance just created.
2479
 */
2480
function hook_field_create_instance($instance) {
2481
  // @todo Needs function body.
2482
}
2483

    
2484
/**
2485
 * Forbid a field update from occurring.
2486
 *
2487
 * Any module may forbid any update for any reason. For example, the
2488
 * field's storage module might forbid an update if it would change
2489
 * the storage schema while data for the field exists. A field type
2490
 * module might forbid an update if it would change existing data's
2491
 * semantics, or if there are external dependencies on field settings
2492
 * that cannot be updated.
2493
 *
2494
 * To forbid the update from occurring, throw a FieldUpdateForbiddenException.
2495
 *
2496
 * @param $field
2497
 *   The field as it will be post-update.
2498
 * @param $prior_field
2499
 *   The field as it is pre-update.
2500
 * @param $has_data
2501
 *   Whether any data already exists for this field.
2502
 */
2503
function hook_field_update_forbid($field, $prior_field, $has_data) {
2504
  // A 'list' field stores integer keys mapped to display values. If
2505
  // the new field will have fewer values, and any data exists for the
2506
  // abandoned keys, the field will have no way to display them. So,
2507
  // forbid such an update.
2508
  if ($has_data && count($field['settings']['allowed_values']) < count($prior_field['settings']['allowed_values'])) {
2509
    // Identify the keys that will be lost.
2510
    $lost_keys = array_diff(array_keys($field['settings']['allowed_values']), array_keys($prior_field['settings']['allowed_values']));
2511
    // If any data exist for those keys, forbid the update.
2512
    $query = new EntityFieldQuery();
2513
    $found = $query
2514
      ->fieldCondition($prior_field['field_name'], 'value', $lost_keys)
2515
      ->range(0, 1)
2516
      ->execute();
2517
    if ($found) {
2518
      throw new FieldUpdateForbiddenException("Cannot update a list field not to include keys with existing data");
2519
    }
2520
  }
2521
}
2522

    
2523
/**
2524
 * Act on a field being updated.
2525
 *
2526
 * This hook is invoked just after field is updated in field_update_field().
2527
 *
2528
 * @param $field
2529
 *   The field as it is post-update.
2530
 * @param $prior_field
2531
 *   The field as it was pre-update.
2532
 * @param $has_data
2533
 *   Whether any data already exists for this field.
2534
 */
2535
function hook_field_update_field($field, $prior_field, $has_data) {
2536
  // Reset the static value that keeps track of allowed values for list fields.
2537
  drupal_static_reset('list_allowed_values');
2538
}
2539

    
2540
/**
2541
 * Act on a field being deleted.
2542
 *
2543
 * This hook is invoked just after a field is deleted by field_delete_field().
2544
 *
2545
 * @param $field
2546
 *   The field just deleted.
2547
 */
2548
function hook_field_delete_field($field) {
2549
  // @todo Needs function body.
2550
}
2551

    
2552
/**
2553
 * Act on a field instance being updated.
2554
 *
2555
 * This hook is invoked from field_update_instance() after the instance record
2556
 * is saved, so it cannot be used by a module to modify the instance itself.
2557
 *
2558
 * @param $instance
2559
 *   The instance as it is post-update.
2560
 * @param $prior_instance
2561
 *   The instance as it was pre-update.
2562
 */
2563
function hook_field_update_instance($instance, $prior_instance) {
2564
  // @todo Needs function body.
2565
}
2566

    
2567
/**
2568
 * Act on a field instance being deleted.
2569
 *
2570
 * This hook is invoked from field_delete_instance() after the instance is
2571
 * deleted.
2572
 *
2573
 * @param $instance
2574
 *   The instance just deleted.
2575
 */
2576
function hook_field_delete_instance($instance) {
2577
  // @todo Needs function body.
2578
}
2579

    
2580
/**
2581
 * Act on field records being read from the database.
2582
 *
2583
 * This hook is invoked from field_read_fields() on each field being read.
2584
 *
2585
 * @param $field
2586
 *   The field record just read from the database.
2587
 */
2588
function hook_field_read_field($field) {
2589
  // @todo Needs function body.
2590
}
2591

    
2592
/**
2593
 * Act on a field record being read from the database.
2594
 *
2595
 * This hook is invoked from field_read_instances() on each instance being read.
2596
 *
2597
 * @param $instance
2598
 *   The instance record just read from the database.
2599
 */
2600
function hook_field_read_instance($instance) {
2601
  // @todo Needs function body.
2602
}
2603

    
2604
/**
2605
 * Acts when a field record is being purged.
2606
 *
2607
 * In field_purge_field(), after the field configuration has been
2608
 * removed from the database, the field storage module has had a chance to
2609
 * run its hook_field_storage_purge_field(), and the field info cache
2610
 * has been cleared, this hook is invoked on all modules to allow them to
2611
 * respond to the field being purged.
2612
 *
2613
 * @param $field
2614
 *   The field being purged.
2615
 */
2616
function hook_field_purge_field($field) {
2617
  db_delete('my_module_field_info')
2618
    ->condition('id', $field['id'])
2619
    ->execute();
2620
}
2621

    
2622
/**
2623
 * Acts when a field instance is being purged.
2624
 *
2625
 * In field_purge_instance(), after the field instance has been
2626
 * removed from the database, the field storage module has had a chance to
2627
 * run its hook_field_storage_purge_instance(), and the field info cache
2628
 * has been cleared, this hook is invoked on all modules to allow them to
2629
 * respond to the field instance being purged.
2630
 *
2631
 * @param $instance
2632
 *   The instance being purged.
2633
 */
2634
function hook_field_purge_instance($instance) {
2635
  db_delete('my_module_field_instance_info')
2636
    ->condition('id', $instance['id'])
2637
    ->execute();
2638
}
2639

    
2640
/**
2641
 * Remove field storage information when a field record is purged.
2642
 *
2643
 * Called from field_purge_field() to allow the field storage module
2644
 * to remove field information when a field is being purged.
2645
 *
2646
 * @param $field
2647
 *   The field being purged.
2648
 *
2649
 * @ingroup field_storage
2650
 */
2651
function hook_field_storage_purge_field($field) {
2652
  $table_name = _field_sql_storage_tablename($field);
2653
  $revision_name = _field_sql_storage_revision_tablename($field);
2654
  db_drop_table($table_name);
2655
  db_drop_table($revision_name);
2656
}
2657

    
2658
/**
2659
 * Remove field storage information when a field instance is purged.
2660
 *
2661
 * Called from field_purge_instance() to allow the field storage module
2662
 * to remove field instance information when a field instance is being
2663
 * purged.
2664
 *
2665
 * @param $instance
2666
 *   The instance being purged.
2667
 *
2668
 * @ingroup field_storage
2669
 */
2670
function hook_field_storage_purge_field_instance($instance) {
2671
  db_delete('my_module_field_instance_info')
2672
    ->condition('id', $instance['id'])
2673
    ->execute();
2674
}
2675

    
2676
/**
2677
 * Remove field storage information when field data is purged.
2678
 *
2679
 * Called from field_purge_data() to allow the field storage
2680
 * module to delete field data information.
2681
 *
2682
 * @param $entity_type
2683
 *   The type of $entity; for example, 'node' or 'user'.
2684
 * @param $entity
2685
 *   The pseudo-entity whose field data to delete.
2686
 * @param $field
2687
 *   The (possibly deleted) field whose data is being purged.
2688
 * @param $instance
2689
 *   The deleted field instance whose data is being purged.
2690
 *
2691
 * @ingroup field_storage
2692
 */
2693
function hook_field_storage_purge($entity_type, $entity, $field, $instance) {
2694
  list($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
2695

    
2696
  $table_name = _field_sql_storage_tablename($field);
2697
  $revision_name = _field_sql_storage_revision_tablename($field);
2698
  db_delete($table_name)
2699
    ->condition('entity_type', $entity_type)
2700
    ->condition('entity_id', $id)
2701
    ->execute();
2702
  db_delete($revision_name)
2703
    ->condition('entity_type', $entity_type)
2704
    ->condition('entity_id', $id)
2705
    ->execute();
2706
}
2707

    
2708
/**
2709
 * @} End of "addtogroup field_crud".
2710
 */
2711

    
2712
/**
2713
 * Determine whether the user has access to a given field.
2714
 *
2715
 * This hook is invoked from field_access() to let modules block access to
2716
 * operations on fields. If no module returns FALSE, the operation is allowed.
2717
 *
2718
 * @param $op
2719
 *   The operation to be performed. Possible values: 'edit', 'view'.
2720
 * @param $field
2721
 *   The field on which the operation is to be performed.
2722
 * @param $entity_type
2723
 *   The type of $entity; for example, 'node' or 'user'.
2724
 * @param $entity
2725
 *   (optional) The entity for the operation.
2726
 * @param $account
2727
 *   (optional) The account to check; if not given use currently logged in user.
2728
 *
2729
 * @return
2730
 *   TRUE if the operation is allowed, and FALSE if the operation is denied.
2731
 *
2732
 * @ingroup field_types
2733
 */
2734
function hook_field_access($op, $field, $entity_type, $entity, $account) {
2735
  if ($field['field_name'] == 'field_of_interest' && $op == 'edit') {
2736
    return user_access('edit field of interest', $account);
2737
  }
2738
  return TRUE;
2739
}
2740

    
2741
/**
2742
 * @} End of "addtogroup hooks".
2743
 */