You are here

public static function ContentEntityBase::create in Drupal 10

Constructs a new entity object, without permanently saving it.

Parameters

array $values: (optional) An array of values to set, keyed by property name. If the entity type has bundles, the bundle key has to be specified.

Return value

static The entity object.

Overrides EntityBase::create

File

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

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