public function FieldConfigBase::setDefaultValue

public FieldConfigBase::setDefaultValue($value)

Sets a default value.

Note that 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 array() for no default value.

Return value

$this

Overrides FieldConfigInterface::setDefaultValue

File

core/lib/Drupal/Core/Field/FieldConfigBase.php, line 408

Class

FieldConfigBase
Base class for configurable field definitions.

Namespace

Drupal\Core\Field

Code

public function setDefaultValue($value) {
  if (!is_array($value)) {
    if ($value === NULL) {
      $value = [];
    }
    $key = $this->getFieldStorageDefinition()->getPropertyNames()[0];
    // Convert to the multi value format to support fields with a cardinality
    // greater than 1.
    $value = array(
      array($key => $value),
    );
  }
  $this->default_value = $value;
  return $this;
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Field!FieldConfigBase.php/function/FieldConfigBase::setDefaultValue/8.1.x