public function FieldItemList::applyDefaultValue

public FieldItemList::applyDefaultValue($notify = TRUE)

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

\Drupal\Core\TypedData\TypedDataInterface Returns itself to allow for chaining.

Overrides TypedData::applyDefaultValue

File

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

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;
}

© 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!FieldItemList.php/function/FieldItemList::applyDefaultValue/8.1.x