function BaseFieldDefinition::setInitialValue

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

Sets an initial value for the field.

Parameters

mixed $value: The initial 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;
  • an empty array for no initial value.

Return value

$this

1 call to BaseFieldDefinition::setInitialValue()
BaseFieldDefinition::setInitialValueFromField in core/lib/Drupal/Core/Field/BaseFieldDefinition.php
Sets a field that will be used for getting initial values.

File

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

Class

BaseFieldDefinition
A class for defining entity fields.

Namespace

Drupal\Core\Field

Code

public function setInitialValue($value) {
    // @todo Implement initial value support for multi-value fields in
    //   https://www.drupal.org/node/2883851.
    if ($this->isMultiple()) {
        throw new FieldException('Multi-value fields can not have an initial value.');
    }
    $this->definition['initial_value'] = $this->normalizeValue($value, $this->getMainPropertyName());
    return $this;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.