function KeyValueEntityStorage::doCreate

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage::doCreate()
  2. 10 core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage::doCreate()
  3. 11.x core/lib/Drupal/Core/Entity/KeyValueStore/KeyValueEntityStorage.php \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage::doCreate()

Overrides EntityStorageBase::doCreate

File

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

Class

KeyValueEntityStorage
Provides a key value backend for entities.

Namespace

Drupal\Core\Entity\KeyValueStore

Code

public function doCreate(array $values = []) {
    // Set default language to site default if not provided.
    $values += [
        $this->getEntityType()
            ->getKey('langcode') => $this->languageManager
            ->getDefaultLanguage()
            ->getId(),
    ];
    $entity_class = $this->getEntityClass();
    $entity = new $entity_class($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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.