function field_create_instance

Creates an instance of a field, binding it to a bundle.

Parameters

array $instance: A field instance definition array. The field_name, entity_type and bundle properties are required. Other properties, if omitted, will be given the following default values:

  • label: the field name
  • description: empty string
  • required: FALSE
  • default_value_function: empty string
  • settings: each omitted setting is given the default value specified in hook_field_info().
  • widget:
    • type: the default widget specified in hook_field_info().
    • settings: each omitted setting is given the default value specified in hook_field_widget_info().

Return value

\Drupal\field\Plugin\Core\Entity\FieldInstance The field instance entity.

Throws

Drupal\field\FieldException

Deprecated

as of Drupal 8.0. Use entity_create('field_instance', $definition)->save().

See: Field API data structures.

Related topics

122 calls to field_create_instance()

File

drupal/core/modules/field/field.crud.inc, line 206
Field CRUD API, handling field and field instance creation and deletion.

Code

function field_create_instance(array $instance) {
  $instance = entity_create('field_instance', $instance);
  $instance
    ->save();
  return $instance;
}