From 387f683a701bf32cafb64d6cf827f9d2d3f6268f Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Wed, 18 Sep 2013 13:29:35 +0300 Subject: [PATCH] Issue #2073033 by claudiu.cristea | yched: Optimize file usage updates in file/image fields. Drop file_usage(). --- core/includes/file.inc | 2 - core/modules/editor/editor.module | 4 +- .../Drupal/editor/Tests/EditorFileUsageTest.php | 15 ++-- core/modules/file/file.module | 21 ++--- core/modules/file/lib/Drupal/file/Entity/File.php | 4 +- .../lib/Drupal/file/FileUsage/FileUsageBase.php | 4 +- .../file/Plugin/field/field_type/FileField.php | 96 +++++++++++++++++----- .../file/Plugin/field/field_type/FileItem.php | 33 -------- .../file/lib/Drupal/file/Tests/DeleteTest.php | 13 +-- .../file/lib/Drupal/file/Tests/FileListingTest.php | 7 +- .../file/lib/Drupal/file/Tests/UsageTest.php | 22 ++--- core/modules/image/image.module | 12 +-- .../Tests/Upgrade/UserPictureUpgradePathTest.php | 5 +- 13 files changed, 126 insertions(+), 112 deletions(-) diff --git a/core/includes/file.inc b/core/includes/file.inc index bb537fb..47b02d2 100644 --- a/core/includes/file.inc +++ b/core/includes/file.inc @@ -912,7 +912,6 @@ function file_create_filename($basename, $directory) { * The file id. * * @see file_unmanaged_delete() - * @see file_usage()->listUsage() */ function file_delete($fid) { return file_delete_multiple(array($fid)); @@ -929,7 +928,6 @@ function file_delete($fid) { * The file id. * * @see file_unmanaged_delete() - * @see file_usage()->listUsage() */ function file_delete_multiple(array $fids) { entity_delete_multiple('file', $fids); diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module index e843c2d..cdd9c7c 100644 --- a/core/modules/editor/editor.module +++ b/core/modules/editor/editor.module @@ -471,7 +471,7 @@ function _editor_record_file_usage(array $uuids, EntityInterface $entity) { $file->status = FILE_STATUS_PERMANENT; $file->save(); } - file_usage()->add($file, 'editor', $entity->entityType(), $entity->id()); + \Drupal::service('file.usage')->add($file, 'editor', $entity->entityType(), $entity->id()); } } @@ -491,7 +491,7 @@ function _editor_record_file_usage(array $uuids, EntityInterface $entity) { function _editor_delete_file_usage(array $uuids, EntityInterface $entity, $count) { foreach ($uuids as $uuid) { $file = entity_load_by_uuid('file', $uuid); - file_usage()->delete($file, 'editor', $entity->entityType(), $entity->id(), $count); + \Drupal::service('file.usage')->delete($file, 'editor', $entity->entityType(), $entity->id(), $count); } } diff --git a/core/modules/editor/lib/Drupal/editor/Tests/EditorFileUsageTest.php b/core/modules/editor/lib/Drupal/editor/Tests/EditorFileUsageTest.php index 80384ae..2bac413 100644 --- a/core/modules/editor/lib/Drupal/editor/Tests/EditorFileUsageTest.php +++ b/core/modules/editor/lib/Drupal/editor/Tests/EditorFileUsageTest.php @@ -68,7 +68,8 @@ function testEditorEntityHooks() { $image->setFileUri('core/misc/druplicon.png'); $image->setFilename(drupal_basename($image->getFileUri())); $image->save(); - $this->assertIdentical(array(), file_usage()->listUsage($image), 'The image has zero usages.'); + $file_usage = $this->container->get('file.usage'); + $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages.'); // Test editor_entity_insert(): increment. $node = entity_create('node', array( @@ -80,7 +81,7 @@ function testEditorEntityHooks() { ) )); $node->save(); - $this->assertIdentical(array('editor' => array('node' => array(1 => '1'))), file_usage()->listUsage($image), 'The image has 1 usage.'); + $this->assertIdentical(array('editor' => array('node' => array(1 => '1'))), $file_usage->listUsage($image), 'The image has 1 usage.'); // Test editor_entity_update(): increment, twice, by creating new revisions. $node->setNewRevision(TRUE); @@ -88,7 +89,7 @@ function testEditorEntityHooks() { $second_revision_id = $node->getRevisionId(); $node->setNewRevision(TRUE); $node->save(); - $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), file_usage()->listUsage($image), 'The image has 3 usages.'); + $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image), 'The image has 3 usages.'); // Test hook_entity_update(): decrement, by modifying the last revision: // remove the data- attribute from the body field. @@ -97,21 +98,21 @@ function testEditorEntityHooks() { $new_value = str_replace('data-editor-file-uuid', 'data-editor-file-uuid-modified', $original_value); $body->setValue($new_value); $node->save(); - $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), file_usage()->listUsage($image), 'The image has 2 usages.'); + $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.'); // Test hook_entity_update(): increment, by modifying the last revision: // readd the data- attribute to the body field. $node->get('body')->offsetGet(0)->get('value')->setValue($original_value); $node->save(); - $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), file_usage()->listUsage($image), 'The image has 3 usages.'); + $this->assertIdentical(array('editor' => array('node' => array(1 => '3'))), $file_usage->listUsage($image), 'The image has 3 usages.'); // Test editor_entity_revision_delete(): decrement, by deleting a revision. entity_revision_delete('node', $second_revision_id); - $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), file_usage()->listUsage($image), 'The image has 2 usages.'); + $this->assertIdentical(array('editor' => array('node' => array(1 => '2'))), $file_usage->listUsage($image), 'The image has 2 usages.'); // Test editor_entity_delete(). $node->delete(); - $this->assertIdentical(array(), file_usage()->listUsage($image), 'The image has zero usages again.'); + $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages again.'); } } diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 68b33cf..2ae0a7c 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -118,15 +118,6 @@ function file_load($fid, $reset = FALSE) { } /** - * Returns the file usage service. - * - * @return Drupal\file\FileUsage\FileUsageInterface. - */ -function file_usage() { - return \Drupal::service('file.usage'); -} - -/** * Copies a file to a new location and adds a file record to the database. * * This function should be used when manipulating files that have records @@ -273,7 +264,7 @@ function file_move(File $source, $destination = NULL, $replace = FILE_EXISTS_REN \Drupal::moduleHandler()->invokeAll('file_move', array($file, $source)); // Delete the original if it's not in use elsewhere. - if ($delete_source && !file_usage()->listUsage($source)) { + if ($delete_source && !\Drupal::service('file.usage')->listUsage($source)) { $source->delete(); } @@ -696,7 +687,7 @@ function file_cron() { $result = \Drupal::entityManager()->getStorageController('file')->retrieveTemporaryFiles(); foreach ($result as $row) { if ($file = file_load($row->fid)) { - $references = file_usage()->listUsage($file); + $references = \Drupal::service('file.usage')->listUsage($file); if (empty($references)) { if (file_exists($file->getFileUri())) { $file->delete(); @@ -716,8 +707,8 @@ function file_cron() { * Saves file uploads to a new location. * * The files will be added to the {file_managed} table as temporary files. - * Temporary files are periodically cleaned. Use file_usage()->add() to register - * the usage of the file which will automatically mark it as permanent. + * Temporary files are periodically cleaned. Use the 'file.usage' service to + * register the usage of the file which will automatically mark it as permanent. * * @param $form_field_name * A string that is the associative array key of the upload form element in @@ -1353,7 +1344,7 @@ function file_managed_file_validate(&$element, &$form_state) { foreach ($fids as $fid) { if ($file = file_load($fid)) { if ($file->isPermanent()) { - $references = file_usage()->listUsage($file); + $references = \Drupal::service('file.usage')->listUsage($file); if (empty($references)) { form_error($element, t('The file used in the !name field may not be referenced.', array('!name' => $element['#title']))); } @@ -1846,7 +1837,7 @@ function file_get_file_references(File $file, $field = NULL, $age = EntityStorag // Fill the static cache, disregard $field and $field_type for now. if (!isset($references[$file->id()][$age])) { $references[$file->id()][$age] = array(); - $usage_list = file_usage()->listUsage($file); + $usage_list = \Drupal::service('file.usage')->listUsage($file); $file_usage_list = isset($usage_list['file']) ? $usage_list['file'] : array(); foreach ($file_usage_list as $entity_type => $entity_ids) { $entity_info = entity_get_info($entity_type); diff --git a/core/modules/file/lib/Drupal/file/Entity/File.php b/core/modules/file/lib/Drupal/file/Entity/File.php index b891253..935708b 100644 --- a/core/modules/file/lib/Drupal/file/Entity/File.php +++ b/core/modules/file/lib/Drupal/file/Entity/File.php @@ -199,10 +199,10 @@ public static function preDelete(EntityStorageControllerInterface $storage_contr foreach ($entities as $entity) { // Delete all remaining references to this file. - $file_usage = file_usage()->listUsage($entity); + $file_usage = \Drupal::service('file.usage')->listUsage($entity); if (!empty($file_usage)) { foreach ($file_usage as $module => $usage) { - file_usage()->delete($entity, $module); + \Drupal::service('file.usage')->delete($entity, $module); } } // Delete the actual file. Failures due to invalid files and files that diff --git a/core/modules/file/lib/Drupal/file/FileUsage/FileUsageBase.php b/core/modules/file/lib/Drupal/file/FileUsage/FileUsageBase.php index 2111492..aef295c 100644 --- a/core/modules/file/lib/Drupal/file/FileUsage/FileUsageBase.php +++ b/core/modules/file/lib/Drupal/file/FileUsage/FileUsageBase.php @@ -18,7 +18,7 @@ * Implements Drupal\file\FileUsage\FileUsageInterface::add(). */ public function add(File $file, $module, $type, $id, $count = 1) { - // Make sure that a used file is permament. + // Make sure that a used file is permanent. if (!$file->isPermanent()) { $file->setPermanent(); $file->save(); @@ -31,7 +31,7 @@ public function add(File $file, $module, $type, $id, $count = 1) { public function delete(File $file, $module, $type = NULL, $id = NULL, $count = 1) { // If there are no more remaining usages of this file, mark it as temporary, // which result in a delete through system_cron(). - $usage = file_usage()->listUsage($file); + $usage = \Drupal::service('file.usage')->listUsage($file); if (empty($usage)) { $file->setTemporary(); $file->save(); diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileField.php b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileField.php index 9483fd1..59f339b 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileField.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileField.php @@ -22,50 +22,102 @@ public function defaultValuesForm(array &$form, array &$form_state) { } /** * {@inheritdoc} */ - public function update() { - parent::update(); - $this->updateFileUsage(); + public function insert() { + parent::insert(); + $entity = $this->getEntity(); + + // Add a new usage for new uploaded files. + foreach ($this->targetEntities() as $file) { + \Drupal::service('file.usage')->add($file, 'file', $entity->entityType(), $entity->id()); + } } /** - * Updates the file usage. + * {@inheritdoc} */ - protected function updateFileUsage() { - $entity = $this->getRoot(); + public function update() { + parent::update(); + $entity = $this->getEntity(); + + // Get current target file entities and file ids. + $files = $this->targetEntities(); + $fids = array_keys($files); // On new revisions, all files are considered to be a new usage and no // deletion of previous file usages are necessary. if (!empty($entity->original) && $entity->getRevisionId() != $entity->original->getRevisionId()) { - foreach ($this->list as $item) { - file_usage()->add($item->entity, 'file', $entity->entityType(), $entity->id()); + foreach ($files as $file) { + \Drupal::service('file.usage')->add($file, 'file', $entity->entityType(), $entity->id()); } return; } - // Build a list of the current target IDs. - $fids = array(); - foreach ($this->list as $item) { - $fids[] = $item->target_id; - } - - // Compare the original field values with the ones that are being saved. + // Get the file IDs attached to field before this update. $field_name = $this->getFieldDefinition()->getFieldName(); $original_fids = array(); $original_items = $entity->original->getTranslation($this->getLangcode())->$field_name; foreach ($original_items as $item) { $original_fids[] = $item->target_id; - if ($item->target_id && !in_array($item->target_id, $fids)) { - // Decrement the file usage count by 1. - file_usage()->delete($item->entity, 'file', $entity->entityType(), $entity->id()); - } + } + + // Decrement file usage by 1 for files that were removed from the field. + $removed_fids = array_filter(array_diff($original_fids, $fids)); + $removed_files = \Drupal::entityManager()->getStorageController('file')->loadMultiple($removed_fids); + foreach ($removed_files as $file) { + \Drupal::service('file.usage')->delete($file, 'file', $entity->entityType(), $entity->id()); } // Add new usage entries for newly added files. - foreach ($this->list as $item) { - if ($item->target_id && !in_array($item->target_id, $original_fids)) { - file_usage()->add($item->entity, 'file', $entity->entityType(), $entity->id()); + foreach ($files as $fid => $file) { + if (!in_array($fid, $original_fids)) { + \Drupal::service('file.usage')->add($file, 'file', $entity->entityType(), $entity->id()); } } } + /** + * {@inheritdoc} + */ + public function delete() { + parent::delete(); + $entity = $this->getEntity(); + + // Delete all file usages within this entity. + foreach ($this->targetEntities() as $file) { + \Drupal::service('file.usage')->delete($file, 'file', $entity->entityType(), $entity->id(), 0); + } + } + + /** + * {@inheritdoc} + */ + public function deleteRevision() { + parent::deleteRevision(); + $entity = $this->getEntity(); + + // Decrement the file usage count by 1. + foreach ($this->targetEntities() as $file) { + \Drupal::service('file.usage')->delete($file, 'file', $entity->entityType(), $entity->id()); + } + } + + /** + * Collects target file entities for this field. + * + * @return array + * An array with the list of target file entities keyed by file id. + * + * @todo Drop this when https://drupal.org/node/2073661 lands. + */ + protected function targetEntities() { + $ids = array(); + foreach ($this->list as $item) { + $ids[] = $item->target_id; + } + // Prevent NULLs as target IDs. + $ids = array_filter($ids); + + return $ids ? \Drupal::entityManager()->getStorageController('file')->loadMultiple($ids) : array(); + } + } diff --git a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php index 2ee23f4..f0f00bf 100644 --- a/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php +++ b/core/modules/file/lib/Drupal/file/Plugin/field/field_type/FileItem.php @@ -203,39 +203,6 @@ public function instanceSettingsForm(array $form, array &$form_state) { /** * {@inheritdoc} */ - public function insert() { - // @todo Move in FileField in https://drupal.org/node/2073033. - $entity = $this->getRoot(); - - // Add a new usage for this new uploaded file. - file_usage()->add($this->entity, 'file', $entity->entityType(), $entity->id()); - } - - /** - * {@inheritdoc} - */ - public function delete() { - // @todo Move in FileField in https://drupal.org/node/2073033. - $entity = $this->getRoot(); - - // Delete all file usages within this entity. - file_usage()->delete($this->entity, 'file', $entity->entityType(), $entity->id(), 0); - } - - /** - * {@inheritdoc} - */ - public function deleteRevision() { - // @todo Move in FileField in https://drupal.org/node/2073033. - $entity = $this->getRoot(); - - // Decrement the file usage count by 1. - file_usage()->delete($this->entity, 'file', $entity->entityType(), $entity->id()); - } - - /** - * {@inheritdoc} - */ public function isEmpty() { return empty($this->target_id); } diff --git a/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php b/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php index 9bd52bc..c6486fe 100644 --- a/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/DeleteTest.php @@ -38,11 +38,12 @@ function testUnused() { */ function testInUse() { $file = $this->createFile(); - file_usage()->add($file, 'testing', 'test', 1); - file_usage()->add($file, 'testing', 'test', 1); + $file_usage = $this->container->get('file.usage'); + $file_usage->add($file, 'testing', 'test', 1); + $file_usage->add($file, 'testing', 'test', 1); - file_usage()->delete($file, 'testing', 'test', 1); - $usage = file_usage()->listUsage($file); + $file_usage->delete($file, 'testing', 'test', 1); + $usage = $file_usage->listUsage($file); $this->assertEqual($usage['testing']['test'], array(1 => 1), 'Test file is still in use.'); $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.'); $this->assertTrue(file_load($file->id()), 'File still exists in the database.'); @@ -50,8 +51,8 @@ function testInUse() { // Clear out the call to hook_file_load(). file_test_reset(); - file_usage()->delete($file, 'testing', 'test', 1); - $usage = file_usage()->listUsage($file); + $file_usage->delete($file, 'testing', 'test', 1); + $usage = $file_usage->listUsage($file); $this->assertFileHooksCalled(array('load', 'update')); $this->assertTrue(empty($usage), 'File usage data was removed.'); $this->assertTrue(file_exists($file->getFileUri()), 'File still exists on the disk.'); diff --git a/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php b/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php index d0681b2..4795c7a 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileListingTest.php @@ -60,7 +60,8 @@ protected function sumUsages($usage) { * Tests file overview with different user permissions. */ function testFileListingPages() { - // Users without sufficent permissions should not see file listing. + $file_usage = $this->container->get('file.usage'); + // Users without sufficient permissions should not see file listing. $this->drupalLogin($this->base_user); $this->drupalGet('admin/content/files'); $this->assertResponse(403); @@ -102,11 +103,11 @@ function testFileListingPages() { $this->drupalGet('admin/content/files'); $file = entity_load('file', $orphaned_file); - $usage = $this->sumUsages(file_usage()->listUsage($file)); + $usage = $this->sumUsages($file_usage->listUsage($file)); $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage); $file = entity_load('file', $used_file); - $usage = $this->sumUsages(file_usage()->listUsage($file)); + $usage = $this->sumUsages($file_usage->listUsage($file)); $this->assertRaw('admin/content/files/usage/' . $file->id() . '">' . $usage); $result = $this->xpath("//td[contains(@class, 'views-field-status') and contains(text(), :value)]", array(':value' => t('Temporary'))); diff --git a/core/modules/file/lib/Drupal/file/Tests/UsageTest.php b/core/modules/file/lib/Drupal/file/Tests/UsageTest.php index 17e51d8..c4b053c 100644 --- a/core/modules/file/lib/Drupal/file/Tests/UsageTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/UsageTest.php @@ -20,7 +20,7 @@ public static function getInfo() { } /** - * Tests file_usage()->listUsage(). + * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::listUsage(). */ function testGetUsage() { $file = $this->createFile(); @@ -43,7 +43,7 @@ function testGetUsage() { )) ->execute(); - $usage = file_usage()->listUsage($file); + $usage = $this->container->get('file.usage')->listUsage($file); $this->assertEqual(count($usage['testing']), 2, 'Returned the correct number of items.'); $this->assertTrue(isset($usage['testing']['foo'][1]), 'Returned the correct id.'); @@ -53,15 +53,16 @@ function testGetUsage() { } /** - * Tests file_usage()->add(). + * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::add(). */ function testAddUsage() { $file = $this->createFile(); - file_usage()->add($file, 'testing', 'foo', 1); + $file_usage = $this->container->get('file.usage'); + $file_usage->add($file, 'testing', 'foo', 1); // Add the file twice to ensure that the count is incremented rather than // creating additional records. - file_usage()->add($file, 'testing', 'bar', 2); - file_usage()->add($file, 'testing', 'bar', 2); + $file_usage->add($file, 'testing', 'bar', 2); + $file_usage->add($file, 'testing', 'bar', 2); $usage = db_select('file_usage', 'f') ->fields('f') @@ -78,10 +79,11 @@ function testAddUsage() { } /** - * Tests file_usage()->delete(). + * Tests \Drupal\file\FileUsage\DatabaseFileUsageBackend::delete(). */ function testRemoveUsage() { $file = $this->createFile(); + $file_usage = $this->container->get('file.usage'); db_insert('file_usage') ->fields(array( 'fid' => $file->id(), @@ -93,7 +95,7 @@ function testRemoveUsage() { ->execute(); // Normal decrement. - file_usage()->delete($file, 'testing', 'bar', 2); + $file_usage->delete($file, 'testing', 'bar', 2); $count = db_select('file_usage', 'f') ->fields('f', array('count')) ->condition('f.fid', $file->id()) @@ -102,7 +104,7 @@ function testRemoveUsage() { $this->assertEqual(2, $count, 'The count was decremented correctly.'); // Multiple decrement and removal. - file_usage()->delete($file, 'testing', 'bar', 2, 2); + $file_usage->delete($file, 'testing', 'bar', 2, 2); $count = db_select('file_usage', 'f') ->fields('f', array('count')) ->condition('f.fid', $file->id()) @@ -111,7 +113,7 @@ function testRemoveUsage() { $this->assertIdentical(FALSE, $count, 'The count was removed entirely when empty.'); // Non-existent decrement. - file_usage()->delete($file, 'testing', 'bar', 2); + $file_usage->delete($file, 'testing', 'bar', 2); $count = db_select('file_usage', 'f') ->fields('f', array('count')) ->condition('f.fid', $file->id()) diff --git a/core/modules/image/image.module b/core/modules/image/image.module index 910612b..990ff45 100644 --- a/core/modules/image/image.module +++ b/core/modules/image/image.module @@ -458,12 +458,12 @@ function image_field_entity_update(FieldInterface $field) { if ($file_new) { $file_new->status = FILE_STATUS_PERMANENT; $file_new->save(); - file_usage()->add($file_new, 'image', 'default_image', $field->uuid); + \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field->uuid); } // Is there an old file? if ($fid_old && ($file_old = file_load($fid_old[0]))) { - file_usage()->delete($file_old, 'image', 'default_image', $field->uuid); + \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field->uuid); } } @@ -505,11 +505,11 @@ function image_field_instance_update(FieldInstanceInterface $field_instance) { if ($file_new) { $file_new->status = FILE_STATUS_PERMANENT; $file_new->save(); - file_usage()->add($file_new, 'image', 'default_image', $field_instance->uuid); + \Drupal::service('file.usage')->add($file_new, 'image', 'default_image', $field_instance->uuid); } // Delete the old file, if present. if ($fid_old && ($file_old = file_load($fid_old[0]))) { - file_usage()->delete($file_old, 'image', 'default_image', $field_instance->uuid); + \Drupal::service('file.usage')->delete($file_old, 'image', 'default_image', $field_instance->uuid); } } @@ -533,7 +533,7 @@ function image_field_entity_delete(FieldInterface $field) { // The value of a managed_file element can be an array if #extended == TRUE. $fid = (isset($field->settings['default_image']['fids']) ? $field->settings['default_image']['fids'] : $field->settings['default_image']); if ($fid && ($file = file_load($fid[0]))) { - file_usage()->delete($file, 'image', 'default_image', $field->uuid); + \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field->uuid); } } @@ -556,6 +556,6 @@ function image_field_instance_delete(FieldInstanceInterface $field_instance) { // Remove the default image when the instance is deleted. if ($fid && ($file = file_load($fid))) { - file_usage()->delete($file, 'image', 'default_image', $field_instance->uuid); + \Drupal::service('file.usage')->delete($file, 'image', 'default_image', $field_instance->uuid); } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php index c8825af..a6c9033 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UserPictureUpgradePathTest.php @@ -39,6 +39,7 @@ public function setUp() { */ public function testUserPictureUpgrade() { $this->assertTrue($this->performUpgrade(), 'The upgrade was completed successfully.'); + $file_usage = $this->container->get('file.usage'); // Retrieve the field instance and check for migrated settings. $instance = field_info_instance('user', 'user_picture', 'user'); @@ -51,7 +52,7 @@ public function testUserPictureUpgrade() { $this->assertFalse(empty($file->uuid->value)); // Check file usage for the default image. - $usage = file_usage()->listUsage($file); + $usage = $file_usage->listUsage($file); $field = field_info_field('user', 'user_picture'); $this->assertTrue(isset($usage['image']['default_image'][$field['uuid']])); @@ -71,7 +72,7 @@ public function testUserPictureUpgrade() { $user = user_load(1); $file = $user->user_picture->entity; $this->assertEqual('public://user_pictures_dir/faked_image.png', $file->getFileUri()); - $usage = file_usage()->listUsage($file); + $usage = $file_usage->listUsage($file); $this->assertEqual(1, $usage['file']['user'][1]); } -- 1.8.3.1