You are here

protected function FieldableEdgeEntityStorageBase::initFieldValues in Apigee Edge 8

Initializes field values.

Parameters

\Drupal\apigee_edge\Entity\FieldableEdgeEntityInterface $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.

See also

\Drupal\Core\Entity\ContentEntityStorageBase::initFieldValues()

2 calls to FieldableEdgeEntityStorageBase::initFieldValues()
AppStorage::initFieldValues in src/Entity/Storage/AppStorage.php
Initializes field values.
FieldableEdgeEntityStorageBase::doCreate in src/Entity/Storage/FieldableEdgeEntityStorageBase.php
1 method overrides FieldableEdgeEntityStorageBase::initFieldValues()
AppStorage::initFieldValues in src/Entity/Storage/AppStorage.php
Initializes field values.

File

src/Entity/Storage/FieldableEdgeEntityStorageBase.php, line 185

Class

FieldableEdgeEntityStorageBase
Base class for fieldable Apigee Edge entities.

Namespace

Drupal\apigee_edge\Entity\Storage

Code

protected function initFieldValues(FieldableEdgeEntityInterface $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
          ->set($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) {
    try {
      $entity
        ->setPropertyValue($name, $value);
    } catch (InvalidArgumentException $exception) {

      // Property not found, which could be fine.
    }
  }

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