Issue #3027178 by Berdir, alexpott, miroslav-lee, andypost, larowlan, mikelutz, JeroenT: Properly deprecate entity_load_multiple_by_properties()

merge-requests/1119/head
Alex Pott 2019-04-30 08:41:05 +01:00
parent fb532b06f8
commit bfe4794d88
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
24 changed files with 87 additions and 76 deletions

View File

@ -184,19 +184,13 @@ function entity_load_multiple($entity_type, array $ids = NULL, $reset = FALSE) {
* An array of entity objects indexed by their IDs. Returns an empty array if * An array of entity objects indexed by their IDs. Returns an empty array if
* no matching entities are found. * no matching entities are found.
* *
* @deprecated as of Drupal 8.0.x, will be removed before Drupal 9.0.0. Use * @deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the
* the entity storage's loadByProperties() method to load an entity by their * entity type storage's loadByProperties() method.
* property values:
* @code
* \Drupal::entityTypeManager()
* ->getStorage($entity_type)
* ->loadByProperties($values);
* @endcode
* *
* @see \Drupal\Core\Entity\EntityTypeManagerInterface::getStorage() * @see https://www.drupal.org/node/3050910
* @see \Drupal\Core\Entity\EntityStorageInterface::loadByProperties()
*/ */
function entity_load_multiple_by_properties($entity_type, array $values) { function entity_load_multiple_by_properties($entity_type, array $values) {
@trigger_error('entity_load_multiple_by_properties() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage\'s loadByProperties() method. See https://www.drupal.org/node/3050910', E_USER_DEPRECATED);
return \Drupal::entityManager() return \Drupal::entityManager()
->getStorage($entity_type) ->getStorage($entity_type)
->loadByProperties($values); ->loadByProperties($values);

View File

@ -208,7 +208,7 @@ class DefaultProcessor extends AggregatorPluginSettingsBase implements Processor
} }
// Try to load an existing entry. // Try to load an existing entry.
if ($entry = entity_load_multiple_by_properties('aggregator_item', $values)) { if ($entry = $this->itemStorage->loadByProperties($values)) {
$entry = reset($entry); $entry = reset($entry);
} }
else { else {

View File

@ -77,7 +77,7 @@ class FeedLanguageTest extends AggregatorTestBase {
// the one from the feed. // the one from the feed.
foreach ($feeds as $feed) { foreach ($feeds as $feed) {
/** @var \Drupal\aggregator\ItemInterface[] $items */ /** @var \Drupal\aggregator\ItemInterface[] $items */
$items = entity_load_multiple_by_properties('aggregator_item', ['fid' => $feed->id()]); $items = \Drupal::entityTypeManager()->getStorage('aggregator_item')->loadByProperties(['fid' => $feed->id()]);
$this->assertTrue(count($items) > 0, 'Feed items were created.'); $this->assertTrue(count($items) > 0, 'Feed items were created.');
foreach ($items as $item) { foreach ($items as $item) {
$this->assertEqual($item->language()->getId(), $feed->language()->getId()); $this->assertEqual($item->language()->getId(), $feed->language()->getId());

View File

@ -45,7 +45,7 @@ class FeedProcessorPluginTest extends AggregatorTestBase {
$description = $feed->description->value ?: ''; $description = $feed->description->value ?: '';
$this->updateAndDelete($feed, NULL); $this->updateAndDelete($feed, NULL);
// Make sure the feed title is changed. // Make sure the feed title is changed.
$entities = entity_load_multiple_by_properties('aggregator_feed', ['description' => $description]); $entities = \Drupal::entityTypeManager()->getStorage('aggregator_feed')->loadByProperties(['description' => $description]);
$this->assertTrue(empty($entities)); $this->assertTrue(empty($entities));
} }

View File

@ -16,10 +16,9 @@ use Drupal\field\Entity\FieldStorageConfig;
*/ */
function comment_uninstall() { function comment_uninstall() {
// Remove the comment fields. // Remove the comment fields.
$fields = entity_load_multiple_by_properties('field_storage_config', ['type' => 'comment']); $storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
foreach ($fields as $field) { $fields = $storage->loadByProperties(['type' => 'comment']);
$field->delete(); $storage->delete($fields);
}
// Remove state setting. // Remove state setting.
\Drupal::state()->delete('comment.node_comment_statistics_scale'); \Drupal::state()->delete('comment.node_comment_statistics_scale');

View File

@ -531,7 +531,7 @@ function comment_node_search_result(EntityInterface $node) {
function comment_user_cancel($edit, UserInterface $account, $method) { function comment_user_cancel($edit, UserInterface $account, $method) {
switch ($method) { switch ($method) {
case 'user_cancel_block_unpublish': case 'user_cancel_block_unpublish':
$comments = entity_load_multiple_by_properties('comment', ['uid' => $account->id()]); $comments = \Drupal::entityTypeManager()->getStorage('comment')->loadByProperties(['uid' => $account->id()]);
foreach ($comments as $comment) { foreach ($comments as $comment) {
$comment->setUnpublished(); $comment->setUnpublished();
$comment->save(); $comment->save();
@ -540,7 +540,7 @@ function comment_user_cancel($edit, UserInterface $account, $method) {
case 'user_cancel_reassign': case 'user_cancel_reassign':
/** @var \Drupal\comment\CommentInterface[] $comments */ /** @var \Drupal\comment\CommentInterface[] $comments */
$comments = entity_load_multiple_by_properties('comment', ['uid' => $account->id()]); $comments = \Drupal::entityTypeManager()->getStorage('comment')->loadByProperties(['uid' => $account->id()]);
foreach ($comments as $comment) { foreach ($comments as $comment) {
$comment->setOwnerId(0); $comment->setOwnerId(0);
$comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous')); $comment->setAuthorName(\Drupal::config('user.settings')->get('anonymous'));

View File

@ -177,7 +177,7 @@ class BulkDeleteTest extends FieldKernelTestBase {
$field->delete(); $field->delete();
// The field still exists, deleted. // The field still exists, deleted.
$fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
$this->assertEqual(count($fields), 1, 'There is one deleted field'); $this->assertEqual(count($fields), 1, 'There is one deleted field');
$field = $fields[$field->uuid()]; $field = $fields[$field->uuid()];
$this->assertEqual($field->getTargetBundle(), $bundle, 'The deleted field is for the correct bundle'); $this->assertEqual($field->getTargetBundle(), $bundle, 'The deleted field is for the correct bundle');
@ -248,7 +248,7 @@ class BulkDeleteTest extends FieldKernelTestBase {
$deleted_field_uuid = $deleted_field->uuid(); $deleted_field_uuid = $deleted_field->uuid();
// Reload the field storage. // Reload the field storage.
$field_storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $deleted_field_storage->uuid(), 'include_deleted' => TRUE]); $field_storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(['uuid' => $deleted_field_storage->uuid(), 'include_deleted' => TRUE]);
$deleted_field_storage = reset($field_storages); $deleted_field_storage = reset($field_storages);
// Create the field again. // Create the field again.
@ -265,7 +265,7 @@ class BulkDeleteTest extends FieldKernelTestBase {
])->save(); ])->save();
// The field still exists, deleted, with the same field name. // The field still exists, deleted, with the same field name.
$fields = entity_load_multiple_by_properties('field_config', ['uuid' => $deleted_field_uuid, 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['uuid' => $deleted_field_uuid, 'include_deleted' => TRUE]);
$this->assertTrue(isset($fields[$deleted_field_uuid]) && $fields[$deleted_field_uuid]->isDeleted(), 'The field exists and is deleted'); $this->assertTrue(isset($fields[$deleted_field_uuid]) && $fields[$deleted_field_uuid]->isDeleted(), 'The field exists and is deleted');
$this->assertTrue(isset($fields[$deleted_field_uuid]) && $fields[$deleted_field_uuid]->getName() == $field_name); $this->assertTrue(isset($fields[$deleted_field_uuid]) && $fields[$deleted_field_uuid]->getName() == $field_name);
@ -292,7 +292,7 @@ class BulkDeleteTest extends FieldKernelTestBase {
$this->assertFalse(\Drupal::database()->schema()->tableExists($deleted_table_name)); $this->assertFalse(\Drupal::database()->schema()->tableExists($deleted_table_name));
// The field has been removed from the system. // The field has been removed from the system.
$fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $deleted_field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['field_storage_uuid' => $deleted_field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
$this->assertEqual(count($fields), 0, 'The field is gone'); $this->assertEqual(count($fields), 0, 'The field is gone');
// Verify there are still 10 entries in the main table. // Verify there are still 10 entries in the main table.
@ -351,19 +351,19 @@ class BulkDeleteTest extends FieldKernelTestBase {
$this->checkHooksInvocations($hooks, $actual_hooks); $this->checkHooksInvocations($hooks, $actual_hooks);
// The field still exists, deleted. // The field still exists, deleted.
$fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
$this->assertEqual(count($fields), 1, 'There is one deleted field'); $this->assertEqual(count($fields), 1, 'There is one deleted field');
// Purge the field. // Purge the field.
field_purge_batch($batch_size); field_purge_batch($batch_size);
// The field is gone. // The field is gone.
$fields = entity_load_multiple_by_properties('field_config', ['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['field_storage_uuid' => $field_storage->uuid(), 'deleted' => TRUE, 'include_deleted' => TRUE]);
$this->assertEqual(count($fields), 0, 'The field is gone'); $this->assertEqual(count($fields), 0, 'The field is gone');
// The field storage still exists, not deleted, because it has a second // The field storage still exists, not deleted, because it has a second
// field. // field.
$storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]); $storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
$this->assertTrue(isset($storages[$field_storage->uuid()]), 'The field storage exists and is not deleted'); $this->assertTrue(isset($storages[$field_storage->uuid()]), 'The field storage exists and is not deleted');
} }
@ -402,17 +402,17 @@ class BulkDeleteTest extends FieldKernelTestBase {
$this->checkHooksInvocations($hooks, $actual_hooks); $this->checkHooksInvocations($hooks, $actual_hooks);
// The field still exists, deleted. // The field still exists, deleted.
$fields = entity_load_multiple_by_properties('field_config', ['uuid' => $field->uuid(), 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
$this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted'); $this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted');
// Purge again to purge the field. // Purge again to purge the field.
field_purge_batch(0); field_purge_batch(0);
// The field is gone. // The field is gone.
$fields = entity_load_multiple_by_properties('field_config', ['uuid' => $field->uuid(), 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
$this->assertEqual(count($fields), 0, 'The field is purged.'); $this->assertEqual(count($fields), 0, 'The field is purged.');
// The field storage still exists, not deleted. // The field storage still exists, not deleted.
$storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]); $storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
$this->assertTrue(isset($storages[$field_storage->uuid()]) && !$storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is not deleted'); $this->assertTrue(isset($storages[$field_storage->uuid()]) && !$storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is not deleted');
// Delete the second field. // Delete the second field.
@ -437,18 +437,18 @@ class BulkDeleteTest extends FieldKernelTestBase {
$this->checkHooksInvocations($hooks, $actual_hooks); $this->checkHooksInvocations($hooks, $actual_hooks);
// The field and the storage still exist, deleted. // The field and the storage still exist, deleted.
$fields = entity_load_multiple_by_properties('field_config', ['uuid' => $field->uuid(), 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
$this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted'); $this->assertTrue(isset($fields[$field->uuid()]) && $fields[$field->uuid()]->isDeleted(), 'The field exists and is deleted');
$storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]); $storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
$this->assertTrue(isset($storages[$field_storage->uuid()]) && $storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is deleted'); $this->assertTrue(isset($storages[$field_storage->uuid()]) && $storages[$field_storage->uuid()]->isDeleted(), 'The field storage exists and is deleted');
// Purge again to purge the field and the storage. // Purge again to purge the field and the storage.
field_purge_batch(0); field_purge_batch(0);
// The field and the storage are gone. // The field and the storage are gone.
$fields = entity_load_multiple_by_properties('field_config', ['uuid' => $field->uuid(), 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['uuid' => $field->uuid(), 'include_deleted' => TRUE]);
$this->assertEqual(count($fields), 0, 'The field is purged.'); $this->assertEqual(count($fields), 0, 'The field is purged.');
$storages = entity_load_multiple_by_properties('field_storage_config', ['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]); $storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(['uuid' => $field_storage->uuid(), 'include_deleted' => TRUE]);
$this->assertEqual(count($storages), 0, 'The field storage is purged.'); $this->assertEqual(count($storages), 0, 'The field storage is purged.');
} }

View File

@ -283,13 +283,13 @@ class FieldCrudTest extends FieldKernelTestBase {
FieldConfig::create($another_field_definition)->save(); FieldConfig::create($another_field_definition)->save();
// Test that the first field is not deleted, and then delete it. // Test that the first field is not deleted, and then delete it.
$field = current(entity_load_multiple_by_properties('field_config', ['entity_type' => 'entity_test', 'field_name' => $this->fieldDefinition['field_name'], 'bundle' => $this->fieldDefinition['bundle'], 'include_deleted' => TRUE])); $field = current(\Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['entity_type' => 'entity_test', 'field_name' => $this->fieldDefinition['field_name'], 'bundle' => $this->fieldDefinition['bundle'], 'include_deleted' => TRUE]));
$this->assertTrue(!empty($field) && empty($field->deleted), 'A new field is not marked for deletion.'); $this->assertTrue(!empty($field) && empty($field->deleted), 'A new field is not marked for deletion.');
$field->delete(); $field->delete();
// Make sure the field was deleted without being marked for purging as there // Make sure the field was deleted without being marked for purging as there
// was no data. // was no data.
$fields = entity_load_multiple_by_properties('field_config', ['entity_type' => 'entity_test', 'field_name' => $this->fieldDefinition['field_name'], 'bundle' => $this->fieldDefinition['bundle'], 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['entity_type' => 'entity_test', 'field_name' => $this->fieldDefinition['field_name'], 'bundle' => $this->fieldDefinition['bundle'], 'include_deleted' => TRUE]);
$this->assertEquals(0, count($fields), 'A deleted field is marked for deletion.'); $this->assertEquals(0, count($fields), 'A deleted field is marked for deletion.');
// Try to load the field normally and make sure it does not show up. // Try to load the field normally and make sure it does not show up.

View File

@ -213,13 +213,14 @@ class FieldStorageCrudTest extends FieldKernelTestBase {
$id = $field_storage->id(); $id = $field_storage->id();
// Check that 'single column' criteria works. // Check that 'single column' criteria works.
$fields = entity_load_multiple_by_properties('field_storage_config', ['field_name' => $field_storage_definition['field_name']]); $field_storage_config_storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
$fields = $field_storage_config_storage->loadByProperties(['field_name' => $field_storage_definition['field_name']]);
$this->assertTrue(count($fields) == 1 && isset($fields[$id]), 'The field was properly read.'); $this->assertTrue(count($fields) == 1 && isset($fields[$id]), 'The field was properly read.');
// Check that 'multi column' criteria works. // Check that 'multi column' criteria works.
$fields = entity_load_multiple_by_properties('field_storage_config', ['field_name' => $field_storage_definition['field_name'], 'type' => $field_storage_definition['type']]); $fields = $field_storage_config_storage->loadByProperties(['field_name' => $field_storage_definition['field_name'], 'type' => $field_storage_definition['type']]);
$this->assertTrue(count($fields) == 1 && isset($fields[$id]), 'The field was properly read.'); $this->assertTrue(count($fields) == 1 && isset($fields[$id]), 'The field was properly read.');
$fields = entity_load_multiple_by_properties('field_storage_config', ['field_name' => $field_storage_definition['field_name'], 'type' => 'foo']); $fields = $field_storage_config_storage->loadByProperties(['field_name' => $field_storage_definition['field_name'], 'type' => 'foo']);
$this->assertTrue(empty($fields), 'No field was found.'); $this->assertTrue(empty($fields), 'No field was found.');
// Create a field from the field storage. // Create a field from the field storage.
@ -314,17 +315,18 @@ class FieldStorageCrudTest extends FieldKernelTestBase {
FieldConfig::create($another_field_definition)->save(); FieldConfig::create($another_field_definition)->save();
// Test that the first field is not deleted, and then delete it. // Test that the first field is not deleted, and then delete it.
$field_storage = current(entity_load_multiple_by_properties('field_storage_config', ['field_name' => $field_storage_definition['field_name'], 'include_deleted' => TRUE])); $field_storage_config_storage = \Drupal::entityTypeManager()->getStorage('field_storage_config');
$field_storage = current($field_storage_config_storage->loadByProperties(['field_name' => $field_storage_definition['field_name'], 'include_deleted' => TRUE]));
$this->assertTrue(!empty($field_storage) && !$field_storage->isDeleted(), 'A new storage is not marked for deletion.'); $this->assertTrue(!empty($field_storage) && !$field_storage->isDeleted(), 'A new storage is not marked for deletion.');
FieldStorageConfig::loadByName('entity_test', $field_storage_definition['field_name'])->delete(); FieldStorageConfig::loadByName('entity_test', $field_storage_definition['field_name'])->delete();
// Make sure that the field storage is deleted as it had no data. // Make sure that the field storage is deleted as it had no data.
$field_storages = entity_load_multiple_by_properties('field_storage_config', ['field_name' => $field_storage_definition['field_name'], 'include_deleted' => TRUE]); $field_storages = $field_storage_config_storage->loadByProperties(['field_name' => $field_storage_definition['field_name'], 'include_deleted' => TRUE]);
$this->assertEquals(0, count($field_storages), 'Field storage was deleted'); $this->assertEquals(0, count($field_storages), 'Field storage was deleted');
// Make sure that this field is marked as deleted when it is // Make sure that this field is marked as deleted when it is
// specifically loaded. // specifically loaded.
$fields = entity_load_multiple_by_properties('field_config', ['entity_type' => 'entity_test', 'field_name' => $field_definition['field_name'], 'bundle' => $field_definition['bundle'], 'include_deleted' => TRUE]); $fields = \Drupal::entityTypeManager()->getStorage('field_config')->loadByProperties(['entity_type' => 'entity_test', 'field_name' => $field_definition['field_name'], 'bundle' => $field_definition['bundle'], 'include_deleted' => TRUE]);
$this->assertEquals(0, count($fields), 'Field storage was deleted'); $this->assertEquals(0, count($fields), 'Field storage was deleted');
// Try to load the storage normally and make sure it does not show up. // Try to load the storage normally and make sure it does not show up.

View File

@ -177,7 +177,7 @@ function file_copy(FileInterface $source, $destination = NULL, $replace = FILE_E
// @todo Do not create a new entity in order to update it. See // @todo Do not create a new entity in order to update it. See
// https://www.drupal.org/node/2241865. // https://www.drupal.org/node/2241865.
if ($replace == FILE_EXISTS_REPLACE) { if ($replace == FILE_EXISTS_REPLACE) {
$existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]); $existing_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
if (count($existing_files)) { if (count($existing_files)) {
$existing = reset($existing_files); $existing = reset($existing_files);
$file->fid = $existing->id(); $file->fid = $existing->id();
@ -256,7 +256,7 @@ function file_move(FileInterface $source, $destination = NULL, $replace = FILE_E
$file->setFileUri($uri); $file->setFileUri($uri);
// If we are replacing an existing file re-use its database record. // If we are replacing an existing file re-use its database record.
if ($replace == FILE_EXISTS_REPLACE) { if ($replace == FILE_EXISTS_REPLACE) {
$existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]); $existing_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
if (count($existing_files)) { if (count($existing_files)) {
$existing = reset($existing_files); $existing = reset($existing_files);
$delete_source = TRUE; $delete_source = TRUE;
@ -579,7 +579,7 @@ function file_save_data($data, $destination = NULL, $replace = FILE_EXISTS_RENAM
// @todo Do not create a new entity in order to update it. See // @todo Do not create a new entity in order to update it. See
// https://www.drupal.org/node/2241865. // https://www.drupal.org/node/2241865.
if ($replace == FILE_EXISTS_REPLACE) { if ($replace == FILE_EXISTS_REPLACE) {
$existing_files = entity_load_multiple_by_properties('file', ['uri' => $uri]); $existing_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
if (count($existing_files)) { if (count($existing_files)) {
$existing = reset($existing_files); $existing = reset($existing_files);
$file->fid = $existing->id(); $file->fid = $existing->id();
@ -659,7 +659,7 @@ function file_theme() {
function file_file_download($uri) { function file_file_download($uri) {
// Get the file record based on the URI. If not in the database just return. // Get the file record based on the URI. If not in the database just return.
/** @var \Drupal\file\FileInterface[] $files */ /** @var \Drupal\file\FileInterface[] $files */
$files = entity_load_multiple_by_properties('file', ['uri' => $uri]); $files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
if (count($files)) { if (count($files)) {
foreach ($files as $item) { foreach ($files as $item) {
// Since some database servers sometimes use a case-insensitive comparison // Since some database servers sometimes use a case-insensitive comparison
@ -1095,7 +1095,7 @@ function _file_save_upload_single(\SplFileInfo $file_info, $form_field_name, $va
// @todo Do not create a new entity in order to update it. See // @todo Do not create a new entity in order to update it. See
// https://www.drupal.org/node/2241865. // https://www.drupal.org/node/2241865.
if ($replace == FileSystemInterface::EXISTS_REPLACE) { if ($replace == FileSystemInterface::EXISTS_REPLACE) {
$existing_files = entity_load_multiple_by_properties('file', ['uri' => $file->getFileUri()]); $existing_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $file->getFileUri()]);
if (count($existing_files)) { if (count($existing_files)) {
$existing = reset($existing_files); $existing = reset($existing_files);
$file->fid = $existing->id(); $file->fid = $existing->id();

View File

@ -151,7 +151,7 @@ function file_test_file_validate(File $file) {
*/ */
function file_test_file_download($uri) { function file_test_file_download($uri) {
if (\Drupal::state()->get('file_test.allow_all', FALSE)) { if (\Drupal::state()->get('file_test.allow_all', FALSE)) {
$files = entity_load_multiple_by_properties('file', ['uri' => $uri]); $files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $uri]);
$file = reset($files); $file = reset($files);
return file_get_content_headers($file); return file_get_content_headers($file);
} }

View File

@ -24,7 +24,7 @@ class LoadTest extends FileManagedUnitTestBase {
* Try to load a non-existent file by URI. * Try to load a non-existent file by URI.
*/ */
public function testLoadMissingFilepath() { public function testLoadMissingFilepath() {
$files = entity_load_multiple_by_properties('file', ['uri' => 'foobar://misc/druplicon.png']); $files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => 'foobar://misc/druplicon.png']);
$this->assertFalse(reset($files), "Try to load a file that doesn't exist in the database fails."); $this->assertFalse(reset($files), "Try to load a file that doesn't exist in the database fails.");
$this->assertFileHooksCalled([]); $this->assertFileHooksCalled([]);
} }
@ -33,7 +33,7 @@ class LoadTest extends FileManagedUnitTestBase {
* Try to load a non-existent file by status. * Try to load a non-existent file by status.
*/ */
public function testLoadInvalidStatus() { public function testLoadInvalidStatus() {
$files = entity_load_multiple_by_properties('file', ['status' => -99]); $files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['status' => -99]);
$this->assertFalse(reset($files), 'Trying to load a file with an invalid status fails.'); $this->assertFalse(reset($files), 'Trying to load a file with an invalid status fails.');
$this->assertFileHooksCalled([]); $this->assertFileHooksCalled([]);
} }
@ -64,9 +64,9 @@ class LoadTest extends FileManagedUnitTestBase {
// Load by path. // Load by path.
file_test_reset(); file_test_reset();
$by_path_files = entity_load_multiple_by_properties('file', ['uri' => $file->getFileUri()]); $by_path_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $file->getFileUri()]);
$this->assertFileHookCalled('load'); $this->assertFileHookCalled('load');
$this->assertEqual(1, count($by_path_files), 'entity_load_multiple_by_properties() returned an array of the correct size.'); $this->assertEqual(1, count($by_path_files), '\Drupal::entityTypeManager()->getStorage(\'file\')->loadByProperties() returned an array of the correct size.');
$by_path_file = reset($by_path_files); $by_path_file = reset($by_path_files);
$this->assertTrue($by_path_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.'); $this->assertTrue($by_path_file->file_test['loaded'], 'file_test_file_load() was able to modify the file during load.');
$this->assertEqual($by_path_file->id(), $file->id(), 'Loading by filepath got the correct fid.', 'File'); $this->assertEqual($by_path_file->id(), $file->id(), 'Loading by filepath got the correct fid.', 'File');

View File

@ -80,10 +80,9 @@ class ForumUninstallTest extends BrowserTestBase {
// Delete any forum terms. // Delete any forum terms.
$vid = $this->config('forum.settings')->get('vocabulary'); $vid = $this->config('forum.settings')->get('vocabulary');
$terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]); $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
foreach ($terms as $term) { $terms = $storage->loadByProperties(['vid' => $vid]);
$term->delete(); $storage->delete($terms);
}
// Ensure that the forum node type can not be deleted. // Ensure that the forum node type can not be deleted.
$this->drupalGet('admin/structure/types/manage/forum'); $this->drupalGet('admin/structure/types/manage/forum');
@ -137,7 +136,7 @@ class ForumUninstallTest extends BrowserTestBase {
// Delete all terms in the Forums vocabulary. Uninstalling the forum module // Delete all terms in the Forums vocabulary. Uninstalling the forum module
// will fail unless this is done. // will fail unless this is done.
$terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => 'forums']); $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => 'forums']);
foreach ($terms as $term) { foreach ($terms as $term) {
$term->delete(); $term->delete();
} }

View File

@ -78,7 +78,7 @@ class MenuUiLanguageTest extends BrowserTestBase {
]; ];
$this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save')); $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
// Check the link was added with the correct menu link default language. // Check the link was added with the correct menu link default language.
$menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]); $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['title' => $link_title]);
$menu_link = reset($menu_links); $menu_link = reset($menu_links);
$this->assertMenuLink([ $this->assertMenuLink([
'menu_name' => $menu_name, 'menu_name' => $menu_name,
@ -100,7 +100,7 @@ class MenuUiLanguageTest extends BrowserTestBase {
]; ];
$this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save')); $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
// Check the link was added with the correct new menu link default language. // Check the link was added with the correct new menu link default language.
$menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $link_title]); $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['title' => $link_title]);
$menu_link = reset($menu_links); $menu_link = reset($menu_links);
$this->assertMenuLink([ $this->assertMenuLink([
'menu_name' => $menu_name, 'menu_name' => $menu_name,

View File

@ -254,7 +254,7 @@ class MenuUiTest extends BrowserTestBase {
$this->assertNull(Menu::load($menu_name), 'Custom menu was deleted'); $this->assertNull(Menu::load($menu_name), 'Custom menu was deleted');
// Test if all menu links associated with the menu were removed from // Test if all menu links associated with the menu were removed from
// database. // database.
$result = entity_load_multiple_by_properties('menu_link_content', ['menu_name' => $menu_name]); $result = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['menu_name' => $menu_name]);
$this->assertFalse($result, 'All menu links associated with the custom menu were deleted.'); $this->assertFalse($result, 'All menu links associated with the custom menu were deleted.');
// Make sure there's no delete button on system menus. // Make sure there's no delete button on system menus.
@ -612,7 +612,7 @@ class MenuUiTest extends BrowserTestBase {
$this->assertResponse(200); $this->assertResponse(200);
$this->assertText('The menu link has been saved.'); $this->assertText('The menu link has been saved.');
$menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $title]); $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['title' => $title]);
$menu_link = reset($menu_links); $menu_link = reset($menu_links);
$this->assertTrue($menu_link, 'Menu link was found in database.'); $this->assertTrue($menu_link, 'Menu link was found in database.');
@ -660,7 +660,7 @@ class MenuUiTest extends BrowserTestBase {
'weight[0][value]' => '0', 'weight[0][value]' => '0',
]; ];
$this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save')); $this->drupalPostForm("admin/structure/menu/manage/{$this->menu->id()}/add", $edit, t('Save'));
$menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => $title]); $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['title' => $title]);
$last_link = reset($menu_links); $last_link = reset($menu_links);
$created_links[] = 'tools:' . $last_link->getPluginId(); $created_links[] = 'tools:' . $last_link->getPluginId();
} }

View File

@ -206,7 +206,7 @@ class BreadcrumbTest extends BrowserTestBase {
'link[0][uri]' => '/node', 'link[0][uri]' => '/node',
]; ];
$this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save')); $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
$menu_links = entity_load_multiple_by_properties('menu_link_content', ['title' => 'Root']); $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(['title' => 'Root']);
$link = reset($menu_links); $link = reset($menu_links);
$edit = [ $edit = [
@ -240,7 +240,7 @@ class BreadcrumbTest extends BrowserTestBase {
// the breadcrumb based on taxonomy term hierarchy. // the breadcrumb based on taxonomy term hierarchy.
$parent_tid = 0; $parent_tid = 0;
foreach ($tags as $name => $null) { foreach ($tags as $name => $null) {
$terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $name]); $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $name]);
$term = reset($terms); $term = reset($terms);
$tags[$name]['term'] = $term; $tags[$name]['term'] = $term;
if ($parent_tid) { if ($parent_tid) {
@ -261,7 +261,7 @@ class BreadcrumbTest extends BrowserTestBase {
'enabled[value]' => 1, 'enabled[value]' => 1,
]; ];
$this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save')); $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
$menu_links = entity_load_multiple_by_properties('menu_link_content', [ $menu_links = \Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties([
'title' => $edit['title[0][value]'], 'title' => $edit['title[0][value]'],
'link.uri' => 'internal:/taxonomy/term/' . $term->id(), 'link.uri' => 'internal:/taxonomy/term/' . $term->id(),
]); ]);

View File

@ -182,10 +182,10 @@ class DependencyTest extends ModuleTestBase {
// Ensure taxonomy has been loaded into the test-runner after forum was // Ensure taxonomy has been loaded into the test-runner after forum was
// enabled. // enabled.
\Drupal::moduleHandler()->load('taxonomy'); \Drupal::moduleHandler()->load('taxonomy');
$terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vid]); $storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
foreach ($terms as $term) { $terms = $storage->loadByProperties(['vid' => $vid]);
$term->delete(); $storage->delete($terms);
}
// Uninstall the forum module, and check that taxonomy now can also be // Uninstall the forum module, and check that taxonomy now can also be
// uninstalled. // uninstalled.
$edit = ['uninstall[forum]' => 'forum']; $edit = ['uninstall[forum]' => 'forum'];

View File

@ -147,7 +147,7 @@ class Vocabulary extends ConfigEntityBundleBase implements VocabularyInterface {
} }
// Load all Taxonomy module fields and delete those which use only this // Load all Taxonomy module fields and delete those which use only this
// vocabulary. // vocabulary.
$field_storages = entity_load_multiple_by_properties('field_storage_config', ['module' => 'taxonomy']); $field_storages = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadByProperties(['module' => 'taxonomy']);
foreach ($field_storages as $field_storage) { foreach ($field_storages as $field_storage) {
$modified_storage = FALSE; $modified_storage = FALSE;
// Term reference fields may reference terms from more than one // Term reference fields may reference terms from more than one

View File

@ -349,7 +349,7 @@ function taxonomy_term_load_multiple_by_name($name, $vocabulary = NULL) {
return []; return [];
} }
} }
return entity_load_multiple_by_properties('taxonomy_term', $values); return \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties($values);
} }
/** /**

View File

@ -31,7 +31,8 @@ class LoadMultipleTest extends TaxonomyTestBase {
$this->createTerm($vocabulary); $this->createTerm($vocabulary);
} }
// Load the terms from the vocabulary. // Load the terms from the vocabulary.
$terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vocabulary->id()]); $term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$terms = $term_storage->loadByProperties(['vid' => $vocabulary->id()]);
$count = count($terms); $count = count($terms);
$this->assertEqual($count, 5, format_string('Correct number of terms were loaded. @count terms.', ['@count' => $count])); $this->assertEqual($count, 5, format_string('Correct number of terms were loaded. @count terms.', ['@count' => $count]));
@ -47,13 +48,13 @@ class LoadMultipleTest extends TaxonomyTestBase {
$this->assertFalse($deleted_term); $this->assertFalse($deleted_term);
// Load terms from the vocabulary by vid. // Load terms from the vocabulary by vid.
$terms3 = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vocabulary->id()]); $terms3 = $term_storage->loadByProperties(['vid' => $vocabulary->id()]);
$this->assertEqual(count($terms3), 4, 'Correct number of terms were loaded.'); $this->assertEqual(count($terms3), 4, 'Correct number of terms were loaded.');
$this->assertFalse(isset($terms3[$deleted->id()])); $this->assertFalse(isset($terms3[$deleted->id()]));
// Create a single term and load it by name. // Create a single term and load it by name.
$term = $this->createTerm($vocabulary); $term = $this->createTerm($vocabulary);
$loaded_terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $term->getName()]); $loaded_terms = $term_storage->loadByProperties(['name' => $term->getName()]);
$this->assertEqual(count($loaded_terms), 1, 'One term was loaded.'); $this->assertEqual(count($loaded_terms), 1, 'One term was loaded.');
$loaded_term = reset($loaded_terms); $loaded_term = reset($loaded_terms);
$this->assertEqual($term->id(), $loaded_term->id(), 'Term loaded by name successfully.'); $this->assertEqual($term->id(), $loaded_term->id(), 'Term loaded by name successfully.');

View File

@ -85,7 +85,7 @@ class TaxonomyImageTest extends TaxonomyTestBase {
$edit['files[field_test_0]'] = \Drupal::service('file_system')->realpath($image->uri); $edit['files[field_test_0]'] = \Drupal::service('file_system')->realpath($image->uri);
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save')); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save'));
$this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save')); $this->drupalPostForm(NULL, ['field_test[0][alt]' => $this->randomMachineName()], t('Save'));
$terms = entity_load_multiple_by_properties('taxonomy_term', ['name' => $edit['name[0][value]']]); $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['name' => $edit['name[0][value]']]);
$term = reset($terms); $term = reset($terms);
$this->assertText(t('Created new term @name.', ['@name' => $term->getName()])); $this->assertText(t('Created new term @name.', ['@name' => $term->getName()]));

View File

@ -38,7 +38,7 @@ class TermKernelTest extends KernelTestBase {
$valid_term = $this->createTerm($vocabulary); $valid_term = $this->createTerm($vocabulary);
// Delete a valid term. // Delete a valid term.
$valid_term->delete(); $valid_term->delete();
$terms = entity_load_multiple_by_properties('taxonomy_term', ['vid' => $vocabulary->id()]); $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties(['vid' => $vocabulary->id()]);
$this->assertTrue(empty($terms), 'Vocabulary is empty after deletion'); $this->assertTrue(empty($terms), 'Vocabulary is empty after deletion');
// Delete an invalid term. Should not throw any notices. // Delete an invalid term. Should not throw any notices.

View File

@ -431,7 +431,7 @@ class UserCancelTest extends BrowserTestBase {
$this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Preview')); $this->drupalPostForm('comment/reply/node/' . $node->id() . '/comment', $edit, t('Preview'));
$this->drupalPostForm(NULL, [], t('Save')); $this->drupalPostForm(NULL, [], t('Save'));
$this->assertText(t('Your comment has been posted.')); $this->assertText(t('Your comment has been posted.'));
$comments = entity_load_multiple_by_properties('comment', ['subject' => $edit['subject[0][value]']]); $comments = \Drupal::entityTypeManager()->getStorage('comment')->loadByProperties(['subject' => $edit['subject[0][value]']]);
$comment = reset($comments); $comment = reset($comments);
$this->assertTrue($comment->id(), 'Comment found.'); $this->assertTrue($comment->id(), 'Comment found.');

View File

@ -91,6 +91,22 @@ class EntityLegacyTest extends KernelTestBase {
$this->assertEquals('entity_test.entity_test.default', $form_display->id()); $this->assertEquals('entity_test.entity_test.default', $form_display->id());
} }
/**
* @expectedDeprecation entity_load_multiple_by_properties() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use the entity type storage's loadByProperties() method. See https://www.drupal.org/node/3050910
*/
public function testEntityLoadMultipleByProperties() {
$this->assertCount(0, entity_load_multiple_by_properties('entity_test', ['name' => 'published entity']));
EntityTest::create(['name' => 'published entity'])->save();
$this->assertCount(1, entity_load_multiple_by_properties('entity_test', ['name' => 'published entity']));
$this->assertCount(0, entity_load_multiple_by_properties('entity_test_mul', ['name' => 'published entity']));
EntityTest::create(['name' => 'published entity'])->save();
EntityTestMul::create(['name' => 'published entity'])->save();
$this->assertCount(2, entity_load_multiple_by_properties('entity_test', ['name' => 'published entity']));
$this->assertCount(1, entity_load_multiple_by_properties('entity_test_mul', ['name' => 'published entity']));
}
/** /**
* @expectedDeprecation entity_view() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, $view_mode, $langcode) instead. See https://www.drupal.org/node/3033656 * @expectedDeprecation entity_view() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->view($entity, $view_mode, $langcode) instead. See https://www.drupal.org/node/3033656
* @expectedDeprecation entity_view_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->viewMultiple($entities, $view_mode, $langcode) instead. See https://www.drupal.org/node/3033656 * @expectedDeprecation entity_view_multiple() is deprecated in Drupal 8.0.0 and will be removed before Drupal 9.0.0. Use \Drupal::entityTypeManager()->getViewBuilder($entity->getEntityTypeId())->viewMultiple($entities, $view_mode, $langcode) instead. See https://www.drupal.org/node/3033656