function BaseFieldDefinition::setDefaultValue
Same name and namespace in other branches
- 11.x core/lib/Drupal/Core/Field/BaseFieldDefinition.php \Drupal\Core\Field\BaseFieldDefinition::setDefaultValue()
- 10 core/lib/Drupal/Core/Field/BaseFieldDefinition.php \Drupal\Core\Field\BaseFieldDefinition::setDefaultValue()
- 9 core/lib/Drupal/Core/Field/BaseFieldDefinition.php \Drupal\Core\Field\BaseFieldDefinition::setDefaultValue()
- 8.9.x core/lib/Drupal/Core/Field/BaseFieldDefinition.php \Drupal\Core\Field\BaseFieldDefinition::setDefaultValue()
Sets a default value for the field.
If a default value callback is set, it will take precedence over any value set here.
Parameters
mixed $value: The default value for the field. This can be either:
- a literal, in which case it will be assigned to the first property of the first item.
- a numerically indexed array of items, each item being a property/value array.
- a non-numerically indexed array, in which case the array is assumed to be a property/value array and used as the first item
- NULL or [] for no default value.
Return value
$this
See also
\Drupal\Core\Field\BaseFieldDefinition::setDefaultValueCallback()
File
-
core/
lib/ Drupal/ Core/ Field/ BaseFieldDefinition.php, line 473
Class
- BaseFieldDefinition
- A class for defining entity fields.
Namespace
Drupal\Core\FieldCode
public function setDefaultValue($value) {
if ($value === NULL) {
$value = [];
}
// Unless the value is an empty array, we may need to transform it.
if (!is_array($value) || !empty($value)) {
if (!is_array($value)) {
$value = [
[
$this->getMainPropertyName() => $value,
],
];
}
elseif (is_array($value) && !is_numeric(array_keys($value)[0])) {
$value = [
0 => $value,
];
}
}
$this->definition['default_value'] = $value;
return $this;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.