function hook_field_info

You are here

7 field.api.php hook_field_info()

Define Field API field types.

Along with this hook, you also need to implement other hooks. See Field Types API for more information.

Return value

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 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).
  • default_widget: The machine name of the default widget to be used by 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).
  • 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).
  • 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.

See also

hook_field_info_alter()

Related topics

7 functions implement hook_field_info()

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

field_test_field_info in modules/field/tests/field_test.field.inc
Implements hook_field_info().
file_field_info in modules/file/file.field.inc
Implements hook_field_info().
image_field_info in modules/image/image.field.inc
Implements hook_field_info().
list_field_info in modules/field/modules/list/list.module
Implements hook_field_info().
number_field_info in modules/field/modules/number/number.module
Implements hook_field_info().

... See full list

5 invocations of hook_field_info()
aggregator_form_aggregator_admin_form_alter in modules/aggregator/aggregator.processor.inc
Implements hook_form_aggregator_admin_form_alter().
field_associate_fields in modules/field/field.module
Allows a module to update the database for fields and columns it controls.
field_help in modules/field/field.module
Implements hook_help().
field_system_info_alter in modules/field/field.module
Implements hook_system_info_alter().
_field_info_collate_types in modules/field/field.info.inc
Collates all information on field types, widget types and related structures.

File

modules/field/field.api.php, line 152

Code

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',
    ),
    'text_long' => array(
      'label' => t('Long text'),
      'description' => t('This field stores long text in the database.'),
      'settings' => array('max_length' => ''),
      'instance_settings' => array('text_processing' => 0),
      'default_widget' => 'text_textarea',
      'default_formatter' => 'text_default',
    ),
    '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',
    ),
  );
}

Comments

Even if a field doesn't have any instance settings, it still needs to define an instance_settings array as of Drupal 7.0-RC1 due to an array merge operator in user_field_info_alter().

no, _field_info_collate_fields() takes care of adding empty 'settings' and 'instance_settings' arrays if they are left undefined in hook_field_info().
Same goes for hook_field_widget_info() and hook_field_formatter_info().

You might think this is also the place to set default cardinality (maximum number of values) for a field, but it isn't (at least, not yet).

You need to use hook_form_alter() and override the form itself. Here's an example of how to set unlimited cardinality for a field.

You can define cardinality for your field in this function field_create_field()

Data collected from this hook can be obtained with field_info_field_types.

If you're having problems getting your fields to appear in the Fields UI screens, be sure you have a formatter and widget available to handle your custom field type. This is done in your hook_field_widget_info() and hook_field_formatter_info() implementations. If your field type is compatible with existing formatters or widgets you can use hook_field_formatter_info_alter() and
hook_field_widget_info_alter().

The following list of hooks are not all needed but may be needed depending on the field type to define a custom field:

hook_field_info
hook_field_formatter_info
hook_field_formatter_info_alter
hook_field_widget_info
hook_field_widget_info_alter
hook_field_is_empty
hook_field_instance_settings_form
hook_field_formatter_info
hook_field_formatter_view

For a custom field that requires additional settings and view formatting the following list may be useful:
hook_field_info
hook_field_formatter_info
hook_field_formatter_info_alter
hook_field_widget_info
hook_field_widget_info_alter
hook_field_is_empty
hook_field_instance_settings_form
hook_field_formatter_info
hook_field_prepare_view
hook_field_formatter_prepare_view
hook_field_formatter_view

If you see someone using a property that is not defined here, this may explain why
http://drupal.stackexchange.com/questions/34823/what-does-the-property-t...