function field_create_field

Creates a field.

This function does not bind the field to any bundle; use field_create_instance() for that.

Parameters

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

  • cardinality: 1
  • locked: FALSE
  • indexes: the field-type indexes, specified by the field type's hook_field_schema(). The indexes specified in $field are added to those default indexes. It is possible to override the definition of a field-type index by providing an index with the same name, or to remove it by redefining it as an empty array of columns. Overriding field-type indexes should be done carefully, for it might seriously affect the site's performance.
  • settings: each omitted setting is given the default value defined in hook_field_info().
  • storage:
    • type: the storage backend specified in the 'field.settings.default_storage' configuration.
    • settings: each omitted setting is given the default value specified in hook_field_storage_info().

Return value

\Drupal\field\Plugin\Core\Entity\Field The field entity.

Throws

Drupal\field\FieldException

Deprecated

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

See: Field API data structures.

Related topics

121 calls to field_create_field()
ActiveTest::testActive in drupal/core/modules/field/lib/Drupal/field/Tests/ActiveTest.php
Test that fields are properly marked active or inactive.
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::testCreateField in drupal/core/modules/field/lib/Drupal/field/Tests/CrudTest.php
Test the creation of a field.

... See full list

File

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

Code

function field_create_field(array $field) {
  $field = entity_create('field_entity', $field);
  $field
    ->save();
  return $field;
}