public function KeyValueEntityStorage::doCreate

public KeyValueEntityStorage::doCreate(array $values = array())

Performs storage-specific creation of entities.

Parameters

array $values: An array of values to set, keyed by property name.

Return value

\Drupal\Core\Entity\EntityInterface

Overrides EntityStorageBase::doCreate

File

core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php, line 89

Class

KeyValueEntityStorage
Provides a key value backend for entities.

Namespace

Drupal\Core\Entity\KeyValueStore

Code

public function doCreate(array $values = array()) {
  // Set default language to site default if not provided.
  $values += array($this->getEntityType()->getKey('langcode') => $this->languageManager->getDefaultLanguage()->getId());
  $entity = new $this->entityClass($values, $this->entityTypeId);

  // @todo This is handled by ContentEntityStorageBase, which assumes
  //   FieldableEntityInterface. The current approach in
  //   https://www.drupal.org/node/1867228 improves this but does not solve it
  //   completely.
  if ($entity instanceof FieldableEntityInterface) {
    foreach ($entity as $name => $field) {
      if (isset($values[$name])) {
        $entity->$name = $values[$name];
      }
      elseif (!array_key_exists($name, $values)) {
        $entity->get($name)->applyDefaultValue();
      }
      unset($values[$name]);
    }
  }

  return $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!KeyValueStore!KeyValueEntityStorage.php/function/KeyValueEntityStorage::doCreate/8.1.x