diff --git a/core/modules/editor/editor.module b/core/modules/editor/editor.module index dccf823..a5091e8 100644 --- a/core/modules/editor/editor.module +++ b/core/modules/editor/editor.module @@ -570,6 +570,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. * @@ -583,8 +586,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, 'Drupal\text\Plugin\Field\FieldType\TextItemBase'); })); } 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 0000000..607ab29 --- /dev/null +++ b/core/modules/editor/tests/modules/src/Plugin/Field/FieldType/EditorTestTextLongItem.php @@ -0,0 +1,21 @@ + '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(); } /** @@ -83,6 +96,7 @@ public function testEditorEntityHooks() { } $body = []; + $description = []; foreach ($image_entities as $key => $image_entity) { // Don't be rude, say hello. $body_value = '

Hello, world!

'; @@ -99,6 +113,10 @@ public function testEditorEntityHooks() { 'value' => $body_value, 'format' => 'filtered_html', ]; + $description[] = [ + 'value' => 'something', + 'format' => 'filtered_html', + ]; } // Test editor_entity_insert(): increment. @@ -107,6 +125,7 @@ public function testEditorEntityHooks() { 'type' => 'page', 'title' => 'test', 'body' => $body, + 'description' => $description, 'uid' => 1, ]); $node->save(); @@ -199,6 +218,28 @@ public function testEditorEntityHooks() { $this->assertIdentical(['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->assertIdentical(['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->assertIdentical(['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) {