ContentEntityStorageBase::initFieldValues

protected ContentEntityStorageBase::initFieldValues(ContentEntityInterface $entity, array $values = [], array $field_names = [])

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.

File

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

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);
}
doc_Drupal
2016-10-29 08:57:34
Comments
Leave a Comment

Please login to continue.