.../edit/lib/Drupal/edit/EditController.php | 7 +-- .../edit/lib/Drupal/edit/EditPluginInterface.php | 20 +++---- core/modules/edit/lib/Drupal/edit/EditorBase.php | 4 +- .../edit/lib/Drupal/edit/EditorSelector.php | 6 +-- .../lib/Drupal/edit/EditorSelectorInterface.php | 10 ++-- .../edit/lib/Drupal/edit/MetadataGenerator.php | 20 +++---- .../lib/Drupal/edit/MetadataGeneratorInterface.php | 18 +++---- .../edit/Plugin/InPlaceEditor/FormEditor.php | 4 +- .../edit/Plugin/InPlaceEditor/PlainTextEditor.php | 6 ++- .../lib/Drupal/edit/Tests/EditorSelectionTest.php | 56 +++++++++++--------- .../Drupal/edit/Tests/MetadataGeneratorTest.php | 13 +++-- .../Plugin/InPlaceEditor/WysiwygEditor.php | 12 +++-- .../Drupal/editor/Plugin/InPlaceEditor/Editor.php | 12 +++-- .../Drupal/editor/Tests/EditIntegrationTest.php | 31 ++++++----- 14 files changed, 112 insertions(+), 107 deletions(-) diff --git a/core/modules/edit/lib/Drupal/edit/EditController.php b/core/modules/edit/lib/Drupal/edit/EditController.php index 2c14041..c3c944c 100644 --- a/core/modules/edit/lib/Drupal/edit/EditController.php +++ b/core/modules/edit/lib/Drupal/edit/EditController.php @@ -153,14 +153,15 @@ public function metadata(Request $request) { throw new NotFoundHttpException(); } + $entity = $entity->getTranslation($langcode); + // If the entity information for this field is requested, include it. $entity_id = $entity->entityType() . '/' . $entity_id; if (is_array($entities) && in_array($entity_id, $entities) && !isset($metadata[$entity_id])) { - $metadata[$entity_id] = $this->metadataGenerator->generateEntity($entity, $langcode); + $metadata[$entity_id] = $this->metadataGenerator->generateEntityMetadata($entity); } - $field_definition = $entity->get($field_name)->getFieldDefinition(); - $metadata[$field] = $this->metadataGenerator->generateField($entity, $field_definition, $langcode, $view_mode); + $metadata[$field] = $this->metadataGenerator->generateFieldMetadata($entity->get($field_name), $view_mode); } return new JsonResponse($metadata); diff --git a/core/modules/edit/lib/Drupal/edit/EditPluginInterface.php b/core/modules/edit/lib/Drupal/edit/EditPluginInterface.php index 0228c2f..88716d6 100644 --- a/core/modules/edit/lib/Drupal/edit/EditPluginInterface.php +++ b/core/modules/edit/lib/Drupal/edit/EditPluginInterface.php @@ -8,7 +8,7 @@ namespace Drupal\edit; use Drupal\Component\Plugin\PluginInspectionInterface; -use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldItemListInterface; /** * Defines an interface for in-place editors plugins. @@ -16,17 +16,15 @@ interface EditPluginInterface extends PluginInspectionInterface { /** - * Checks whether this editor is compatible with a given field instance. + * Checks whether this editor is compatible with a given field. * - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The field definition of the field being edited. - * @param array $items - * The field's item values. + * @param \Drupal\Core\Field\FieldItemListInterface $field + * The field being edited. * * @return bool * TRUE if it is compatible, FALSE otherwise. */ - public function isCompatible(FieldDefinitionInterface $field_definition, array $items); + public function isCompatible(FieldItemListInterface $field); /** * Generates metadata that is needed specifically for this editor. @@ -34,16 +32,14 @@ public function isCompatible(FieldDefinitionInterface $field_definition, array $ * Will only be called by \Drupal\edit\MetadataGeneratorInterface::generate() * when the passed in field instance & item values will use this editor. * - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The field definition of the field being edited. - * @param array $items - * The field's item values. + * @param \Drupal\Core\Field\FieldItemListInterface $field + * The field being edited. * * @return array * A keyed array with metadata. Each key should be prefixed with the plugin * ID of the editor. */ - public function getMetadata(FieldDefinitionInterface $field_definition, array $items); + public function getMetadata(FieldItemListInterface $field); /** * Returns the attachments for this editor. diff --git a/core/modules/edit/lib/Drupal/edit/EditorBase.php b/core/modules/edit/lib/Drupal/edit/EditorBase.php index ca8dab6..84e8c64 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorBase.php +++ b/core/modules/edit/lib/Drupal/edit/EditorBase.php @@ -9,7 +9,7 @@ use Drupal\Core\Plugin\PluginBase; use Drupal\edit\EditPluginInterface; -use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldItemListInterface; /** * Defines a base editor implementation. @@ -19,7 +19,7 @@ /** * {@inheritdoc} */ - function getMetadata(FieldDefinitionInterface $field_definition, array $items) { + function getMetadata(FieldItemListInterface $field) { return array(); } diff --git a/core/modules/edit/lib/Drupal/edit/EditorSelector.php b/core/modules/edit/lib/Drupal/edit/EditorSelector.php index e21ef1a..00402f1 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorSelector.php +++ b/core/modules/edit/lib/Drupal/edit/EditorSelector.php @@ -9,7 +9,7 @@ use Drupal\Component\Plugin\PluginManagerInterface; use Drupal\Component\Utility\NestedArray; -use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldItemListInterface;; use Drupal\Core\Field\FormatterPluginManager; /** @@ -54,7 +54,7 @@ public function __construct(PluginManagerInterface $editor_manager, FormatterPlu /** * {@inheritdoc} */ - public function getEditor($formatter_type, FieldDefinitionInterface $field_definition, array $items) { + public function getEditor($formatter_type, FieldItemListInterface $field) { // Build a static cache of the editors that have registered themselves as // alternatives to a certain editor. if (!isset($this->alternatives)) { @@ -91,7 +91,7 @@ public function getEditor($formatter_type, FieldDefinitionInterface $field_defin // Make a choice. foreach ($editor_choices as $editor_id) { $editor = $this->editorManager->createInstance($editor_id); - if ($editor->isCompatible($field_definition, $items)) { + if ($editor->isCompatible($field)) { return $editor_id; } } diff --git a/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php b/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php index 28fb6d2..d9437a8 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php +++ b/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php @@ -7,7 +7,7 @@ namespace Drupal\edit; -use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldItemListInterface;; /** * Interface for selecting an in-place editor (an Editor plugin) for a field. @@ -19,15 +19,13 @@ * * @param string $formatter_type * The field's formatter type name. - * @param \Drupal\Core\Field\FieldDefinitionInterface $instance - * The field definition. - * @param array $items - * The field's item values. + * @param \Drupal\Core\Field\FieldItemListInterface $field + * The field. * * @return string|null * The editor to use, or NULL to not enable in-place editing. */ - public function getEditor($formatter_type, FieldDefinitionInterface $instance, array $items); + public function getEditor($formatter_type, FieldItemListInterface $field); /** * Returns the attachments for all editors. diff --git a/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php b/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php index 4f18ed4..38a73fc 100644 --- a/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php +++ b/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php @@ -7,9 +7,9 @@ namespace Drupal\edit; -use Drupal\Core\Entity\EntityInterface; use Drupal\Component\Plugin\PluginManagerInterface; -use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Field\FieldItemListInterface; use Drupal\edit\Access\EditEntityFieldAccessCheckInterface; use Drupal\field\FieldInstanceInterface; @@ -58,17 +58,18 @@ public function __construct(EditEntityFieldAccessCheckInterface $access_checker, /** * {@inheritdoc} */ - public function generateEntity(EntityInterface $entity, $langcode) { + public function generateEntityMetadata(EntityInterface $entity) { return array( - 'label' => $entity->label($langcode), + 'label' => $entity->label(), ); } /** * {@inheritdoc} */ - public function generateField(EntityInterface $entity, FieldDefinitionInterface $field_definition, $langcode, $view_mode) { - $field_name = $field_definition->getName(); + public function generateFieldMetadata(FieldItemListInterface $field, $view_mode) { + $entity = $field->getEntity(); + $field_name = $field->getFieldDefinition()->getName(); // Early-return if user does not have access. $access = $this->accessChecker->accessEditEntityField($entity, $field_name); @@ -78,14 +79,13 @@ public function generateField(EntityInterface $entity, FieldDefinitionInterface // Early-return if no editor is available. $formatter_id = entity_get_render_display($entity, $view_mode)->getRenderer($field_name)->getPluginId(); - $items = $entity->getTranslation($langcode)->get($field_name)->getValue(); - $editor_id = $this->editorSelector->getEditor($formatter_id, $field_definition, $items); + $editor_id = $this->editorSelector->getEditor($formatter_id, $field); if (!isset($editor_id)) { return array('access' => FALSE); } // Gather metadata, allow the editor to add additional metadata of its own. - $label = $field_definition->getLabel(); + $label = $field->getFieldDefinition()->getLabel(); $editor = $this->editorManager->createInstance($editor_id); $metadata = array( 'label' => check_plain($label), @@ -93,7 +93,7 @@ public function generateField(EntityInterface $entity, FieldDefinitionInterface 'editor' => $editor_id, 'aria' => t('Entity @type @id, field @field', array('@type' => $entity->entityType(), '@id' => $entity->id(), '@field' => $label)), ); - $custom_metadata = $editor->getMetadata($field_definition, $items); + $custom_metadata = $editor->getMetadata($field); if (count($custom_metadata)) { $metadata['custom'] = $custom_metadata; } diff --git a/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php b/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php index 6cde4dd..43d61b4 100644 --- a/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php +++ b/core/modules/edit/lib/Drupal/edit/MetadataGeneratorInterface.php @@ -8,7 +8,7 @@ namespace Drupal\edit; use Drupal\Core\Entity\EntityInterface; -use Drupal\Core\Field\FieldDefinitionInterface; +use Drupal\Core\Field\FieldItemListInterface; /** * Interface for generating in-place editing metadata. @@ -19,24 +19,18 @@ * Generates in-place editing metadata for an entity. * * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity being edited. - * @param string $langcode - * The name of the language for which the field is being edited. + * The entity, in the language in which one of its fields is being edited. * @return array * An array containing metadata with the following keys: * - label: the user-visible label for the entity in the given language. */ - public function generateEntity(EntityInterface $entity, $langcode); + public function generateEntityMetadata(EntityInterface $entity); /** * Generates in-place editing metadata for an entity field. * - * @param \Drupal\Core\Entity\EntityInterface $entity - * The entity being edited. - * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition - * The field definition of the field being edited. - * @param string $langcode - * The name of the language for which the field is being edited. + * @param \Drupal\Core\Field\FieldItemListInterface $field + * The field being edited. * @param string $view_mode * The view mode the field should be rerendered in. * @return array @@ -47,6 +41,6 @@ public function generateEntity(EntityInterface $entity, $langcode); * - aria: the ARIA label. * - custom: (optional) any additional metadata that the editor provides. */ - public function generateField(EntityInterface $entity, FieldDefinitionInterface $field_definition, $langcode, $view_mode); + public function generateFieldMetadata(FieldItemListInterface $field, $view_mode); } diff --git a/core/modules/edit/lib/Drupal/edit/Plugin/InPlaceEditor/FormEditor.php b/core/modules/edit/lib/Drupal/edit/Plugin/InPlaceEditor/FormEditor.php index 1f969a6..662c81f 100644 --- a/core/modules/edit/lib/Drupal/edit/Plugin/InPlaceEditor/FormEditor.php +++ b/core/modules/edit/lib/Drupal/edit/Plugin/InPlaceEditor/FormEditor.php @@ -7,8 +7,8 @@ namespace Drupal\edit\Plugin\InPlaceEditor; +use Drupal\Core\Field\FieldItemListInterface; use Drupal\edit\EditorBase; -use Drupal\Core\Field\FieldDefinitionInterface; /** * Defines the form in-place editor. @@ -22,7 +22,7 @@ class FormEditor extends EditorBase { /** * {@inheritdoc} */ - function isCompatible(FieldDefinitionInterface $field_definition, array $items) { + public function isCompatible(FieldItemListInterface $field) { return TRUE; } diff --git a/core/modules/edit/lib/Drupal/edit/Plugin/InPlaceEditor/PlainTextEditor.php b/core/modules/edit/lib/Drupal/edit/Plugin/InPlaceEditor/PlainTextEditor.php index c17e304..c25b11a 100644 --- a/core/modules/edit/lib/Drupal/edit/Plugin/InPlaceEditor/PlainTextEditor.php +++ b/core/modules/edit/lib/Drupal/edit/Plugin/InPlaceEditor/PlainTextEditor.php @@ -7,8 +7,8 @@ namespace Drupal\edit\Plugin\InPlaceEditor; +use Drupal\Core\Field\FieldItemListInterface; use Drupal\edit\EditorBase; -use Drupal\Core\Field\FieldDefinitionInterface; /** * Defines the plain text in-place editor. @@ -25,7 +25,9 @@ class PlainTextEditor extends EditorBase { * @todo The processed text logic is too coupled to text fields. Figure out * how to generalize to other textual field types. */ - function isCompatible(FieldDefinitionInterface $field_definition, array $items) { + public function isCompatible(FieldItemListInterface $field) { + $field_definition = $field->getFieldDefinition(); + // This editor is incompatible with multivalued fields. if ($field_definition->getCardinality() != 1) { return FALSE; diff --git a/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php b/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php index 8c290ad..768a8fc 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php @@ -7,6 +7,7 @@ namespace Drupal\edit\Tests; +use Drupal\Core\Language\Language; use Drupal\edit\Plugin\InPlaceEditorManager; use Drupal\edit\EditorSelector; @@ -45,13 +46,13 @@ protected function setUp() { } /** - * Retrieves the FieldInstance object for the given field and returns the - * editor that Edit selects. + * Returns the in-place editor that Edit selects. */ - protected function getSelectedEditor($items, $field_name, $view_mode = 'default') { + protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') { + $entity = entity_load('entity_test', $entity_id, TRUE); + $field = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name); $options = entity_get_display('entity_test', 'entity_test', $view_mode)->getComponent($field_name); - $field_instance = field_info_instance('entity_test', $field_name, 'entity_test'); - return $this->editorSelector->getEditor($options['type'], $field_instance, $items); + return $this->editorSelector->getEditor($options['type'], $field); } /** @@ -72,32 +73,34 @@ public function testText() { array() ); - // Pretend there is an entity with these items for the field. - $items = array(array('value' => 'Hello, world!', 'format' => 'full_html')); + // Create an entity with values for this text field. + $this->entity = entity_create('entity_test', array()); + $this->entity->{$field_name}->value = 'Hello, world!'; + $this->entity->{$field_name}->format = 'full_html'; + $this->entity->save(); // Editor selection without text processing, with cardinality 1. - $this->assertEqual('plain_text', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality 1, the 'plain_text' editor is selected."); + $this->assertEqual('plain_text', $this->getSelectedEditor($this->entity->id(), $field_name), "Without text processing, cardinality 1, the 'plain_text' editor is selected."); // Editor selection with text processing, cardinality 1. $this->field_text_instance->settings['text_processing'] = 1; $this->field_text_instance->save(); - $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality 1, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With text processing, cardinality 1, the 'form' editor is selected."); // Editor selection without text processing, cardinality 1 (again). $this->field_text_instance->settings['text_processing'] = 0; $this->field_text_instance->save(); - $this->assertEqual('plain_text', $this->getSelectedEditor($items, $field_name), "Without text processing again, cardinality 1, the 'plain_text' editor is selected."); + $this->assertEqual('plain_text', $this->getSelectedEditor($this->entity->id(), $field_name), "Without text processing again, cardinality 1, the 'plain_text' editor is selected."); // Editor selection without text processing, cardinality >1 $this->field_text_field->cardinality = 2; $this->field_text_field->save(); - $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html'); - $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality >1, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "Without text processing, cardinality >1, the 'form' editor is selected."); // Editor selection with text processing, cardinality >1 $this->field_text_instance->settings['text_processing'] = 1; $this->field_text_instance->save(); - $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With text processing, cardinality >1, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With text processing, cardinality >1, the 'form' editor is selected."); } /** @@ -122,21 +125,24 @@ public function testTextWysiwyg() { array() ); - // Pretend there is an entity with these items for the field. - $items = array(array('value' => 'Hello, world!', 'format' => 'filtered_html')); + // Create an entity with values for this text field. + $this->entity = entity_create('entity_test', array()); + $this->entity->{$field_name}->value = 'Hello, world!'; + $this->entity->{$field_name}->format = 'filtered_html'; + $this->entity->save(); // Editor selection w/ cardinality 1, text format w/o associated text editor. - $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected."); // Editor selection w/ cardinality 1, text format w/ associated text editor. - $items[0]['format'] = 'full_html'; - $this->assertEqual('wysiwyg', $this->getSelectedEditor($items, $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected."); + $this->entity->{$field_name}->format = 'full_html'; + $this->entity->save(); + $this->assertEqual('wysiwyg', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality 1, and the full_html text format, the 'wysiwyg' editor is selected."); // Editor selection with text processing, cardinality >1 $this->field_textarea_field->cardinality = 2; $this->field_textarea_field->save(); - $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html'); - $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); } /** @@ -156,16 +162,18 @@ public function testNumber() { array() ); - // Pretend there is an entity with these items for the field. - $items = array(42, 43); + // Create an entity with values for this text field. + $this->entity = entity_create('entity_test', array()); + $this->entity->{$field_name}->value = 42; + $this->entity->save(); // Editor selection with cardinality 1. - $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality 1, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality 1, the 'form' editor is selected."); // Editor selection with cardinality >1. $this->field_nr_field->cardinality = 2; $this->field_nr_field->save(); - $this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "With cardinality >1, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "With cardinality >1, the 'form' editor is selected."); } } diff --git a/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php b/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php index c7f82e0..d443656 100644 --- a/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php +++ b/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php @@ -96,15 +96,14 @@ public function testSimpleEntityType() { // Create an entity with values for this text field. $this->entity = entity_create('entity_test', array()); - $this->is_new = TRUE; $this->entity->{$field_1_name}->value = 'Test'; $this->entity->{$field_2_name}->value = 42; $this->entity->save(); $entity = entity_load('entity_test', $this->entity->id()); // Verify metadata for field 1. - $instance_1 = field_info_instance($entity->entityType(), $field_1_name, $entity->bundle()); - $metadata_1 = $this->metadataGenerator->generateField($entity, $instance_1, Language::LANGCODE_NOT_SPECIFIED, 'default'); + $field_1 = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_1_name); + $metadata_1 = $this->metadataGenerator->generateFieldMetadata($field_1, 'default'); $expected_1 = array( 'access' => TRUE, 'label' => 'Simple text field', @@ -114,8 +113,8 @@ public function testSimpleEntityType() { $this->assertEqual($expected_1, $metadata_1, 'The correct metadata is generated for the first field.'); // Verify metadata for field 2. - $instance_2 = field_info_instance($entity->entityType(), $field_2_name, $entity->bundle()); - $metadata_2 = $this->metadataGenerator->generateField($entity, $instance_2, Language::LANGCODE_NOT_SPECIFIED, 'default'); + $field_2 = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_2_name); + $metadata_2 = $this->metadataGenerator->generateFieldMetadata($field_2, 'default'); $expected_2 = array( 'access' => TRUE, 'label' => 'Simple number field', @@ -169,8 +168,8 @@ public function testEditorWithCustomMetadata() { $entity = entity_load('entity_test', $this->entity->id()); // Verify metadata. - $instance = field_info_instance($entity->entityType(), $field_name, $entity->bundle()); - $metadata = $this->metadataGenerator->generateField($entity, $instance, Language::LANGCODE_NOT_SPECIFIED, 'default'); + $field = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name); + $metadata = $this->metadataGenerator->generateFieldMetadata($field, 'default'); $expected = array( 'access' => TRUE, 'label' => 'Rich text field', diff --git a/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/InPlaceEditor/WysiwygEditor.php b/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/InPlaceEditor/WysiwygEditor.php index b286d66..1dc623b 100644 --- a/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/InPlaceEditor/WysiwygEditor.php +++ b/core/modules/edit/tests/modules/lib/Drupal/edit_test/Plugin/InPlaceEditor/WysiwygEditor.php @@ -7,8 +7,8 @@ namespace Drupal\edit_test\Plugin\InPlaceEditor; +use Drupal\Core\Field\FieldItemListInterface; use Drupal\edit\EditorBase; -use Drupal\Core\Field\FieldDefinitionInterface; /** * Defines the 'wysiwyg' in-place editor. @@ -23,7 +23,9 @@ class WysiwygEditor extends EditorBase { /** * {@inheritdoc} */ - function isCompatible(FieldDefinitionInterface $field_definition, array $items) { + public function isCompatible(FieldItemListInterface $field) { + $field_definition = $field->getFieldDefinition(); + // This editor is incompatible with multivalued fields. if ($field_definition->getCardinality() != 1) { return FALSE; @@ -32,7 +34,7 @@ function isCompatible(FieldDefinitionInterface $field_definition, array $items) // if there is a currently active text format and that text format is the // 'full_html' text format. elseif ($field_definition->getSetting('text_processing')) { - $format_id = $items[0]['format']; + $format_id = $field[0]->get('format')->getValue(); if (isset($format_id) && $format_id === 'full_html') { return TRUE; } @@ -43,8 +45,8 @@ function isCompatible(FieldDefinitionInterface $field_definition, array $items) /** * {@inheritdoc} */ - function getMetadata(FieldDefinitionInterface $field_definition, array $items) { - $format_id = $items[0]['format']; + function getMetadata(FieldItemListInterface $field) { + $format_id = $field[0]->get('format')->getValue(); $metadata['format'] = $format_id; return $metadata; } diff --git a/core/modules/editor/lib/Drupal/editor/Plugin/InPlaceEditor/Editor.php b/core/modules/editor/lib/Drupal/editor/Plugin/InPlaceEditor/Editor.php index 2138b57..029efc2 100644 --- a/core/modules/editor/lib/Drupal/editor/Plugin/InPlaceEditor/Editor.php +++ b/core/modules/editor/lib/Drupal/editor/Plugin/InPlaceEditor/Editor.php @@ -8,8 +8,8 @@ namespace Drupal\editor\Plugin\InPlaceEditor; use Drupal\Component\Plugin\PluginBase; +use Drupal\Core\Field\FieldItemListInterface; use Drupal\edit\EditPluginInterface; -use Drupal\Core\Field\FieldDefinitionInterface; /** * Defines the formatted text in-place editor. @@ -24,7 +24,9 @@ class Editor extends PluginBase implements EditPluginInterface { /** * {@inheritdoc} */ - function isCompatible(FieldDefinitionInterface $field_definition, array $items) { + public function isCompatible(FieldItemListInterface $field) { + $field_definition = $field->getFieldDefinition(); + // This editor is incompatible with multivalued fields. if ($field_definition->getCardinality() != 1) { return FALSE; @@ -33,7 +35,7 @@ function isCompatible(FieldDefinitionInterface $field_definition, array $items) // if there is a currently active text format, that text format has an // associated editor and that editor supports inline editing. elseif ($field_definition->getSetting('text_processing')) { - $format_id = $items[0]['format']; + $format_id = $field[0]->get('format')->getValue(); if (isset($format_id) && $editor = editor_load($format_id)) { $definition = \Drupal::service('plugin.manager.editor')->getDefinition($editor->editor); if ($definition['supports_inline_editing'] === TRUE) { @@ -48,8 +50,8 @@ function isCompatible(FieldDefinitionInterface $field_definition, array $items) /** * {@inheritdoc} */ - function getMetadata(FieldDefinitionInterface $field_definition, array $items) { - $format_id = $items[0]['format']; + function getMetadata(FieldItemListInterface $field) { + $format_id = $field[0]->get('format')->getValue(); $metadata['format'] = $format_id; $metadata['formatHasTransformations'] = $this->textFormatHasTransformationFilters($format_id); return $metadata; diff --git a/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php b/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php index 9081290..6a3a216 100644 --- a/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php +++ b/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php @@ -106,13 +106,13 @@ public function setUp() { } /** - * Retrieves the FieldInstance object for the given field and returns the - * editor that Edit selects. + * Returns the in-place editor that Edit selects. */ - protected function getSelectedEditor($items, $field_name, $view_mode = 'default') { + protected function getSelectedEditor($entity_id, $field_name, $view_mode = 'default') { + $entity = entity_load('entity_test', $entity_id, TRUE); + $field = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name); $options = entity_get_display('entity_test', 'entity_test', $view_mode)->getComponent($field_name); - $field_instance = field_info_instance('entity_test', $field_name, 'entity_test'); - return $this->editorSelector->getEditor($options['type'], $field_instance, $items); + return $this->editorSelector->getEditor($options['type'], $field); } /** @@ -126,21 +126,24 @@ public function testEditorSelection() { $this->editorManager = new InPlaceEditorManager($this->container->get('container.namespaces')); $this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter')); - // Pretend there is an entity with these items for the field. - $items = array(array('value' => 'Hello, world!', 'format' => 'filtered_html')); + // Create an entity with values for this text field. + $this->entity = entity_create('entity_test', array()); + $this->entity->{$this->field_name}->value = 'Hello, world!'; + $this->entity->{$this->field_name}->format = 'filtered_html'; + $this->entity->save(); // Editor selection w/ cardinality 1, text format w/o associated text editor. - $this->assertEqual('form', $this->getSelectedEditor($items, $this->field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $this->field_name), "With cardinality 1, and the filtered_html text format, the 'form' editor is selected."); // Editor selection w/ cardinality 1, text format w/ associated text editor. - $items[0]['format'] = 'full_html'; - $this->assertEqual('editor', $this->getSelectedEditor($items, $this->field_name), "With cardinality 1, and the full_html text format, the 'editor' editor is selected."); + $this->entity->{$this->field_name}->format = 'full_html'; + $this->entity->save(); + $this->assertEqual('editor', $this->getSelectedEditor($this->entity->id(), $this->field_name), "With cardinality 1, and the full_html text format, the 'editor' editor is selected."); // Editor selection with text processing, cardinality >1 $this->field_textarea_field->cardinality = 2; $this->field_textarea_field->save(); - $items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html'); - $this->assertEqual('form', $this->getSelectedEditor($items, $this->field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); + $this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $this->field_name), "With cardinality >1, and both items using the full_html text format, the 'form' editor is selected."); } /** @@ -160,8 +163,8 @@ public function testMetadata() { $entity = entity_load('entity_test', $this->entity->id()); // Verify metadata. - $instance = field_info_instance($entity->entityType(), $this->field_name, $entity->bundle()); - $metadata = $this->metadataGenerator->generateField($entity, $instance, Language::LANGCODE_NOT_SPECIFIED, 'default'); + $field = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($this->field_name); + $metadata = $this->metadataGenerator->generateFieldMetadata($field, 'default'); $expected = array( 'access' => TRUE, 'label' => 'Long text field',