Change record status: 
Introduced in branch: 

The entity field API now supports the assignment of default values when entity fields are created.

- For base fields, the default values can be statically defined in field definitions (example: "0" for the comment author field of a comment to indicate the anonymous user)
Usually, a default value is specified in the settings array of a field definition, e.g. Comment::baseFieldDefinitions() contains:

['uid'] = FieldDefinition::create('entity_reference')
setLabel(t('User ID'))
setDescription(t('The user ID of the comment author.'))
'target_type' => 'user',
'default_value' => 0,
$fields['name'] = FieldDefinition::create('string')
setDescription(t("The comment author's name."))
'default_value' => '',
'max_length' => 60,
setConstraints(array('CommentName' => array()));

- For configurable fields, the default value is determined by the $field and $instance definition structures as in Drupal 7 (usually in $instance->default_value / $instance->default_value_callback, but some field types implement their own logic by overriding the getDefaultValue() method in their field class as mentioned below).
See also: about the former field_get_default_value() function.

The default value can be specified either as:
- a scalar (in which case it will be applied to the the first property of the first field item)
- an array of property (the properties of the first item)
- a delta-indexed array of property arrays (so the default value can include several items).

If a dynamic default value need to be specified, the getDefaultValue() method can be overridden in the field class:

   * {@inheritdoc}
public function getDefaultValue($notify = TRUE) {
return array('value' => Language::LANGCODE_NOT_SPECIFIED);

The field item's applyDefaultValue() method will be invoked when no default value has been provided in the field definition (and defaults to NULL).

During entity creation fields will be populated with their defaults if no values are provided to entity_create(). For example:

= entity_create('comment', array('node_type' => 'article'));
$comment->uid->value === 0 // TRUE
$comment->name->value === '' // TRUE

Various field types are provides that include useful default values, for example:

  • uuid: Creates a new UUID for a new entity
  • created: Sets the default value to the current timestamp
  • changed: The same as created, the value is additionally updated automatically every time the entity is saved.

See also Defining and using Content Entity Field definitions

Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other updates done