W3cubDocs

/Drupal 8

protected function 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);
}

© 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!Entity!ContentEntityStorageBase.php/function/ContentEntityStorageBase::initFieldValues/8.1.x