Skip to content
Snippets Groups Projects
Unverified Commit aeefe066 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2923428 by undertext, Manuel Garcia, andypost, badjava, amateescu,...

Issue #2923428 by undertext, Manuel Garcia, andypost, badjava, amateescu, douggreen: Wrong check for file status in _editor_record_file_usage
parent d948f9cb
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -441,8 +441,9 @@ function editor_entity_revision_delete(EntityInterface $entity) { ...@@ -441,8 +441,9 @@ function editor_entity_revision_delete(EntityInterface $entity) {
function _editor_record_file_usage(array $uuids, EntityInterface $entity) { function _editor_record_file_usage(array $uuids, EntityInterface $entity) {
foreach ($uuids as $uuid) { foreach ($uuids as $uuid) {
if ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid)) { if ($file = \Drupal::service('entity.repository')->loadEntityByUuid('file', $uuid)) {
if ($file->status !== FILE_STATUS_PERMANENT) { /** @var \Drupal\file\FileInterface $file */
$file->status = FILE_STATUS_PERMANENT; if ($file->isTemporary()) {
$file->setPermanent();
$file->save(); $file->save();
} }
\Drupal::service('file.usage')->add($file, 'editor', $entity->getEntityTypeId(), $entity->id()); \Drupal::service('file.usage')->add($file, 'editor', $entity->getEntityTypeId(), $entity->id());
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
*/ */
use Drupal\filter\FilterFormatInterface; use Drupal\filter\FilterFormatInterface;
use Drupal\file\FileInterface;
/** /**
* Implements hook_editor_js_settings_alter(). * Implements hook_editor_js_settings_alter().
...@@ -44,3 +45,13 @@ function editor_test_editor_info_alter(&$items) { ...@@ -44,3 +45,13 @@ function editor_test_editor_info_alter(&$items) {
unset($items['trex']); 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);
}
...@@ -59,6 +59,45 @@ protected function setUp() { ...@@ -59,6 +59,45 @@ protected function setUp() {
node_add_body_field($type); 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. * Tests the configurable text editor manager.
*/ */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment