Loading core/modules/editor/editor.module +3 −2 Original line number Diff line number Diff line Loading @@ -441,8 +441,9 @@ function editor_entity_revision_delete(EntityInterface $entity) { function _editor_record_file_usage(array $uuids, EntityInterface $entity) { foreach ($uuids as $uuid) { if ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid)) { if ($file->status !== FILE_STATUS_PERMANENT) { $file->status = FILE_STATUS_PERMANENT; /** @var \Drupal\file\FileInterface $file */ if ($file->isTemporary()) { $file->setPermanent(); $file->save(); } \Drupal::service('file.usage')->add($file, 'editor', $entity->getEntityTypeId(), $entity->id()); Loading core/modules/editor/tests/modules/editor_test.module +11 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ */ use Drupal\filter\FilterFormatInterface; use Drupal\file\FileInterface; /** * Implements hook_editor_js_settings_alter(). Loading Loading @@ -44,3 +45,13 @@ function editor_test_editor_info_alter(&$items) { unset($items['trex']); } } /** * Implements hook_ENTITY_TYPE_presave() for file entities. */ function editor_test_file_presave(FileInterface $file) { // Use state to keep track of how many times a file is saved. $file_save_count = \Drupal::state()->get('editor_test.file_save_count', []); $file_save_count[$file->getFilename()] = isset($file_save_count[$file->getFilename()]) ? $file_save_count[$file->getFilename()] + 1 : 1; \Drupal::state()->set('editor_test.file_save_count', $file_save_count); } core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php +39 −0 Original line number Diff line number Diff line Loading @@ -59,6 +59,45 @@ protected function setUp() { node_add_body_field($type); } /** * Tests file save operations when node with referenced files is saved. */ public function testFileSaveOperations() { $permanent_image = File::create([ 'uri' => 'core/misc/druplicon.png', 'status' => 1, ]); $permanent_image->save(); $temporary_image = File::create([ 'uri' => 'core/misc/tree.png', 'status' => 0, ]); $temporary_image->save(); $body_value = '<img data-entity-type="file" data-entity-uuid="' . $permanent_image->uuid() . '" />'; $body_value .= '<img data-entity-type="file" data-entity-uuid="' . $temporary_image->uuid() . '" />'; $body[] = [ 'value' => $body_value, 'format' => 'filtered_html', ]; $node = Node::create([ 'type' => 'page', 'title' => 'test', 'body' => $body, 'uid' => 1, ]); $node->save(); $file_save_count = \Drupal::state()->get('editor_test.file_save_count', []); $this->assertEquals(1, $file_save_count[$permanent_image->getFilename()]); $this->assertEquals(2, $file_save_count[$temporary_image->getFilename()]); // Assert both images are now permanent. $permanent_image = File::load($permanent_image->id()); $temporary_image = File::load($temporary_image->id()); $this->assertTrue($permanent_image->isPermanent(), 'Permanent image was saved as permanent.'); $this->assertTrue($temporary_image->isPermanent(), 'Temporary image was saved as permanent.'); } /** * Tests the configurable text editor manager. */ Loading Loading
core/modules/editor/editor.module +3 −2 Original line number Diff line number Diff line Loading @@ -441,8 +441,9 @@ function editor_entity_revision_delete(EntityInterface $entity) { function _editor_record_file_usage(array $uuids, EntityInterface $entity) { foreach ($uuids as $uuid) { if ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid)) { if ($file->status !== FILE_STATUS_PERMANENT) { $file->status = FILE_STATUS_PERMANENT; /** @var \Drupal\file\FileInterface $file */ if ($file->isTemporary()) { $file->setPermanent(); $file->save(); } \Drupal::service('file.usage')->add($file, 'editor', $entity->getEntityTypeId(), $entity->id()); Loading
core/modules/editor/tests/modules/editor_test.module +11 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ */ use Drupal\filter\FilterFormatInterface; use Drupal\file\FileInterface; /** * Implements hook_editor_js_settings_alter(). Loading Loading @@ -44,3 +45,13 @@ function editor_test_editor_info_alter(&$items) { unset($items['trex']); } } /** * Implements hook_ENTITY_TYPE_presave() for file entities. */ function editor_test_file_presave(FileInterface $file) { // Use state to keep track of how many times a file is saved. $file_save_count = \Drupal::state()->get('editor_test.file_save_count', []); $file_save_count[$file->getFilename()] = isset($file_save_count[$file->getFilename()]) ? $file_save_count[$file->getFilename()] + 1 : 1; \Drupal::state()->set('editor_test.file_save_count', $file_save_count); }
core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php +39 −0 Original line number Diff line number Diff line Loading @@ -59,6 +59,45 @@ protected function setUp() { node_add_body_field($type); } /** * Tests file save operations when node with referenced files is saved. */ public function testFileSaveOperations() { $permanent_image = File::create([ 'uri' => 'core/misc/druplicon.png', 'status' => 1, ]); $permanent_image->save(); $temporary_image = File::create([ 'uri' => 'core/misc/tree.png', 'status' => 0, ]); $temporary_image->save(); $body_value = '<img data-entity-type="file" data-entity-uuid="' . $permanent_image->uuid() . '" />'; $body_value .= '<img data-entity-type="file" data-entity-uuid="' . $temporary_image->uuid() . '" />'; $body[] = [ 'value' => $body_value, 'format' => 'filtered_html', ]; $node = Node::create([ 'type' => 'page', 'title' => 'test', 'body' => $body, 'uid' => 1, ]); $node->save(); $file_save_count = \Drupal::state()->get('editor_test.file_save_count', []); $this->assertEquals(1, $file_save_count[$permanent_image->getFilename()]); $this->assertEquals(2, $file_save_count[$temporary_image->getFilename()]); // Assert both images are now permanent. $permanent_image = File::load($permanent_image->id()); $temporary_image = File::load($temporary_image->id()); $this->assertTrue($permanent_image->isPermanent(), 'Permanent image was saved as permanent.'); $this->assertTrue($temporary_image->isPermanent(), 'Temporary image was saved as permanent.'); } /** * Tests the configurable text editor manager. */ Loading