diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module index 0fb4267e776a5c375ca0ba1b717b3822674c3208..25b22e7096b1ad4243ecfd4dc15081506fc4605e 100644 --- a/core/modules/editor/editor.module +++ b/core/modules/editor/editor.module @@ -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); })); } diff --git a/core/modules/editor/tests/modules/src/Plugin/Field/FieldType/EditorTestTextLongItem.php b/core/modules/editor/tests/modules/src/Plugin/Field/FieldType/EditorTestTextLongItem.php new file mode 100644 index 0000000000000000000000000000000000000000..14d22cab7fcb34312a0ff64a29918c6b6c772291 --- /dev/null +++ b/core/modules/editor/tests/modules/src/Plugin/Field/FieldType/EditorTestTextLongItem.php @@ -0,0 +1,21 @@ +<?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 { + +} diff --git a/core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php b/core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php index f01e433c3a1773de24af72c392a6a24a13120272..8137147561b8fa155c833c04e4be255b1c35e1cf 100644 --- a/core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php +++ b/core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php @@ -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) {