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()
ApiDataTest::setUp in drupal/core/modules/field/lib/Drupal/field/Tests/Views/ApiDataTest.php
Sets up a Drupal site for running functional and integration tests.
ArbitraryRebuildTest::setUp in drupal/core/modules/system/lib/Drupal/system/Tests/Form/ArbitraryRebuildTest.php
Sets up a Drupal site for running functional and integration tests.
BulkDeleteTest::setUp in drupal/core/modules/field/lib/Drupal/field/Tests/BulkDeleteTest.php
Set the default field storage backend for fields created during tests.
ContactFieldsTest::setUp in drupal/core/modules/contact/lib/Drupal/contact/Tests/Views/ContactFieldsTest.php
Sets up a Drupal site for running functional and integration tests.
CrudTest::testDeleteField in drupal/core/modules/field/lib/Drupal/field/Tests/CrudTest.php
Test the deletion of a field.

... See full list

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;
}