Commit 9b1690c2 authored by larowlan's avatar larowlan

Issue #2928930 by andypost, mikelutz, anya_m, Berdir: Properly deprecate...

Issue #2928930 by andypost, mikelutz, anya_m, Berdir: Properly deprecate entity_create() and remove all occurrences
parent bfe4794d
...@@ -271,10 +271,12 @@ function entity_delete_multiple($entity_type, array $ids) { ...@@ -271,10 +271,12 @@ function entity_delete_multiple($entity_type, array $ids) {
* \Drupal::entityTypeManager()->getStorage($entity_type)->create($values); * \Drupal::entityTypeManager()->getStorage($entity_type)->create($values);
* @endcode * @endcode
* *
* @see https://www.drupal.org/node/2266845
* @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage() * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage()
* @see \Drupal\Core\Entity\EntityStorageInterface::create() * @see \Drupal\Core\Entity\EntityStorageInterface::create()
*/ */
function entity_create($entity_type, array $values = []) { 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() return \Drupal::entityManager()
->getStorage($entity_type) ->getStorage($entity_type)
->create($values); ->create($values);
......
...@@ -87,8 +87,6 @@ public static function createFromBaseFieldDefinition(BaseFieldDefinition $base_f ...@@ -87,8 +87,6 @@ public static function createFromBaseFieldDefinition(BaseFieldDefinition $base_f
* (optional) The type of the entity to create. Defaults to * (optional) The type of the entity to create. Defaults to
* 'base_field_override'. * 'base_field_override'.
* *
* @see entity_create()
*
* @throws \Drupal\Core\Field\FieldException * @throws \Drupal\Core\Field\FieldException
* Exception thrown if $values does not contain a field_name, entity_type or * Exception thrown if $values does not contain a field_name, entity_type or
* bundle value. * bundle value.
......
...@@ -56,21 +56,20 @@ protected function setUp($import_test_views = TRUE) { ...@@ -56,21 +56,20 @@ protected function setUp($import_test_views = TRUE) {
/** /**
* Creates a custom block. * Creates a custom block.
* *
* @param array $settings * @param array $values
* (optional) An associative array of settings for the block_content, as * (optional) The values for the block_content entity.
* used in entity_create().
* *
* @return \Drupal\block_content\Entity\BlockContent * @return \Drupal\block_content\Entity\BlockContent
* Created custom block. * Created custom block.
*/ */
protected function createBlockContent(array $settings = []) { protected function createBlockContent(array $values = []) {
$status = 0; $status = 0;
$settings += [ $values += [
'info' => $this->randomMachineName(), 'info' => $this->randomMachineName(),
'type' => 'basic', 'type' => 'basic',
'langcode' => 'en', 'langcode' => 'en',
]; ];
if ($block_content = BlockContent::create($settings)) { if ($block_content = BlockContent::create($values)) {
$status = $block_content->save(); $status = $block_content->save();
} }
$this->assertEqual($status, SAVED_NEW, new FormattableMarkup('Created block content %info.', ['%info' => $block_content->label()])); $this->assertEqual($status, SAVED_NEW, new FormattableMarkup('Created block content %info.', ['%info' => $block_content->label()]));
......
...@@ -51,21 +51,20 @@ protected function setUp($import_test_views = TRUE) { ...@@ -51,21 +51,20 @@ protected function setUp($import_test_views = TRUE) {
/** /**
* Creates a custom block. * Creates a custom block.
* *
* @param array $settings * @param array $values
* (optional) An associative array of settings for the block_content, as * (optional) The values for the block_content entity.
* used in entity_create().
* *
* @return \Drupal\block_content\Entity\BlockContent * @return \Drupal\block_content\Entity\BlockContent
* Created custom block. * Created custom block.
*/ */
protected function createBlockContent(array $settings = []) { protected function createBlockContent(array $values = []) {
$status = 0; $status = 0;
$settings += [ $values += [
'info' => $this->randomMachineName(), 'info' => $this->randomMachineName(),
'type' => 'basic', 'type' => 'basic',
'langcode' => 'en', 'langcode' => 'en',
]; ];
if ($block_content = BlockContent::create($settings)) { if ($block_content = BlockContent::create($values)) {
$status = $block_content->save(); $status = $block_content->save();
} }
$this->assertEqual($status, SAVED_NEW, new FormattableMarkup('Created block content %info.', ['%info' => $block_content->label()])); $this->assertEqual($status, SAVED_NEW, new FormattableMarkup('Created block content %info.', ['%info' => $block_content->label()]));
......
...@@ -125,7 +125,9 @@ public function testImageFieldSync() { ...@@ -125,7 +125,9 @@ public function testImageFieldSync() {
'user_id' => mt_rand(1, 128), 'user_id' => mt_rand(1, 128),
'langcode' => $default_langcode, 'langcode' => $default_langcode,
]; ];
$entity = entity_create($this->entityTypeId, $values); $entity = \Drupal::entityTypeManager()
->getStorage($this->entityTypeId)
->create($values);
// Create some file entities from the generated test files and store them. // Create some file entities from the generated test files and store them.
$values = []; $values = [];
......
...@@ -90,8 +90,6 @@ class FieldConfig extends FieldConfigBase implements FieldConfigInterface { ...@@ -90,8 +90,6 @@ class FieldConfig extends FieldConfigBase implements FieldConfigInterface {
* bundle to which the field is attached to. Other array elements will be * bundle to which the field is attached to. Other array elements will be
* used to set the corresponding properties on the class; see the class * used to set the corresponding properties on the class; see the class
* property documentation for details. * property documentation for details.
*
* @see entity_create()
*/ */
public function __construct(array $values, $entity_type = 'field_config') { public function __construct(array $values, $entity_type = 'field_config') {
// Allow either an injected FieldStorageConfig object, or a field_name and // Allow either an injected FieldStorageConfig object, or a field_name and
......
...@@ -243,8 +243,6 @@ class FieldStorageConfig extends ConfigEntityBase implements FieldStorageConfigI ...@@ -243,8 +243,6 @@ class FieldStorageConfig extends ConfigEntityBase implements FieldStorageConfigI
* a 'field_name' property can be accepted in place of 'id'. * a 'field_name' property can be accepted in place of 'id'.
* - entity_type: required. * - entity_type: required.
* - type: required. * - type: required.
*
* @see entity_create()
*/ */
public function __construct(array $values, $entity_type = 'field_storage_config') { public function __construct(array $values, $entity_type = 'field_storage_config') {
// Check required properties. // Check required properties.
......
...@@ -286,7 +286,7 @@ protected function setUpContentTypes() { ...@@ -286,7 +286,7 @@ protected function setUpContentTypes() {
*/ */
protected function createReferencedEntityWithTranslation() { protected function createReferencedEntityWithTranslation() {
/** @var \Drupal\node\Entity\Node $node */ /** @var \Drupal\node\Entity\Node $node */
$node = entity_create($this->testEntityTypeName, [ $node = \Drupal::entityTypeManager()->getStorage($this->testEntityTypeName)->create([
'title' => $this->originalLabel, 'title' => $this->originalLabel,
'type' => $this->referencedType->id(), 'type' => $this->referencedType->id(),
'description' => [ 'description' => [
...@@ -309,7 +309,7 @@ protected function createReferencedEntityWithTranslation() { ...@@ -309,7 +309,7 @@ protected function createReferencedEntityWithTranslation() {
*/ */
protected function createNotTranslatedReferencedEntity() { protected function createNotTranslatedReferencedEntity() {
/** @var \Drupal\node\Entity\Node $node */ /** @var \Drupal\node\Entity\Node $node */
$node = entity_create($this->testEntityTypeName, [ $node = \Drupal::entityTypeManager()->getStorage($this->testEntityTypeName)->create([
'title' => $this->labelOfNotTranslatedReference, 'title' => $this->labelOfNotTranslatedReference,
'type' => $this->referencedType->id(), 'type' => $this->referencedType->id(),
'description' => [ 'description' => [
...@@ -328,7 +328,7 @@ protected function createNotTranslatedReferencedEntity() { ...@@ -328,7 +328,7 @@ protected function createNotTranslatedReferencedEntity() {
*/ */
protected function createReferrerEntity($translatable = TRUE) { protected function createReferrerEntity($translatable = TRUE) {
/** @var \Drupal\node\Entity\Node $node */ /** @var \Drupal\node\Entity\Node $node */
$node = entity_create($this->testEntityTypeName, [ $node = \Drupal::entityTypeManager()->getStorage($this->testEntityTypeName)->create([
'title' => $this->randomMachineName(), 'title' => $this->randomMachineName(),
'type' => $this->referrerType->id(), 'type' => $this->referrerType->id(),
'description' => [ 'description' => [
......
...@@ -99,7 +99,7 @@ public function testReEnabledField() { ...@@ -99,7 +99,7 @@ public function testReEnabledField() {
// Add another telephone field to a different entity type in order to test // Add another telephone field to a different entity type in order to test
// the message for the case when multiple fields are blocking the // the message for the case when multiple fields are blocking the
// uninstallation of a module. // uninstallation of a module.
$field_storage2 = entity_create('field_storage_config', [ $field_storage2 = FieldStorageConfig::create([
'field_name' => 'field_telephone_2', 'field_name' => 'field_telephone_2',
'entity_type' => 'user', 'entity_type' => 'user',
'type' => 'telephone', 'type' => 'telephone',
......
...@@ -250,7 +250,8 @@ public function testEntityFormatterRecursiveRendering() { ...@@ -250,7 +250,8 @@ public function testEntityFormatterRecursiveRendering() {
]) ])
->save(); ->save();
$referencing_entity_1 = entity_create($this->entityType, ['name' => $this->randomMachineName()]); $storage = \Drupal::entityTypeManager()->getStorage($this->entityType);
$referencing_entity_1 = $storage->create(['name' => $this->randomMachineName()]);
$referencing_entity_1->save(); $referencing_entity_1->save();
// Create a self-reference. // Create a self-reference.
...@@ -273,7 +274,7 @@ public function testEntityFormatterRecursiveRendering() { ...@@ -273,7 +274,7 @@ public function testEntityFormatterRecursiveRendering() {
// Repeat the process with another entity in order to check that the // Repeat the process with another entity in order to check that the
// 'recursive_render_id' counter is generated properly. // 'recursive_render_id' counter is generated properly.
$referencing_entity_2 = entity_create($this->entityType, ['name' => $this->randomMachineName()]); $referencing_entity_2 = $storage->create(['name' => $this->randomMachineName()]);
$referencing_entity_2->save(); $referencing_entity_2->save();
$referencing_entity_2->{$this->fieldName}->entity = $referencing_entity_2; $referencing_entity_2->{$this->fieldName}->entity = $referencing_entity_2;
$referencing_entity_2->save(); $referencing_entity_2->save();
......
...@@ -249,7 +249,11 @@ public function testEntityFormDisplayBuildForm() { ...@@ -249,7 +249,11 @@ public function testEntityFormDisplayBuildForm() {
$this->createFieldWithStorage('_2'); $this->createFieldWithStorage('_2');
$entity_type = 'entity_test'; $entity_type = 'entity_test';
$entity = entity_create($entity_type, ['id' => 1, 'revision_id' => 1, 'type' => $this->fieldTestData->field->getTargetBundle()]); $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->create([
'id' => 1,
'revision_id' => 1,
'type' => $this->fieldTestData->field->getTargetBundle(),
]);
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */ /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository'); $display_repository = \Drupal::service('entity_display.repository');
......
...@@ -34,7 +34,7 @@ protected function setUp() { ...@@ -34,7 +34,7 @@ protected function setUp() {
$this->createFieldWithStorage('', $this->entityType, $this->bundle); $this->createFieldWithStorage('', $this->entityType, $this->bundle);
// Create an 'entity_test' entity. // Create an 'entity_test' entity.
$this->entity = entity_create($this->entityType, [ $this->entity = \Drupal::entityTypeManager()->getStorage($this->entityType)->create([
'type' => $this->bundle, 'type' => $this->bundle,
]); ]);
} }
......
...@@ -175,10 +175,11 @@ public function testTranslatableFieldSaveLoad() { ...@@ -175,10 +175,11 @@ public function testTranslatableFieldSaveLoad() {
$langcode = $entity->language()->getId(); $langcode = $entity->language()->getId();
$this->assertEqual($entity->getTranslation($langcode)->{$field_name_default}->getValue(), $field->getDefaultValueLiteral(), format_string('Default value correctly populated for language %language.', ['%language' => $langcode])); $this->assertEqual($entity->getTranslation($langcode)->{$field_name_default}->getValue(), $field->getDefaultValueLiteral(), format_string('Default value correctly populated for language %language.', ['%language' => $langcode]));
$storage = \Drupal::entityTypeManager()->getStorage($entity_type_id);
// Check that explicit empty values are not overridden with default values. // Check that explicit empty values are not overridden with default values.
foreach ([NULL, []] as $empty_items) { foreach ([NULL, []] as $empty_items) {
$values = ['type' => $field->getTargetBundle(), 'langcode' => $translation_langcodes[0]]; $values = ['type' => $field->getTargetBundle(), 'langcode' => $translation_langcodes[0]];
$entity = entity_create($entity_type_id, $values); $entity = $storage->create($values);
foreach ($translation_langcodes as $langcode) { foreach ($translation_langcodes as $langcode) {
$values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality()); $values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality());
$translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode); $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
......
...@@ -123,7 +123,7 @@ public function normalize($entity, $format = NULL, array $context = []) { ...@@ -123,7 +123,7 @@ public function normalize($entity, $format = NULL, array $context = []) {
* @param array $data * @param array $data
* Entity data to restore. * Entity data to restore.
* @param string $class * @param string $class
* Unused, entity_create() is used to instantiate entity objects. * Unused parameter.
* @param string $format * @param string $format
* Format the given data was extracted from. * Format the given data was extracted from.
* @param array $context * @param array $context
......
...@@ -86,8 +86,6 @@ class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguag ...@@ -86,8 +86,6 @@ class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguag
* - target_bundle: The bundle. * - target_bundle: The bundle.
* Other array elements will be used to set the corresponding properties on * Other array elements will be used to set the corresponding properties on
* the class; see the class property documentation for details. * the class; see the class property documentation for details.
*
* @see entity_create()
*/ */
public function __construct(array $values, $entity_type = 'language_content_settings') { public function __construct(array $values, $entity_type = 'language_content_settings') {
if (empty($values['target_entity_type_id'])) { if (empty($values['target_entity_type_id'])) {
......
...@@ -40,10 +40,11 @@ public function getNodeByTitle($title, $reset = FALSE) { ...@@ -40,10 +40,11 @@ public function getNodeByTitle($title, $reset = FALSE) {
/** /**
* Creates a node based on default settings. * Creates a node based on default settings.
* *
* @param array $settings * @param array $values
* (optional) An associative array of settings for the node, as used in * (optional) An associative array of values for the node, as used in
* entity_create(). Override the defaults by specifying the key and value * creation of entity. Override the defaults by specifying the key and value
* in the array, for example: * in the array, for example:
*
* @code * @code
* $this->drupalCreateNode(array( * $this->drupalCreateNode(array(
* 'title' => t('Hello, world!'), * 'title' => t('Hello, world!'),
...@@ -65,9 +66,9 @@ public function getNodeByTitle($title, $reset = FALSE) { ...@@ -65,9 +66,9 @@ public function getNodeByTitle($title, $reset = FALSE) {
* @return \Drupal\node\NodeInterface * @return \Drupal\node\NodeInterface
* The created node entity. * The created node entity.
*/ */
protected function createNode(array $settings = []) { protected function createNode(array $values = []) {
// Populate defaults array. // Populate defaults array.
$settings += [ $values += [
'body' => [ 'body' => [
[ [
'value' => $this->randomMachineName(32), 'value' => $this->randomMachineName(32),
...@@ -78,22 +79,22 @@ protected function createNode(array $settings = []) { ...@@ -78,22 +79,22 @@ protected function createNode(array $settings = []) {
'type' => 'page', 'type' => 'page',
]; ];
if (!array_key_exists('uid', $settings)) { if (!array_key_exists('uid', $values)) {
$user = User::load(\Drupal::currentUser()->id()); $user = User::load(\Drupal::currentUser()->id());
if ($user) { if ($user) {
$settings['uid'] = $user->id(); $values['uid'] = $user->id();
} }
elseif (method_exists($this, 'setUpCurrentUser')) { elseif (method_exists($this, 'setUpCurrentUser')) {
/** @var \Drupal\user\UserInterface $user */ /** @var \Drupal\user\UserInterface $user */
$user = $this->setUpCurrentUser(); $user = $this->setUpCurrentUser();
$settings['uid'] = $user->id(); $values['uid'] = $user->id();
} }
else { else {
$settings['uid'] = 0; $values['uid'] = 0;
} }
} }
$node = Node::create($settings); $node = Node::create($values);
$node->save(); $node->save();
return $node; return $node;
......
...@@ -212,8 +212,8 @@ protected function createUser(array $permissions = [], $name = NULL, $admin = FA ...@@ -212,8 +212,8 @@ protected function createUser(array $permissions = [], $name = NULL, $admin = FA
* @param string $name * @param string $name
* (optional) The label for the role. Defaults to a random string. * (optional) The label for the role. Defaults to a random string.
* @param int $weight * @param int $weight
* (optional) The weight for the role. Defaults NULL so that entity_create() * (optional) The weight for the role. Defaults to NULL which sets the
* sets the weight to maximum + 1. * weight to maximum + 1.
* *
* @return string * @return string
* Role ID of newly created role, or FALSE if role creation failed. * Role ID of newly created role, or FALSE if role creation failed.
...@@ -239,8 +239,8 @@ protected function createAdminRole($rid = NULL, $name = NULL, $weight = NULL) { ...@@ -239,8 +239,8 @@ protected function createAdminRole($rid = NULL, $name = NULL, $weight = NULL) {
* @param string $name * @param string $name
* (optional) The label for the role. Defaults to a random string. * (optional) The label for the role. Defaults to a random string.
* @param int $weight * @param int $weight
* (optional) The weight for the role. Defaults NULL so that entity_create() * (optional) The weight for the role. Defaults to NULL which sets the
* sets the weight to maximum + 1. * weight to maximum + 1.
* *
* @return string * @return string
* Role ID of newly created role, or FALSE if role creation failed. * Role ID of newly created role, or FALSE if role creation failed.
......
...@@ -107,10 +107,10 @@ protected function assertCRUD($entity_type, UserInterface $user1) { ...@@ -107,10 +107,10 @@ protected function assertCRUD($entity_type, UserInterface $user1) {
// Test deleting a list of entities not indexed by entity id. // Test deleting a list of entities not indexed by entity id.
$entities = []; $entities = [];
$entity = entity_create($entity_type, ['name' => 'test', 'user_id' => $user1->id()]); $entity = $storage->create(['name' => 'test', 'user_id' => $user1->id()]);
$entity->save(); $entity->save();
$entities['test'] = $entity; $entities['test'] = $entity;
$entity = entity_create($entity_type, ['name' => 'test2', 'user_id' => $user1->id()]); $entity = $storage->create(['name' => 'test2', 'user_id' => $user1->id()]);
$entity->save(); $entity->save();
$entities['test2'] = $entity; $entities['test2'] = $entity;
$controller = \Drupal::entityManager()->getStorage($entity_type); $controller = \Drupal::entityManager()->getStorage($entity_type);
......
...@@ -121,4 +121,18 @@ public function testEntityView() { ...@@ -121,4 +121,18 @@ public function testEntityView() {
$this->assertEquals(4, count(entity_view_multiple($entities, 'default'))); $this->assertEquals(4, count(entity_view_multiple($entities, 'default')));
} }
/**
* Tests deprecation of the entity_create() function.
*
* @expectedDeprecation 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
*/
public function testEntityCreate() {
$values = ['name' => $this->getRandomGenerator()->sentences(2)];
$expected = EntityTest::create($values);
$actual = entity_create('entity_test', $values);
$this->assertEquals($expected->label(), $actual->label());
$this->assertEquals($expected->getEntityTypeId(), $actual->getEntityTypeId());
$this->assertInstanceOf(EntityTest::class, $actual);
}
} }
...@@ -108,7 +108,9 @@ public function testEntityReferenceFieldValidation() { ...@@ -108,7 +108,9 @@ public function testEntityReferenceFieldValidation() {
// Test a non-referenceable bundle. // Test a non-referenceable bundle.
entity_test_create_bundle('non_referenceable', NULL, $this->referencedEntityType); entity_test_create_bundle('non_referenceable', NULL, $this->referencedEntityType);
$referenced_entity = entity_create($this->referencedEntityType, ['type' => 'non_referenceable']); $referenced_entity = $this->entityTypeManager
->getStorage($this->referencedEntityType)
->create(['type' => 'non_referenceable']);
$referenced_entity->save(); $referenced_entity->save();
$entity->{$this->fieldName}->target_id = $referenced_entity->id(); $entity->{$this->fieldName}->target_id = $referenced_entity->id();
$violations = $entity->{$this->fieldName}->validate(); $violations = $entity->{$this->fieldName}->validate();
......
...@@ -171,7 +171,7 @@ public function testSetNewRevision() { ...@@ -171,7 +171,7 @@ public function testSetNewRevision() {
$this->installEntitySchema($entity_type); $this->installEntitySchema($entity_type);
$storage = \Drupal::entityTypeManager()->getStorage($entity_type); $storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$entity = entity_create($entity_type, [ $entity = $storage->create([
'name' => 'foo', 'name' => 'foo',
'user_id' => $user->id(), 'user_id' => $user->id(),
]); ]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment