From 9b1690c21396da6052d2d2f5d5f7299657966f6d Mon Sep 17 00:00:00 2001 From: Lee Rowlands Date: Wed, 1 May 2019 09:41:13 +1000 Subject: [PATCH] Issue #2928930 by andypost, mikelutz, anya_m, Berdir: Properly deprecate entity_create() and remove all occurrences --- core/includes/entity.inc | 2 ++ .../Core/Field/Entity/BaseFieldOverride.php | 2 -- .../src/Tests/Views/BlockContentTestBase.php | 11 +++++----- .../Functional/Views/BlockContentTestBase.php | 11 +++++----- .../ContentTranslationSyncImageTest.php | 4 +++- core/modules/field/src/Entity/FieldConfig.php | 2 -- .../field/src/Entity/FieldStorageConfig.php | 2 -- ...erenceFieldTranslatedReferenceViewTest.php | 6 +++--- .../Functional/reEnableModuleFieldTest.php | 2 +- .../EntityReferenceFormatterTest.php | 5 +++-- .../tests/src/Kernel/FieldAttachOtherTest.php | 6 +++++- .../tests/src/Kernel/FieldValidationTest.php | 2 +- .../tests/src/Kernel/TranslationTest.php | 3 ++- .../Normalizer/ContentEntityNormalizer.php | 2 +- .../src/Entity/ContentLanguageSettings.php | 2 -- .../tests/src/Traits/NodeCreationTrait.php | 21 ++++++++++--------- .../tests/src/Traits/UserCreationTrait.php | 8 +++---- .../KernelTests/Core/Entity/EntityApiTest.php | 4 ++-- .../Core/Entity/EntityLegacyTest.php | 14 +++++++++++++ .../Core/Entity/EntityReferenceFieldTest.php | 4 +++- .../Entity/EntityRevisionTranslationTest.php | 2 +- 21 files changed, 66 insertions(+), 49 deletions(-) diff --git a/core/includes/entity.inc b/core/includes/entity.inc index 22c2ad475440..32964e667658 100644 --- a/core/includes/entity.inc +++ b/core/includes/entity.inc @@ -271,10 +271,12 @@ function entity_delete_multiple($entity_type, array $ids) { * \Drupal::entityTypeManager()->getStorage($entity_type)->create($values); * @endcode * + * @see https://www.drupal.org/node/2266845 * @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage() * @see \Drupal\Core\Entity\EntityStorageInterface::create() */ 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); diff --git a/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php b/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php index 4ad9fd529e20..7ffb8ba57b88 100644 --- a/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php +++ b/core/lib/Drupal/Core/Field/Entity/BaseFieldOverride.php @@ -87,8 +87,6 @@ class BaseFieldOverride extends FieldConfigBase { * (optional) The type of the entity to create. Defaults to * 'base_field_override'. * - * @see entity_create() - * * @throws \Drupal\Core\Field\FieldException * Exception thrown if $values does not contain a field_name, entity_type or * bundle value. diff --git a/core/modules/block_content/src/Tests/Views/BlockContentTestBase.php b/core/modules/block_content/src/Tests/Views/BlockContentTestBase.php index 0e3fbfeeb009..ceb71d72fa21 100644 --- a/core/modules/block_content/src/Tests/Views/BlockContentTestBase.php +++ b/core/modules/block_content/src/Tests/Views/BlockContentTestBase.php @@ -56,21 +56,20 @@ abstract class BlockContentTestBase extends ViewTestBase { /** * Creates a custom block. * - * @param array $settings - * (optional) An associative array of settings for the block_content, as - * used in entity_create(). + * @param array $values + * (optional) The values for the block_content entity. * * @return \Drupal\block_content\Entity\BlockContent * Created custom block. */ - protected function createBlockContent(array $settings = []) { + protected function createBlockContent(array $values = []) { $status = 0; - $settings += [ + $values += [ 'info' => $this->randomMachineName(), 'type' => 'basic', 'langcode' => 'en', ]; - if ($block_content = BlockContent::create($settings)) { + if ($block_content = BlockContent::create($values)) { $status = $block_content->save(); } $this->assertEqual($status, SAVED_NEW, new FormattableMarkup('Created block content %info.', ['%info' => $block_content->label()])); diff --git a/core/modules/block_content/tests/src/Functional/Views/BlockContentTestBase.php b/core/modules/block_content/tests/src/Functional/Views/BlockContentTestBase.php index dd14a4266cb3..254fc408f87a 100644 --- a/core/modules/block_content/tests/src/Functional/Views/BlockContentTestBase.php +++ b/core/modules/block_content/tests/src/Functional/Views/BlockContentTestBase.php @@ -51,21 +51,20 @@ abstract class BlockContentTestBase extends ViewTestBase { /** * Creates a custom block. * - * @param array $settings - * (optional) An associative array of settings for the block_content, as - * used in entity_create(). + * @param array $values + * (optional) The values for the block_content entity. * * @return \Drupal\block_content\Entity\BlockContent * Created custom block. */ - protected function createBlockContent(array $settings = []) { + protected function createBlockContent(array $values = []) { $status = 0; - $settings += [ + $values += [ 'info' => $this->randomMachineName(), 'type' => 'basic', 'langcode' => 'en', ]; - if ($block_content = BlockContent::create($settings)) { + if ($block_content = BlockContent::create($values)) { $status = $block_content->save(); } $this->assertEqual($status, SAVED_NEW, new FormattableMarkup('Created block content %info.', ['%info' => $block_content->label()])); diff --git a/core/modules/content_translation/tests/src/Functional/ContentTranslationSyncImageTest.php b/core/modules/content_translation/tests/src/Functional/ContentTranslationSyncImageTest.php index 99d93331cace..e091f7fb2664 100644 --- a/core/modules/content_translation/tests/src/Functional/ContentTranslationSyncImageTest.php +++ b/core/modules/content_translation/tests/src/Functional/ContentTranslationSyncImageTest.php @@ -125,7 +125,9 @@ class ContentTranslationSyncImageTest extends ContentTranslationTestBase { 'user_id' => mt_rand(1, 128), '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. $values = []; diff --git a/core/modules/field/src/Entity/FieldConfig.php b/core/modules/field/src/Entity/FieldConfig.php index 8218db1ed3bf..411943b11c2d 100644 --- a/core/modules/field/src/Entity/FieldConfig.php +++ b/core/modules/field/src/Entity/FieldConfig.php @@ -90,8 +90,6 @@ class FieldConfig extends FieldConfigBase implements FieldConfigInterface { * 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 * property documentation for details. - * - * @see entity_create() */ public function __construct(array $values, $entity_type = 'field_config') { // Allow either an injected FieldStorageConfig object, or a field_name and diff --git a/core/modules/field/src/Entity/FieldStorageConfig.php b/core/modules/field/src/Entity/FieldStorageConfig.php index f6d54824ad27..3277c5416977 100644 --- a/core/modules/field/src/Entity/FieldStorageConfig.php +++ b/core/modules/field/src/Entity/FieldStorageConfig.php @@ -243,8 +243,6 @@ class FieldStorageConfig extends ConfigEntityBase implements FieldStorageConfigI * a 'field_name' property can be accepted in place of 'id'. * - entity_type: required. * - type: required. - * - * @see entity_create() */ public function __construct(array $values, $entity_type = 'field_storage_config') { // Check required properties. diff --git a/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldTranslatedReferenceViewTest.php b/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldTranslatedReferenceViewTest.php index 6771270961fd..f327d4cd42ea 100644 --- a/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldTranslatedReferenceViewTest.php +++ b/core/modules/field/tests/src/Functional/EntityReference/EntityReferenceFieldTranslatedReferenceViewTest.php @@ -286,7 +286,7 @@ class EntityReferenceFieldTranslatedReferenceViewTest extends BrowserTestBase { */ protected function createReferencedEntityWithTranslation() { /** @var \Drupal\node\Entity\Node $node */ - $node = entity_create($this->testEntityTypeName, [ + $node = \Drupal::entityTypeManager()->getStorage($this->testEntityTypeName)->create([ 'title' => $this->originalLabel, 'type' => $this->referencedType->id(), 'description' => [ @@ -309,7 +309,7 @@ class EntityReferenceFieldTranslatedReferenceViewTest extends BrowserTestBase { */ protected function createNotTranslatedReferencedEntity() { /** @var \Drupal\node\Entity\Node $node */ - $node = entity_create($this->testEntityTypeName, [ + $node = \Drupal::entityTypeManager()->getStorage($this->testEntityTypeName)->create([ 'title' => $this->labelOfNotTranslatedReference, 'type' => $this->referencedType->id(), 'description' => [ @@ -328,7 +328,7 @@ class EntityReferenceFieldTranslatedReferenceViewTest extends BrowserTestBase { */ protected function createReferrerEntity($translatable = TRUE) { /** @var \Drupal\node\Entity\Node $node */ - $node = entity_create($this->testEntityTypeName, [ + $node = \Drupal::entityTypeManager()->getStorage($this->testEntityTypeName)->create([ 'title' => $this->randomMachineName(), 'type' => $this->referrerType->id(), 'description' => [ diff --git a/core/modules/field/tests/src/Functional/reEnableModuleFieldTest.php b/core/modules/field/tests/src/Functional/reEnableModuleFieldTest.php index 046bbb566e31..39fb2dc9b376 100644 --- a/core/modules/field/tests/src/Functional/reEnableModuleFieldTest.php +++ b/core/modules/field/tests/src/Functional/reEnableModuleFieldTest.php @@ -99,7 +99,7 @@ class reEnableModuleFieldTest extends BrowserTestBase { // Add another telephone field to a different entity type in order to test // the message for the case when multiple fields are blocking the // uninstallation of a module. - $field_storage2 = entity_create('field_storage_config', [ + $field_storage2 = FieldStorageConfig::create([ 'field_name' => 'field_telephone_2', 'entity_type' => 'user', 'type' => 'telephone', diff --git a/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php b/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php index 75d487ce6adf..303ac6af5ac6 100644 --- a/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php +++ b/core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php @@ -250,7 +250,8 @@ class EntityReferenceFormatterTest extends EntityKernelTestBase { ]) ->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(); // Create a self-reference. @@ -273,7 +274,7 @@ class EntityReferenceFormatterTest extends EntityKernelTestBase { // Repeat the process with another entity in order to check that the // '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->{$this->fieldName}->entity = $referencing_entity_2; $referencing_entity_2->save(); diff --git a/core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php b/core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php index 5824f5169bce..c3b1f9305454 100644 --- a/core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php +++ b/core/modules/field/tests/src/Kernel/FieldAttachOtherTest.php @@ -249,7 +249,11 @@ class FieldAttachOtherTest extends FieldKernelTestBase { $this->createFieldWithStorage('_2'); $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 */ $display_repository = \Drupal::service('entity_display.repository'); diff --git a/core/modules/field/tests/src/Kernel/FieldValidationTest.php b/core/modules/field/tests/src/Kernel/FieldValidationTest.php index 92d33972f65d..8ab818b2998a 100644 --- a/core/modules/field/tests/src/Kernel/FieldValidationTest.php +++ b/core/modules/field/tests/src/Kernel/FieldValidationTest.php @@ -34,7 +34,7 @@ class FieldValidationTest extends FieldKernelTestBase { $this->createFieldWithStorage('', $this->entityType, $this->bundle); // Create an 'entity_test' entity. - $this->entity = entity_create($this->entityType, [ + $this->entity = \Drupal::entityTypeManager()->getStorage($this->entityType)->create([ 'type' => $this->bundle, ]); } diff --git a/core/modules/field/tests/src/Kernel/TranslationTest.php b/core/modules/field/tests/src/Kernel/TranslationTest.php index 1a101b79ee38..31ee4b41fdef 100644 --- a/core/modules/field/tests/src/Kernel/TranslationTest.php +++ b/core/modules/field/tests/src/Kernel/TranslationTest.php @@ -175,10 +175,11 @@ class TranslationTest extends FieldKernelTestBase { $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])); + $storage = \Drupal::entityTypeManager()->getStorage($entity_type_id); // Check that explicit empty values are not overridden with default values. foreach ([NULL, []] as $empty_items) { $values = ['type' => $field->getTargetBundle(), 'langcode' => $translation_langcodes[0]]; - $entity = entity_create($entity_type_id, $values); + $entity = $storage->create($values); foreach ($translation_langcodes as $langcode) { $values[$this->fieldName][$langcode] = $this->_generateTestFieldValues($this->fieldStorage->getCardinality()); $translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode); diff --git a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php index 39daf1cf1cbf..aec86c91246d 100644 --- a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php @@ -123,7 +123,7 @@ class ContentEntityNormalizer extends NormalizerBase { * @param array $data * Entity data to restore. * @param string $class - * Unused, entity_create() is used to instantiate entity objects. + * Unused parameter. * @param string $format * Format the given data was extracted from. * @param array $context diff --git a/core/modules/language/src/Entity/ContentLanguageSettings.php b/core/modules/language/src/Entity/ContentLanguageSettings.php index d89b1508f94c..59803defe1a9 100644 --- a/core/modules/language/src/Entity/ContentLanguageSettings.php +++ b/core/modules/language/src/Entity/ContentLanguageSettings.php @@ -86,8 +86,6 @@ class ContentLanguageSettings extends ConfigEntityBase implements ContentLanguag * - target_bundle: The bundle. * Other array elements will be used to set the corresponding properties on * the class; see the class property documentation for details. - * - * @see entity_create() */ public function __construct(array $values, $entity_type = 'language_content_settings') { if (empty($values['target_entity_type_id'])) { diff --git a/core/modules/node/tests/src/Traits/NodeCreationTrait.php b/core/modules/node/tests/src/Traits/NodeCreationTrait.php index 714ae2fe6e54..2039988b114b 100644 --- a/core/modules/node/tests/src/Traits/NodeCreationTrait.php +++ b/core/modules/node/tests/src/Traits/NodeCreationTrait.php @@ -40,10 +40,11 @@ trait NodeCreationTrait { /** * Creates a node based on default settings. * - * @param array $settings - * (optional) An associative array of settings for the node, as used in - * entity_create(). Override the defaults by specifying the key and value + * @param array $values + * (optional) An associative array of values for the node, as used in + * creation of entity. Override the defaults by specifying the key and value * in the array, for example: + * * @code * $this->drupalCreateNode(array( * 'title' => t('Hello, world!'), @@ -65,9 +66,9 @@ trait NodeCreationTrait { * @return \Drupal\node\NodeInterface * The created node entity. */ - protected function createNode(array $settings = []) { + protected function createNode(array $values = []) { // Populate defaults array. - $settings += [ + $values += [ 'body' => [ [ 'value' => $this->randomMachineName(32), @@ -78,22 +79,22 @@ trait NodeCreationTrait { 'type' => 'page', ]; - if (!array_key_exists('uid', $settings)) { + if (!array_key_exists('uid', $values)) { $user = User::load(\Drupal::currentUser()->id()); if ($user) { - $settings['uid'] = $user->id(); + $values['uid'] = $user->id(); } elseif (method_exists($this, 'setUpCurrentUser')) { /** @var \Drupal\user\UserInterface $user */ $user = $this->setUpCurrentUser(); - $settings['uid'] = $user->id(); + $values['uid'] = $user->id(); } else { - $settings['uid'] = 0; + $values['uid'] = 0; } } - $node = Node::create($settings); + $node = Node::create($values); $node->save(); return $node; diff --git a/core/modules/user/tests/src/Traits/UserCreationTrait.php b/core/modules/user/tests/src/Traits/UserCreationTrait.php index 11c4d9b0bc53..956bd3d78262 100644 --- a/core/modules/user/tests/src/Traits/UserCreationTrait.php +++ b/core/modules/user/tests/src/Traits/UserCreationTrait.php @@ -212,8 +212,8 @@ trait UserCreationTrait { * @param string $name * (optional) The label for the role. Defaults to a random string. * @param int $weight - * (optional) The weight for the role. Defaults NULL so that entity_create() - * sets the weight to maximum + 1. + * (optional) The weight for the role. Defaults to NULL which sets the + * weight to maximum + 1. * * @return string * Role ID of newly created role, or FALSE if role creation failed. @@ -239,8 +239,8 @@ trait UserCreationTrait { * @param string $name * (optional) The label for the role. Defaults to a random string. * @param int $weight - * (optional) The weight for the role. Defaults NULL so that entity_create() - * sets the weight to maximum + 1. + * (optional) The weight for the role. Defaults to NULL which sets the + * weight to maximum + 1. * * @return string * Role ID of newly created role, or FALSE if role creation failed. diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php index 23d1734b74f1..2e20fe44acf6 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php @@ -107,10 +107,10 @@ class EntityApiTest extends EntityKernelTestBase { // Test deleting a list of entities not indexed by entity id. $entities = []; - $entity = entity_create($entity_type, ['name' => 'test', 'user_id' => $user1->id()]); + $entity = $storage->create(['name' => 'test', 'user_id' => $user1->id()]); $entity->save(); $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(); $entities['test2'] = $entity; $controller = \Drupal::entityManager()->getStorage($entity_type); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php index cf4ea64b958a..fa067915a481 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityLegacyTest.php @@ -121,4 +121,18 @@ class EntityLegacyTest extends KernelTestBase { $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); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php index 5c57a6531d0f..472b83fc48c8 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityReferenceFieldTest.php @@ -108,7 +108,9 @@ class EntityReferenceFieldTest extends EntityKernelTestBase { // Test a non-referenceable bundle. 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(); $entity->{$this->fieldName}->target_id = $referenced_entity->id(); $violations = $entity->{$this->fieldName}->validate(); diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php index 1d3f1b420d85..2fb815a06e1f 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityRevisionTranslationTest.php @@ -171,7 +171,7 @@ class EntityRevisionTranslationTest extends EntityKernelTestBase { $this->installEntitySchema($entity_type); $storage = \Drupal::entityTypeManager()->getStorage($entity_type); - $entity = entity_create($entity_type, [ + $entity = $storage->create([ 'name' => 'foo', 'user_id' => $user->id(), ]);