function ContentEntityBase::create

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::create()
  2. 10 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::create()

Overrides EntityBase::create

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 1160

Class

ContentEntityBase
Implements Entity Field API specific enhancements to the Entity class.

Namespace

Drupal\Core\Entity

Code

public static function create(array $values = []) {
    $entity_type_repository = \Drupal::service('entity_type.repository');
    $entity_type_manager = \Drupal::entityTypeManager();
    $class_name = static::class;
    $storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass($class_name));
    // Always explicitly specify the bundle if the entity has a bundle class.
    if ($storage instanceof BundleEntityStorageInterface && ($bundle = $storage->getBundleFromClass($class_name))) {
        $values[$storage->getEntityType()
            ->getKey('bundle')] = $bundle;
    }
    return $storage->create($values);
}

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