diff --git a/core/modules/edit/lib/Drupal/edit/EditController.php b/core/modules/edit/lib/Drupal/edit/EditController.php index 1e62ab6c379..45c63ac8231 100644 --- a/core/modules/edit/lib/Drupal/edit/EditController.php +++ b/core/modules/edit/lib/Drupal/edit/EditController.php @@ -161,14 +161,15 @@ class EditController implements ContainerInjectionInterface { 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 0228c2fbf65..59cac9a5a30 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 @@ use Drupal\Core\Field\FieldDefinitionInterface; interface EditPluginInterface extends PluginInspectionInterface { /** - * Checks whether this editor is compatible with a given field instance. + * Checks whether this in-place 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 $items + * The field values to be in-place edited. * * @return bool * TRUE if it is compatible, FALSE otherwise. */ - public function isCompatible(FieldDefinitionInterface $field_definition, array $items); + public function isCompatible(FieldItemListInterface $items); /** * Generates metadata that is needed specifically for this editor. @@ -34,16 +32,14 @@ interface EditPluginInterface extends PluginInspectionInterface { * 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 $items + * The field values to be in-place 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 $items); /** * 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 ca8dab6b7ee..20313d5359a 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorBase.php +++ b/core/modules/edit/lib/Drupal/edit/EditorBase.php @@ -9,7 +9,7 @@ namespace Drupal\edit; 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 @@ abstract class EditorBase extends PluginBase implements EditPluginInterface { /** * {@inheritdoc} */ - function getMetadata(FieldDefinitionInterface $field_definition, array $items) { + function getMetadata(FieldItemListInterface $items) { return array(); } diff --git a/core/modules/edit/lib/Drupal/edit/EditorSelector.php b/core/modules/edit/lib/Drupal/edit/EditorSelector.php index e21ef1ad6bf..9b4b9f972c2 100644 --- a/core/modules/edit/lib/Drupal/edit/EditorSelector.php +++ b/core/modules/edit/lib/Drupal/edit/EditorSelector.php @@ -9,7 +9,7 @@ namespace Drupal\edit; 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 @@ class EditorSelector implements EditorSelectorInterface { /** * {@inheritdoc} */ - public function getEditor($formatter_type, FieldDefinitionInterface $field_definition, array $items) { + public function getEditor($formatter_type, FieldItemListInterface $items) { // 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 @@ class EditorSelector implements EditorSelectorInterface { // Make a choice. foreach ($editor_choices as $editor_id) { $editor = $this->editorManager->createInstance($editor_id); - if ($editor->isCompatible($field_definition, $items)) { + if ($editor->isCompatible($items)) { return $editor_id; } } diff --git a/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php b/core/modules/edit/lib/Drupal/edit/EditorSelectorInterface.php index 28fb6d2cb98..c9f249e1d4c 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 @@ interface EditorSelectorInterface { * * @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 $items + * The field values to be in-place edited. * * @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 $items); /** * 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 4f18ed4cb9f..eac10973312 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 @@ class MetadataGenerator implements MetadataGeneratorInterface { /** * {@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 $items, $view_mode) { + $entity = $items->getEntity(); + $field_name = $items->getFieldDefinition()->getName(); // Early-return if user does not have access. $access = $this->accessChecker->accessEditEntityField($entity, $field_name); @@ -78,14 +79,13 @@ class MetadataGenerator implements MetadataGeneratorInterface { // 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, $items); 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 = $items->getFieldDefinition()->getLabel(); $editor = $this->editorManager->createInstance($editor_id); $metadata = array( 'label' => check_plain($label), @@ -93,7 +93,7 @@ class MetadataGenerator implements MetadataGeneratorInterface { '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($items); 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 6cde4dde9dd..b3a38333eaa 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 @@ interface MetadataGeneratorInterface { * 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 $items + * The field values to be in-place edited. * @param string $view_mode * The view mode the field should be rerendered in. * @return array @@ -47,6 +41,6 @@ interface MetadataGeneratorInterface { * - 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 $items, $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 1f969a693b6..756cf24362f 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 $items) { 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 c17e30409d5..a36b2ce2373 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 $items) { + $field_definition = $items->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 8c290ad6a17..7a12d54d157 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 @@ class EditorSelectionTest extends EditTestBase { } /** - * 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); + $items = $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'], $items); } /** @@ -72,32 +73,34 @@ class EditorSelectionTest extends EditTestBase { 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 @@ class EditorSelectionTest extends EditTestBase { 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 @@ class EditorSelectionTest extends EditTestBase { 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 c7f82e076c0..3b7f7f6057e 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 @@ class MetadataGeneratorTest extends EditTestBase { // 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'); + $items_1 = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_1_name); + $metadata_1 = $this->metadataGenerator->generateFieldMetadata($items_1, 'default'); $expected_1 = array( 'access' => TRUE, 'label' => 'Simple text field', @@ -114,8 +113,8 @@ class MetadataGeneratorTest extends EditTestBase { $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'); + $items_2 = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_2_name); + $metadata_2 = $this->metadataGenerator->generateFieldMetadata($items_2, 'default'); $expected_2 = array( 'access' => TRUE, 'label' => 'Simple number field', @@ -169,8 +168,8 @@ class MetadataGeneratorTest extends EditTestBase { $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'); + $items = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name); + $metadata = $this->metadataGenerator->generateFieldMetadata($items, '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 b286d66f5a2..d8dc40e3bc8 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 $items) { + $field_definition = $items->getFieldDefinition(); + // This editor is incompatible with multivalued fields. if ($field_definition->getCardinality() != 1) { return FALSE; @@ -32,8 +34,7 @@ class WysiwygEditor extends EditorBase { // 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']; - if (isset($format_id) && $format_id === 'full_html') { + if ($items[0]->format === 'full_html') { return TRUE; } return FALSE; @@ -43,9 +44,8 @@ class WysiwygEditor extends EditorBase { /** * {@inheritdoc} */ - function getMetadata(FieldDefinitionInterface $field_definition, array $items) { - $format_id = $items[0]['format']; - $metadata['format'] = $format_id; + function getMetadata(FieldItemListInterface $items) { + $metadata['format'] = $items[0]->format; 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 2138b578b8e..b3ba3062d97 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 $items) { + $field_definition = $items->getFieldDefinition(); + // This editor is incompatible with multivalued fields. if ($field_definition->getCardinality() != 1) { return FALSE; @@ -33,8 +35,7 @@ class Editor extends PluginBase implements EditPluginInterface { // 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']; - if (isset($format_id) && $editor = editor_load($format_id)) { + if ($editor = editor_load($items[0]->format)) { $definition = \Drupal::service('plugin.manager.editor')->getDefinition($editor->editor); if ($definition['supports_inline_editing'] === TRUE) { return TRUE; @@ -48,8 +49,8 @@ class Editor extends PluginBase implements EditPluginInterface { /** * {@inheritdoc} */ - function getMetadata(FieldDefinitionInterface $field_definition, array $items) { - $format_id = $items[0]['format']; + function getMetadata(FieldItemListInterface $items) { + $format_id = $items[0]->format; $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 908129057fb..016a0ecf4bb 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 @@ class EditIntegrationTest extends EditTestBase { } /** - * 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); + $items = $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'], $items); } /** @@ -126,21 +126,24 @@ class EditIntegrationTest extends EditTestBase { $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 @@ class EditIntegrationTest extends EditTestBase { $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'); + $items = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($this->field_name); + $metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default'); $expected = array( 'access' => TRUE, 'label' => 'Long text field',