function BaseFieldDefinition::setDefaultValue

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/BaseFieldDefinition.php \Drupal\Core\Field\BaseFieldDefinition::setDefaultValue()
  2. 8.9.x core/lib/Drupal/Core/Field/BaseFieldDefinition.php \Drupal\Core\Field\BaseFieldDefinition::setDefaultValue()
  3. 10 core/lib/Drupal/Core/Field/BaseFieldDefinition.php \Drupal\Core\Field\BaseFieldDefinition::setDefaultValue()

File

core/lib/Drupal/Core/Field/BaseFieldDefinition.php, line 456

Class

BaseFieldDefinition
A class for defining entity fields.

Namespace

Drupal\Core\Field

Code

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.