You are here

public function FieldItemList::applyDefaultValue in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/FieldItemList.php \Drupal\Core\Field\FieldItemList::applyDefaultValue()

Applies the default value.

Parameters

bool $notify: (optional) Whether to notify the parent object of the change. Defaults to TRUE. If a property is updated from a parent object, set it to FALSE to avoid being notified again.

Return value

$this Returns itself to allow for chaining.

Overrides TypedData::applyDefaultValue

File

core/lib/Drupal/Core/Field/FieldItemList.php, line 168

Class

FieldItemList
Represents an entity field; that is, a list of field item objects.

Namespace

Drupal\Core\Field

Code

public function applyDefaultValue($notify = TRUE) {
  if ($value = $this
    ->getFieldDefinition()
    ->getDefaultValue($this
    ->getEntity())) {
    $this
      ->setValue($value, $notify);
  }
  else {

    // Create one field item and give it a chance to apply its defaults.
    // Remove it if this ended up doing nothing.
    // @todo Having to create an item in case it wants to set a value is
    // absurd. Remove that in https://www.drupal.org/node/2356623.
    $item = $this
      ->first() ?: $this
      ->appendItem();
    $item
      ->applyDefaultValue(FALSE);
    $this
      ->filterEmptyItems();
  }
  return $this;
}