function ContentEntityStorageBase::doCreate

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

Overrides EntityStorageBase::doCreate

File

core/lib/Drupal/Core/Entity/ContentEntityStorageBase.php, line 103

Class

ContentEntityStorageBase
Base class for content entity storage handlers.

Namespace

Drupal\Core\Entity

Code

protected function doCreate(array $values) {
    // We have to determine the bundle first.
    $bundle = FALSE;
    if ($this->bundleKey) {
        if (!isset($values[$this->bundleKey])) {
            throw new EntityStorageException('Missing bundle for entity type ' . $this->entityTypeId);
        }
        // Normalize the bundle value. This is an optimized version of
        // \Drupal\Core\Field\FieldInputValueNormalizerTrait::normalizeValue()
        // because we just need the scalar value.
        $bundle_value = $values[$this->bundleKey];
        if (!is_array($bundle_value)) {
            // The bundle value is a scalar, use it as-is.
            $bundle = $bundle_value;
        }
        elseif (is_numeric(array_keys($bundle_value)[0])) {
            // The bundle value is a field item list array, keyed by delta.
            $bundle = reset($bundle_value[0]);
        }
        else {
            // The bundle value is a field item array, keyed by the field's main
            // property name.
            $bundle = reset($bundle_value);
        }
    }
    $entity = new $this->entityClass([], $this->entityTypeId, $bundle);
    $this->initFieldValues($entity, $values);
    return $entity;
}

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