Loading core/modules/editor/editor.module +10 −2 Original line number Diff line number Diff line Loading @@ -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(). Loading Loading @@ -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. * Loading @@ -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); })); } Loading core/modules/editor/tests/modules/src/Plugin/Field/FieldType/EditorTestTextLongItem.php 0 → 100644 +21 −0 Original line number Diff line number Diff line <?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 { } core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php +41 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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(); } /** Loading Loading @@ -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>'; Loading @@ -138,6 +152,10 @@ public function testEditorEntityHooks() { 'value' => $body_value, 'format' => 'filtered_html', ]; $description[] = [ 'value' => 'something', 'format' => 'filtered_html', ]; } // Test editor_entity_insert(): increment. Loading @@ -146,6 +164,7 @@ public function testEditorEntityHooks() { 'type' => 'page', 'title' => 'test', 'body' => $body, 'description' => $description, 'uid' => 1, ]); $node->save(); Loading Loading @@ -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) { Loading Loading
core/modules/editor/editor.module +10 −2 Original line number Diff line number Diff line Loading @@ -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(). Loading Loading @@ -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. * Loading @@ -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); })); } Loading
core/modules/editor/tests/modules/src/Plugin/Field/FieldType/EditorTestTextLongItem.php 0 → 100644 +21 −0 Original line number Diff line number Diff line <?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 { }
core/modules/editor/tests/src/Kernel/EditorFileUsageTest.php +41 −0 Original line number Diff line number Diff line Loading @@ -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; Loading Loading @@ -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(); } /** Loading Loading @@ -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>'; Loading @@ -138,6 +152,10 @@ public function testEditorEntityHooks() { 'value' => $body_value, 'format' => 'filtered_html', ]; $description[] = [ 'value' => 'something', 'format' => 'filtered_html', ]; } // Test editor_entity_insert(): increment. Loading @@ -146,6 +164,7 @@ public function testEditorEntityHooks() { 'type' => 'page', 'title' => 'test', 'body' => $body, 'description' => $description, 'uid' => 1, ]); $node->save(); Loading Loading @@ -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) { Loading