Issue #2928930 by andypost, mikelutz, anya_m, Berdir: Properly deprecate entity_create() and remove all occurrences
parent
bfe4794d88
commit
9b1690c213
|
@ -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);
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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()]));
|
||||
|
|
|
@ -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()]));
|
||||
|
|
|
@ -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 = [];
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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' => [
|
||||
|
|
|
@ -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',
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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,
|
||||
]);
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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'])) {
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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(),
|
||||
]);
|
||||
|
|
Loading…
Reference in New Issue