function hook_field_schema

Define the Field API schema for a field structure.

This hook MUST be defined in .install for it to be detected during installation and upgrade.

Parameters

$field: A field structure.

Return value

An associative array with the following keys:

  • 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.
  • indexes: (optional) An array of Schema API index definitions. Only columns that appear in the 'columns' array are allowed. Those indexes will be used as default indexes. Individual field definitions can specify additional indexes or modify, at their own risk, the indexes specified by the field type. Some storage engines might not support indexes.
  • foreign keys: (optional) An array of Schema API foreign key definitions. Note, however, that the field data is not necessarily stored in SQL. Also, the possible usage is limited, as you cannot specify another field as related, only existing SQL tables, such as {taxonomy_term_data}.

Related topics

12 functions implement hook_field_schema()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

datetime_field_schema in drupal/core/modules/datetime/datetime.install
Implements hook_field_schema().
email_field_schema in drupal/core/modules/email/email.install
Implements hook_field_schema().
entity_reference_field_schema in drupal/core/modules/entity_reference/entity_reference.install
Implements hook_field_schema().
field_test_field_schema in drupal/core/modules/field/tests/modules/field_test/field_test.install
Implements hook_field_schema().
file_field_schema in drupal/core/modules/file/file.install
Implements hook_field_schema().

... See full list

1 invocation of hook_field_schema()
Field::getSchema in drupal/core/modules/field/lib/Drupal/field/Plugin/Core/Entity/Field.php
Returns the field schema.

File

drupal/core/modules/field/field.api.php, line 239

Code

function hook_field_schema($field) {
  if ($field['type'] == 'text_long') {
    $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(
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
    ),
  );
  return array(
    'columns' => $columns,
    'indexes' => array(
      'format' => array(
        'format',
      ),
    ),
    'foreign keys' => array(
      'format' => array(
        'table' => 'filter_format',
        'columns' => array(
          'format' => 'format',
        ),
      ),
    ),
  );
}