You are here

function entity_create in Drupal 8

Constructs a new entity object, without permanently saving it.

Parameters

string $entity_type: The type of the entity.

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

\Drupal\Core\Entity\EntityInterface A new entity object.

Deprecated

in drupal:8.0.0 and is removed from drupal:9.0.0. Use The method overriding Entity::create() for the entity type, e.g. \Drupal\node\Entity\Node::create() if the entity type is known. If the entity type is variable, use the entity storage's create() method to construct a new entity:

\Drupal::entityTypeManager()
  ->getStorage($entity_type)
  ->create($values);

See also

https://www.drupal.org/node/2266845

\Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()

\Drupal\Core\Entity\EntityStorageInterface::create()

1 call to entity_create()
EntityLegacyTest::testEntityCreate in core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php
Tests deprecation of the entity_create() function.
3 string references to 'entity_create'
KeyValueEntityStorageTest::testCreate in core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php
@covers ::create @covers ::doCreate
KeyValueEntityStorageTest::testCreateWithoutUuidKey in core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php
@covers ::create @covers ::doCreate
KeyValueEntityStorageTest::testCreateWithPredefinedUuid in core/tests/Drupal/Tests/Core/Entity/KeyValueStore/KeyValueEntityStorageTest.php
@covers ::create @covers ::doCreate

File

core/includes/entity.inc, line 281
Entity API for handling entities like nodes or users.

Code

function entity_create($entity_type, array $values = []) {
  @trigger_error('entity_create() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the create() method of the entity type class directly or \\Drupal::entityTypeManager()->getStorage($entity_type)->create($values) instead. See https://www.drupal.org/node/2266845', E_USER_DEPRECATED);
  return \Drupal::entityManager()
    ->getStorage($entity_type)
    ->create($values);
}