Commit 7e2ab24b authored by alexpott's avatar alexpott

Issue #2326537 by olli: Fixed editor.module causes fatal error for images with...

Issue #2326537 by olli: Fixed editor.module causes fatal error for images with invalid or non-existing file UUIDs.
parent b55880f8
...@@ -409,12 +409,13 @@ function editor_entity_revision_delete(EntityInterface $entity) { ...@@ -409,12 +409,13 @@ 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) {
$file = entity_load_by_uuid('file', $uuid); if ($file = entity_load_by_uuid('file', $uuid)) {
if ($file->status !== FILE_STATUS_PERMANENT) { if ($file->status !== FILE_STATUS_PERMANENT) {
$file->status = FILE_STATUS_PERMANENT; $file->status = FILE_STATUS_PERMANENT;
$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());
} }
} }
...@@ -433,8 +434,9 @@ function _editor_record_file_usage(array $uuids, EntityInterface $entity) { ...@@ -433,8 +434,9 @@ function _editor_record_file_usage(array $uuids, EntityInterface $entity) {
*/ */
function _editor_delete_file_usage(array $uuids, EntityInterface $entity, $count) { function _editor_delete_file_usage(array $uuids, EntityInterface $entity, $count) {
foreach ($uuids as $uuid) { foreach ($uuids as $uuid) {
$file = entity_load_by_uuid('file', $uuid); if ($file = entity_load_by_uuid('file', $uuid)) {
\Drupal::service('file.usage')->delete($file, 'editor', $entity->getEntityTypeId(), $entity->id(), $count); \Drupal::service('file.usage')->delete($file, 'editor', $entity->getEntityTypeId(), $entity->id(), $count);
}
} }
} }
......
...@@ -62,13 +62,18 @@ public function testEditorEntityHooks() { ...@@ -62,13 +62,18 @@ public function testEditorEntityHooks() {
$file_usage = $this->container->get('file.usage'); $file_usage = $this->container->get('file.usage');
$this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages.'); $this->assertIdentical(array(), $file_usage->listUsage($image), 'The image has zero usages.');
$body_value = '<p>Hello, world!</p><img src="awesome-llama.jpg" data-editor-file-uuid="' . $image->uuid() . '" />';
// Test handling of an invalid data- attribute.
$body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="invalid-editor-file-uuid-value" />';
// Test handling of a non-existing UUID.
$body_value .= '<img src="awesome-llama.jpg" data-editor-file-uuid="30aac704-ba2c-40fc-b609-9ed121aa90f4" />';
// Test editor_entity_insert(): increment. // Test editor_entity_insert(): increment.
$this->createUser(); $this->createUser();
$node = entity_create('node', array( $node = entity_create('node', array(
'type' => 'page', 'type' => 'page',
'title' => 'test', 'title' => 'test',
'body' => array( 'body' => array(
'value' => '<p>Hello, world!</p><img src="awesome-llama.jpg" data-editor-file-uuid="' . $image->uuid() . '" />', 'value' => $body_value,
'format' => 'filtered_html', 'format' => 'filtered_html',
), ),
'uid' => 1, 'uid' => 1,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment