diff --git a/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php b/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php index 3d064afcfdd5f9c993ffe2e0a312c7c7029a0cff..20f47975025ee853ac2b12d65101ca2188a2e311 100644 --- a/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php +++ b/core/modules/file/src/Plugin/Field/FieldType/FileFieldItemList.php @@ -81,9 +81,11 @@ public function delete() { parent::delete(); $entity = $this->getEntity(); - // Delete all file usages within this entity. + // If a translation is deleted only decrement the file usage by one. If the + // default translation is deleted remove all file usages within this entity. + $count = $entity->isDefaultTranslation() ? 0 : 1; foreach ($this->referencedEntities() as $file) { - \Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id(), 0); + \Drupal::service('file.usage')->delete($file, 'file', $entity->getEntityTypeId(), $entity->id(), $count); } } diff --git a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php b/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php index a7150e5170ba9ecb4b145c0f5044e2e5dca62714..7987bcf3420ade881e9eaa7e961aec17c53c3362 100644 --- a/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php +++ b/core/modules/file/src/Tests/FileOnTranslatedEntityTest.php @@ -30,7 +30,9 @@ protected function setUp() { parent::setUp(); // Create the "Basic page" node type. - $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page')); + // @todo Remove the disabling of new revision creation in + // https://www.drupal.org/node/1239558. + $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page', 'new_revision' => FALSE]); // Create a file field on the "Basic page" node type. $this->fieldName = strtolower($this->randomMachineName()); diff --git a/core/modules/file/tests/src/Kernel/UsageTest.php b/core/modules/file/tests/src/Kernel/UsageTest.php index fdf12311ba97641a48a9214926ef093ba0055b41..8e2601510b845c5342da99cd175b17af2bac8cb9 100644 --- a/core/modules/file/tests/src/Kernel/UsageTest.php +++ b/core/modules/file/tests/src/Kernel/UsageTest.php @@ -2,6 +2,13 @@ namespace Drupal\Tests\file\Kernel; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; +use Drupal\language\Entity\ConfigurableLanguage; +use Drupal\language\Entity\ContentLanguageSettings; +use Drupal\node\Entity\Node; +use Drupal\node\Entity\NodeType; + /** * Tests file usage functions. * @@ -203,4 +210,57 @@ function testTempFileCustomCleanup() { $this->assertTrue(file_exists($perm_new->getFileUri()), 'New permanent file was correctly ignored.'); } + /** + * Tests file usage with translated entities. + */ + public function testFileUsageWithEntityTranslation() { + /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */ + $file_usage = $this->container->get('file.usage'); + + $this->enableModules(['node', 'language']); + $this->installEntitySchema('node'); + $this->installSchema('node', ['node_access']); + + // Activate English and Romanian languages. + ConfigurableLanguage::create(['id' => 'en'])->save(); + ConfigurableLanguage::create(['id' => 'ro'])->save(); + + NodeType::create(['type' => 'page'])->save(); + ContentLanguageSettings::loadByEntityTypeBundle('node', 'page') + ->setLanguageAlterable(FALSE) + ->setDefaultLangcode('en') + ->save(); + // Create a file field attached to 'page' node-type. + FieldStorageConfig::create([ + 'type' => 'file', + 'entity_type' => 'node', + 'field_name' => 'file', + ])->save(); + FieldConfig::create([ + 'entity_type' => 'node', + 'bundle' => 'page', + 'field_name' => 'file', + 'label' => 'File', + ])->save(); + + // Create a node, attach a file and add a Romanian translation. + $node = Node::create(['type' => 'page', 'title' => 'Page']); + $node + ->set('file', $file = $this->createFile()) + ->addTranslation('ro', $node->getTranslation('en')->toArray()) + ->save(); + + // Check that the file is used twice. + $usage = $file_usage->listUsage($file); + $this->assertEquals(2, $usage['file']['node'][$node->id()]); + + // Remove the Romanian translation. + $node->removeTranslation('ro'); + $node->save(); + + // Check that one usage has been removed and is used only once now. + $usage = $file_usage->listUsage($file); + $this->assertEquals(1, $usage['file']['node'][$node->id()]); + } + } diff --git a/core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php b/core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php index e716d283c3375e610d4eb3bd397f200ca305326f..a8985ce4c77513ec722b322059d0b2430db27b65 100644 --- a/core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php +++ b/core/modules/image/src/Tests/ImageOnTranslatedEntityTest.php @@ -30,7 +30,9 @@ protected function setUp() { parent::setUp(); // Create the "Basic page" node type. - $this->drupalCreateContentType(array('type' => 'basicpage', 'name' => 'Basic page')); + // @todo Remove the disabling of new revision creation in + // https://www.drupal.org/node/1239558. + $this->drupalCreateContentType(['type' => 'basicpage', 'name' => 'Basic page', 'new_revision' => FALSE]); // Create a image field on the "Basic page" node type. $this->fieldName = strtolower($this->randomMachineName());