field.api.php 88.9 KB
Newer Older
1 2 3
<?php

/**
4
 * @addtogroup hooks
5 6 7
 * @{
 */

8 9
use Drupal\field\FieldUpdateForbiddenException;

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

67 68 69
  return $extra;
}

70 71 72 73 74 75 76 77 78
/**
 * Alter "pseudo-field" components on fieldable entities.
 *
 * @param $info
 *   The associative array of 'pseudo-field' components.
 *
 * @see hook_field_extra_fields()
 */
function hook_field_extra_fields_alter(&$info) {
79
  // Force node title to always be at the top of the list by default.
80
  foreach (node_type_get_types() as $bundle) {
81 82
    if (isset($info['node'][$bundle->type]['form']['title'])) {
      $info['node'][$bundle->type]['form']['title']['weight'] = -20;
83
    }
84 85 86
  }
}

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

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

176 177 178 179
/**
 * Perform alterations on Field API field types.
 *
 * @param $info
180
 *   Array of information on field types exposed by hook_field_info()
181 182 183 184 185
 *   implementations.
 */
function hook_field_info_alter(&$info) {
  // Add a setting to all field types.
  foreach ($info as $field_type => $field_type_info) {
186 187 188
    $info[$field_type]['settings'] += array(
      'mymodule_additional_setting' => 'default value',
    );
189 190 191 192 193 194 195 196
  }

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

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

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

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

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

/**
 * Define custom presave behavior for this module's field types.
402
 *
403 404 405
 * Make changes or additions to field values by altering the $items parameter by
 * reference. There is no return value.
 *
406 407 408
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
409
 *   The entity for the operation.
410 411 412
 * @param $field
 *   The field structure for the operation.
 * @param $instance
413
 *   The instance structure for $field on $entity's bundle.
414
 * @param $langcode
415
 *   The language associated with $items.
416
 * @param $items
417
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
418
 */
419
function hook_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
420 421 422 423 424 425 426 427 428
  if ($field['type'] == 'number_decimal') {
    // Let PHP round the value to ensure consistent behavior across storage
    // backends.
    foreach ($items as $delta => $item) {
      if (isset($item['value'])) {
        $items[$delta]['value'] = round($item['value'], $field['settings']['scale']);
      }
    }
  }
429 430 431
}

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

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

    // We don't maintain data for old revisions, so clear all previous values
    // from the table. Since this hook runs once per field, per object, make
    // sure we only wipe values once.
    if (!isset($first_call[$entity->nid])) {
      $first_call[$entity->nid] = FALSE;
      db_delete('taxonomy_index')->condition('nid', $entity->nid)->execute();
    }
    // Only save data to the table if the node is published.
    if ($entity->status) {
      $query = db_insert('taxonomy_index')->fields(array('nid', 'tid', 'sticky', 'created'));
      foreach ($items as $item) {
        $query->values(array(
          'nid' => $entity->nid,
          'tid' => $item['tid'],
          'sticky' => $entity->sticky,
          'created' => $entity->created,
        ));
      }
      $query->execute();
    }
  }
523 524
}

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
/**
 * Update the storage information for a field.
 *
 * This is invoked on the field's storage module from field_update_field(),
 * before the new field information is saved to the database. The field storage
 * module should update its storage tables to agree with the new field
 * information. If there is a problem, the field storage module should throw an
 * exception.
 *
 * @param $field
 *   The updated field structure to be saved.
 * @param $prior_field
 *   The previously-saved field structure.
 * @param $has_data
 *   TRUE if the field has data in storage currently.
 */
function hook_field_storage_update_field($field, $prior_field, $has_data) {
  if (!$has_data) {
    // There is no data. Re-create the tables completely.
    $prior_schema = _field_sql_storage_schema($prior_field);
    foreach ($prior_schema as $name => $table) {
      db_drop_table($name, $table);
    }
    $schema = _field_sql_storage_schema($field);
    foreach ($schema as $name => $table) {
      db_create_table($name, $table);
    }
  }
  else {
    // There is data. See field_sql_storage_field_storage_update_field() for
    // an example of what to do to modify the schema in place, preserving the
    // old data as much as possible.
  }
  drupal_get_schema(NULL, TRUE);
}

561
/**
562
 * Define custom delete behavior for this module's field data.
563
 *
564 565 566 567 568 569
 * This hook is invoked from field_attach_delete() on the module that defines a
 * field, during the process of deleting an entity object (node, taxonomy term,
 * etc.). It is invoked just before the data for this field on the particular
 * entity object is deleted from field storage. Only field modules that are
 * storing or tracking information outside the standard field storage mechanism
 * need to implement this hook.
570
 *
571 572 573
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
574
 *   The entity for the operation.
575 576 577
 * @param $field
 *   The field structure for the operation.
 * @param $instance
578
 *   The instance structure for $field on $entity's bundle.
579
 * @param $langcode
580
 *   The language associated with $items.
581
 * @param $items
582
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
583 584 585
 *
 * @see hook_field_insert()
 * @see hook_field_update()
586
 */
587
function hook_field_delete($entity_type, $entity, $field, $instance, $langcode, &$items) {
588 589 590 591 592 593
  foreach ($items as $delta => $item) {
    // For hook_file_references(), remember that this is being deleted.
    $item['file_field_name'] = $field['field_name'];
    // Pass in the ID of the object that is being removed so all references can
    // be counted in hook_file_references().
    $item['file_field_type'] = $entity_type;
594 595
    $item['file_field_id'] = $entity->id();
    file_field_delete_file($item, $field, $entity_type, $entity->id());
596
  }
597 598 599
}

/**
600
 * Define custom revision delete behavior for this module's field types.
601
 *
602 603 604
 * This hook is invoked just before the data is deleted from field storage in
 * field_attach_delete_revision(), and will only be called for fieldable types
 * that are versioned.
605
 *
606 607 608
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
609
 *   The entity for the operation.
610 611 612
 * @param $field
 *   The field structure for the operation.
 * @param $instance
613
 *   The instance structure for $field on $entity's bundle.
614
 * @param $langcode
615
 *   The language associated with $items.
616
 * @param $items
617
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
618
 */
619
function hook_field_delete_revision($entity_type, $entity, $field, $instance, $langcode, &$items) {
620 621 622
  foreach ($items as $delta => $item) {
    // For hook_file_references, remember that this file is being deleted.
    $item['file_field_name'] = $field['field_name'];
623
    if (file_field_delete_file($item, $field, $entity_type, $entity->id())) {
624 625 626
      $items[$delta] = NULL;
    }
  }
627 628 629
}

/**
630 631
 * Define custom prepare_translation behavior for this module's field types.
 *
632 633 634
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
635
 *   The entity for the operation.
636 637 638
 * @param $field
 *   The field structure for the operation.
 * @param $instance
639
 *   The instance structure for $field on $entity's bundle.
640
 * @param $langcode
641
 *   The language associated with $items.
642
 * @param $items
643
 *   $entity->{$field['field_name']}[$langcode], or an empty array if unset.
644 645 646 647
 * @param $source_entity
 *   The source entity from which field values are being copied.
 * @param $source_langcode
 *   The source language from which field values are being copied.
648
 */
649 650 651 652 653 654 655 656 657
function hook_field_prepare_translation($entity_type, $entity, $field, $instance, $langcode, &$items, $source_entity, $source_langcode) {
  // If the translating user is not permitted to use the assigned text format,
  // we must not expose the source values.
  $field_name = $field['field_name'];
  $formats = filter_formats();
  $format_id = $source_entity->{$field_name}[$source_langcode][0]['format'];
  if (!filter_access($formats[$format_id])) {
    $items = array();
  }
658 659
}

660 661 662 663 664 665 666
/**
 * Define what constitutes an empty item for a field type.
 *
 * @param $item
 *   An item that may or may not be empty.
 * @param $field
 *   The field to which $item belongs.
667
 *
668
 * @return
669 670
 *   TRUE if $field's type considers $item not to contain any data; FALSE
 *   otherwise.
671 672
 */
function hook_field_is_empty($item, $field) {
673
  if (empty($item['value']) && (string) $item['value'] !== '0') {
674 675 676 677 678
    return TRUE;
  }
  return FALSE;
}

679
/**
680
 * @} End of "defgroup field_types".
681 682 683 684 685 686 687 688 689 690
 */

/**
 * @defgroup field_widget Field Widget API
 * @{
 * Define Field API widget types.
 *
 * Field API widgets specify how fields are displayed in edit forms. Fields of a
 * given @link field_types field type @endlink may be edited using more than one
 * widget. In this case, the Field UI module allows the site builder to choose
691 692 693 694 695 696 697 698
 * which widget to use.
 *
 * Widgets are Plugins managed by the
 * Drupal\field\Plugin\Type\Widget\WidgetPluginManager class. A widget is
 * implemented by providing a class that implements
 * Drupal\field\Plugin\Type\Widget\WidgetInterface (in most cases, by
 * subclassing Drupal\field\Plugin\Type\Widget\WidgetBase), and provides the
 * proper annotation block.
699
 *
700
 * Widgets are @link forms_api_reference.html Form API @endlink
701 702 703
 * elements with additional processing capabilities. The methods of the
 * WidgetInterface object are typically called by the Field Attach API during
 * the creation of the field form structure with field_attach_form().
704 705 706 707 708 709
 *
 * @see field
 * @see field_types
 * @see field_formatter
 */

710 711 712
/**
 * Perform alterations on Field API widget types.
 *
713 714 715
 * @param array $info
 *   An array of informations on existing widget types, as collected by the
 *   annotation discovery mechanism.
716
 */
717
function hook_field_widget_info_alter(array &$info) {
718 719 720 721 722 723
  // Add a setting to a widget type.
  $info['text_textfield']['settings'] += array(
    'mymodule_additional_setting' => 'default value',
  );

  // Let a new field type re-use an existing widget.
724
  $info['options_select']['field_types'][] = 'my_field_type';
725 726
}

727 728 729 730 731 732 733 734 735 736
/**
 * Alter forms for field widgets provided by other modules.
 *
 * @param $element
 *   The field widget form element as constructed by hook_field_widget_form().
 * @param $form_state
 *   An associative array containing the current state of the form.
 * @param $context
 *   An associative array containing the following key-value pairs, matching the
 *   arguments received by hook_field_widget_form():
737 738 739 740 741 742 743
 *   - form: The form structure to which widgets are being attached. This may be
 *     a full form structure, or a sub-element of a larger form.
 *   - field: The field structure.
 *   - instance: The field instance structure.
 *   - langcode: The language associated with $items.
 *   - items: Array of default values for this field.
 *   - delta: The order of this item in the array of subelements (0, 1, 2, etc).
744 745
 *   - default: A boolean indicating whether the form is being shown as a dummy
 *     form to set default values.
746 747
 *
 * @see hook_field_widget_form()
748
 * @see hook_field_widget_WIDGET_TYPE_form_alter()
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
 */
function hook_field_widget_form_alter(&$element, &$form_state, $context) {
  // Add a css class to widget form elements for all fields of type mytype.
  if ($context['field']['type'] == 'mytype') {
    // Be sure not to overwrite existing attributes.
    $element['#attributes']['class'][] = 'myclass';
  }
}

/**
 * Alter widget forms for a specific widget provided by another module.
 *
 * Modules can implement hook_field_widget_WIDGET_TYPE_form_alter() to modify a
 * specific widget form, rather than using hook_field_widget_form_alter() and
 * checking the widget type.
 *
 * @param $element
 *   The field widget form element as constructed by hook_field_widget_form().
 * @param $form_state
 *   An associative array containing the current state of the form.
 * @param $context
 *   An associative array containing the following key-value pairs, matching the
 *   arguments received by hook_field_widget_form():
 *   - "form": The form structure where widgets are being attached to. This
 *     might be a full form structure, or a sub-element of a larger form.
 *   - "field": The field structure.
 *   - "instance": The field instance structure.
 *   - "langcode": The language associated with $items.
 *   - "items": Array of default values for this field.
 *   - "delta": The order of this item in the array of subelements (0, 1, 2,
 *     etc).
780 781
 *   - default: A boolean indicating whether the form is being shown as a dummy
 *     form to set default values.
782 783 784 785 786 787 788 789 790 791 792
 *
 * @see hook_field_widget_form()
 * @see hook_field_widget_form_alter()
 */
function hook_field_widget_WIDGET_TYPE_form_alter(&$element, &$form_state, $context) {
  // Code here will only act on widgets of type WIDGET_TYPE.  For example,
  // hook_field_widget_mymodule_autocomplete_form_alter() will only act on
  // widgets of type 'mymodule_autocomplete'.
  $element['#autocomplete_path'] = 'mymodule/autocomplete_path';
}

793 794 795 796 797 798 799 800 801 802 803 804
/**
 * Alters the widget properties of a field instance before it gets displayed.
 *
 * Note that instead of hook_field_widget_properties_alter(), which is called
 * for all fields on all entity types,
 * hook_field_widget_properties_ENTITY_TYPE_alter() may be used to alter widget
 * properties for fields on a specific entity type only.
 *
 * This hook is called once per field per added or edit entity. If the result
 * of the hook involves reading from the database, it is highly recommended to
 * statically cache the information.
 *
805
 * @param array $widget_properties
806
 *   The instance's widget properties.
807
 * @param array $context
808 809
 *   An associative array containing:
 *   - entity_type: The entity type; e.g., 'node' or 'user'.
810
 *   - bundle: The bundle: e.g., 'page' or 'article'.
811 812 813 814 815
 *   - field: The field that the widget belongs to.
 *   - instance: The instance of the field.
 *
 * @see hook_field_widget_properties_ENTITY_TYPE_alter()
 */
816
function hook_field_widget_properties_alter(array &$widget_properties, array $context) {
817 818 819 820
  // Change a widget's type according to the time of day.
  $field = $context['field'];
  if ($context['entity_type'] == 'node' && $field['field_name'] == 'field_foo') {
    $time = date('H');
821
    $widget_properties['type'] = $time < 12 ? 'widget_am' : 'widget_pm';
822 823 824
  }
}

825
/**
826
 * @} End of "defgroup field_widget".
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
 */


/**
 * @defgroup field_formatter Field Formatter API
 * @{
 * Define Field API formatter types.
 *
 * Field API formatters specify how fields are displayed when the entity to
 * which the field is attached is displayed. Fields of a given
 * @link field_types field type @endlink may be displayed using more than one
 * formatter. In this case, the Field UI module allows the site builder to
 * choose which formatter to use. Field formatters are defined by implementing
 * hook_field_formatter_info().
 *
 * @see field
 * @see field_types
 * @see field_widget
 */

847 848 849
/**
 * Expose Field API formatter types.
 *
850 851 852
 * Formatters handle the display of field values. Formatter hooks are typically
 * called by the Field Attach API field_attach_prepare_view() and
 * field_attach_view() functions.
853 854
 *
 * @return
855 856 857 858
 *   An array describing the formatter types implemented by the module. The keys
 *   are formatter type names. To avoid name clashes, formatter type names
 *   should be prefixed with the name of the module that exposes them. The
 *   values are arrays describing the formatter type, with the following
859 860
 *   key/value pairs:
 *   - label: The human-readable name of the formatter type.
861
 *   - description: A short description of the formatter type.
862
 *   - field types: An array of field types the formatter supports.
863 864 865
 *   - settings: An array whose keys are the names of the settings available to
 *     the formatter type, and whose values are the default values for those
 *     settings.
866 867 868 869
 *
 * @see hook_field_formatter_info_alter()
 * @see hook_field_formatter_view()
 * @see hook_field_formatter_prepare_view()
870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
 */
function hook_field_formatter_info() {
  return array(
    'text_default' => array(
      'label' => t('Default'),
      'field types' => array('text', 'text_long', 'text_with_summary'),
    ),
    'text_plain' => array(
      'label' => t('Plain text'),
      'field types' => array('text', 'text_long', 'text_with_summary'),
    ),

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

    // The 'summary or trimmed' field formatter for text_with_summary
    // fields displays returns the summary element of the field or, if
    // the summary is empty, the trimmed version of the full element
    // of the field.
    'text_summary_or_trimmed' => array(
      'label' => t('Summary or trimmed'),
      'field types' => array('text_with_summary'),
    ),
  );
}

/**
 * Perform alterations on Field API formatter types.
 *
 * @param $info
907 908
 *   An array of information on formatter types exposed by
 *   hook_field_formatter_info() implementations.
909 910 911 912 913 914 915 916 917 918 919 920
 */
function hook_field_formatter_info_alter(&$info) {
  // Add a setting to a formatter type.
  $info['text_default']['settings'] += array(
    'mymodule_additional_setting' => 'default value',
  );

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

/**
921
 * Allow formatters to load information for field values being displayed.
922 923 924
 *
 * This should be used when a formatter needs to load additional information
 * from the database in order to render a field, for example a reference field
925
 * that displays properties of the referenced entities such as name or type.
926
 *
927 928
 * This hook is called after the field type's own hook_field_prepare_view().
 *
929
 * Unlike most other field hooks, this hook operates on multiple entities. The
930
 * $entities, $instances and $items parameters are arrays keyed by entity ID.
931
 * For performance reasons, information for all available entities should be
932 933
 * loaded in a single query where possible.
 *
934 935 936
 * Changes or additions to field values are done by alterings the $items
 * parameter by reference.
 *
937 938 939
 * @param $entity_type
 *   The type of $entity.
 * @param $entities
940
 *   Array of entities being displayed, keyed by entity ID.
941 942 943
 * @param $field
 *   The field structure for the operation.
 * @param $instances
944
 *   Array of instance structures for $field for each entity, keyed by entity
945
 *   ID.
946 947 948 949
 * @param $langcode
 *   The language the field values are to be shown in. If no language is
 *   provided the current language is used.
 * @param $items
950
 *   Array of field values for the entities, keyed by entity ID.
951
 * @param $displays
952
 *   Array of display settings to use for each entity, keyed by entity ID.
953
 */
954
function hook_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items, $displays) {
955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
  $tids = array();

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

  if ($tids) {
    $terms = taxonomy_term_load_multiple($tids);

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

      foreach ($items[$id] as $delta => $item) {
        // Check whether the taxonomy term field instance value could be loaded.
        if (isset($terms[$item['tid']])) {
          // Replace the instance value with the term data.
          $items[$id][$delta]['taxonomy_term'] = $terms[$item['tid']];
        }
        // Otherwise, unset the instance value, since the term does not exist.
        else {
          unset($items[$id][$delta]);
          $rekey = TRUE;
        }
      }

      if ($rekey) {
        // Rekey the items array.
        $items[$id] = array_values($items[$id]);
      }
    }
  }
992
}
993

994
/**
995
 * Build a renderable array for a field value.
996
 *
997 998 999
 * @param $entity_type
 *   The type of $entity.
 * @param $entity
1000
 *   The entity being displayed.
1001 1002 1003 1004 1005
 * @param $field
 *   The field structure.
 * @param $instance
 *   The field instance.
 * @param $langcode
1006
 *   The language associated with $items.
1007 1008
 * @param $items
 *   Array of values for this field.
1009 1010
 * @param $display
 *   The display settings to use, as found in the 'display' entry of instance
1011
 *   definitions. The array notably contains the following keys and values:
1012 1013
 *   - type: The name of the formatter to use.
 *   - settings: The array of formatter settings.
1014 1015
 *
 * @return
1016 1017
 *   A renderable array for $items, as an array of child elements keyed by
 *   numeric indexes starting from 0.
1018
 */
1019
function hook_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
Dries's avatar