You are here

public function KeyValueEntityStorage::doCreate in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage::doCreate()

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 94
Contains \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage.

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;
}