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

Issue #2857444 by nedjo, jofitz, gaurav.kapoor, Wim Leers: Editor module fails...

Issue #2857444 by nedjo, jofitz, gaurav.kapoor, Wim Leers: Editor module fails to track usage of files embedded in non-core fields
parent bb592590
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\filter\FilterFormatInterface;
use Drupal\filter\Plugin\FilterInterface;
use Drupal\text\Plugin\Field\FieldType\TextItemBase;
/**
* Implements hook_help().
......@@ -587,6 +588,9 @@ function _editor_get_file_uuids_by_field(EntityInterface $entity) {
/**
* Determines the formatted text fields on an entity.
*
* A field type is considered to provide formatted text if its class is a
* subclass of Drupal\text\Plugin\Field\FieldType\TextItemBase.
*
* @param \Drupal\Core\Entity\FieldableEntityInterface $entity
* An entity whose fields to analyze.
*
......@@ -600,8 +604,12 @@ function _editor_get_formatted_text_fields(FieldableEntityInterface $entity) {
}
// Only return formatted text fields.
return array_keys(array_filter($field_definitions, function (FieldDefinitionInterface $definition) {
return in_array($definition->getType(), ['text', 'text_long', 'text_with_summary'], TRUE);
// @todo: improve as part of https://www.drupal.org/node/2732429
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
return array_keys(array_filter($field_definitions, function (FieldDefinitionInterface $definition) use ($field_type_manager) {
$type = $definition->getType();
$plugin_class = $field_type_manager->getPluginClass($type);
return is_subclass_of($plugin_class, TextItemBase::class);
}));
}
......
<?php
namespace Drupal\editor_test\Plugin\Field\FieldType;
use Drupal\text\Plugin\Field\FieldType\TextLongItem;
/**
* Plugin implementation of the 'editor_test_text_long' field type.
*
* @FieldType(
* id = "editor_test_text_long",
* label = @Translation("Filter test text (formatted, long)"),
* description = @Translation("This field stores a long text with a text format."),
* category = @Translation("Text"),
* default_widget = "text_textarea",
* default_formatter = "text_default"
* )
*/
class EditorTestTextLongItem extends TextLongItem {
}
......@@ -7,6 +7,7 @@
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\file\Entity\File;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\filter\Entity\FilterFormat;
......@@ -57,6 +58,18 @@ protected function setUp(): void {
$type = NodeType::create(['type' => 'page', 'name' => 'page']);
$type->save();
node_add_body_field($type);
FieldStorageConfig::create([
'field_name' => 'description',
'entity_type' => 'node',
'type' => 'editor_test_text_long',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
])->save();
FieldConfig::create([
'field_name' => 'description',
'entity_type' => 'node',
'bundle' => 'page',
'label' => 'Description',
])->save();
}
/**
......@@ -122,6 +135,7 @@ public function testEditorEntityHooks() {
}
$body = [];
$description = [];
foreach ($image_entities as $key => $image_entity) {
// Don't be rude, say hello.
$body_value = '<p>Hello, world!</p>';
......@@ -138,6 +152,10 @@ public function testEditorEntityHooks() {
'value' => $body_value,
'format' => 'filtered_html',
];
$description[] = [
'value' => 'something',
'format' => 'filtered_html',
];
}
// Test editor_entity_insert(): increment.
......@@ -146,6 +164,7 @@ public function testEditorEntityHooks() {
'type' => 'page',
'title' => 'test',
'body' => $body,
'description' => $description,
'uid' => 1,
]);
$node->save();
......@@ -238,6 +257,28 @@ public function testEditorEntityHooks() {
$this->assertSame(['editor' => ['node' => [1 => '2']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
}
// Empty out the body and summary. The number of usages should decrease by
// one.
foreach ($original_values as $key => $original_value) {
$node->body[$key]->value = '';
$node->body[$key]->summary = '';
}
$node->save();
foreach ($image_entities as $key => $image_entity) {
$this->assertSame(['editor' => ['node' => [1 => '1']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 1 usage.');
}
// Set the field of a custom field type that is a subclass of
// Drupal\text\Plugin\Field\FieldType\TextItemBase. The number of usages
// should increase by one.
foreach ($original_values as $key => $original_value) {
$node->description[$key]->value = $original_value;
}
$node->save();
foreach ($image_entities as $key => $image_entity) {
$this->assertSame(['editor' => ['node' => [1 => '2']]], $file_usage->listUsage($image_entity), 'The image ' . $image_paths[$key] . ' has 2 usages.');
}
// Test editor_entity_delete().
$node->delete();
foreach ($image_entities as $key => $image_entity) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment