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;
}
doc_Drupal
2016-10-29 09:21:45
Comments
Leave a Comment

Please login to continue.