You are here

protected function ContentEntityStorageBase::initFieldValues in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::initFieldValues()
  2. 9 core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php \Drupal\Core\Entity\ContentEntityStorageBase::initFieldValues()

Initializes field values.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: An entity object.

array $values: (optional) An associative array of initial field values keyed by field name. If none is provided default values will be applied.

array $field_names: (optional) An associative array of field names to be initialized. If none is provided all fields will be initialized.

2 calls to ContentEntityStorageBase::initFieldValues()
ContentEntityStorageBase::createTranslation in core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php
Constructs a new entity translation object, without permanently saving it.
ContentEntityStorageBase::doCreate in core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php
Performs storage-specific creation of entities.

File

core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php, line 266

Class

ContentEntityStorageBase
Base class for content entity storage handlers.

Namespace

Drupal\Core\Entity

Code

protected function initFieldValues(ContentEntityInterface $entity, array $values = [], array $field_names = []) {

  // Populate field values.
  foreach ($entity as $name => $field) {
    if (!$field_names || isset($field_names[$name])) {
      if (isset($values[$name])) {
        $entity->{$name} = $values[$name];
      }
      elseif (!array_key_exists($name, $values)) {
        $entity
          ->get($name)
          ->applyDefaultValue();
      }
    }
    unset($values[$name]);
  }

  // Set any passed values for non-defined fields also.
  foreach ($values as $name => $value) {
    $entity->{$name} = $value;
  }

  // Make sure modules can alter field initial values.
  $this
    ->invokeHook('field_values_init', $entity);
}