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