Issue #2144879 by Wim Leers, yched: Brush up MetadataGeneratorInterface::generate(Field|Entity)(): use FieldItemListInterface + better naming.
parent
09d4cc9356
commit
0959ae04a5
|
@ -161,14 +161,15 @@ class EditController implements ContainerInjectionInterface {
|
||||||
throw new NotFoundHttpException();
|
throw new NotFoundHttpException();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$entity = $entity->getTranslation($langcode);
|
||||||
|
|
||||||
// If the entity information for this field is requested, include it.
|
// If the entity information for this field is requested, include it.
|
||||||
$entity_id = $entity->entityType() . '/' . $entity_id;
|
$entity_id = $entity->entityType() . '/' . $entity_id;
|
||||||
if (is_array($entities) && in_array($entity_id, $entities) && !isset($metadata[$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->generateFieldMetadata($entity->get($field_name), $view_mode);
|
||||||
$metadata[$field] = $this->metadataGenerator->generateField($entity, $field_definition, $langcode, $view_mode);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new JsonResponse($metadata);
|
return new JsonResponse($metadata);
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
namespace Drupal\edit;
|
namespace Drupal\edit;
|
||||||
|
|
||||||
use Drupal\Component\Plugin\PluginInspectionInterface;
|
use Drupal\Component\Plugin\PluginInspectionInterface;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
use Drupal\Core\Field\FieldItemListInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines an interface for in-place editors plugins.
|
* Defines an interface for in-place editors plugins.
|
||||||
|
@ -16,17 +16,15 @@ use Drupal\Core\Field\FieldDefinitionInterface;
|
||||||
interface EditPluginInterface extends PluginInspectionInterface {
|
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
|
* @param \Drupal\Core\Field\FieldItemListInterface $items
|
||||||
* The field definition of the field being edited.
|
* The field values to be in-place edited.
|
||||||
* @param array $items
|
|
||||||
* The field's item values.
|
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
* TRUE if it is compatible, FALSE otherwise.
|
* 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.
|
* 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()
|
* Will only be called by \Drupal\edit\MetadataGeneratorInterface::generate()
|
||||||
* when the passed in field instance & item values will use this editor.
|
* when the passed in field instance & item values will use this editor.
|
||||||
*
|
*
|
||||||
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
|
* @param \Drupal\Core\Field\FieldItemListInterface $items
|
||||||
* The field definition of the field being edited.
|
* The field values to be in-place edited.
|
||||||
* @param array $items
|
|
||||||
* The field's item values.
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
* A keyed array with metadata. Each key should be prefixed with the plugin
|
* A keyed array with metadata. Each key should be prefixed with the plugin
|
||||||
* ID of the editor.
|
* ID of the editor.
|
||||||
*/
|
*/
|
||||||
public function getMetadata(FieldDefinitionInterface $field_definition, array $items);
|
public function getMetadata(FieldItemListInterface $items);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the attachments for this editor.
|
* Returns the attachments for this editor.
|
||||||
|
|
|
@ -9,7 +9,7 @@ namespace Drupal\edit;
|
||||||
|
|
||||||
use Drupal\Core\Plugin\PluginBase;
|
use Drupal\Core\Plugin\PluginBase;
|
||||||
use Drupal\edit\EditPluginInterface;
|
use Drupal\edit\EditPluginInterface;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
use Drupal\Core\Field\FieldItemListInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines a base editor implementation.
|
* Defines a base editor implementation.
|
||||||
|
@ -19,7 +19,7 @@ abstract class EditorBase extends PluginBase implements EditPluginInterface {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function getMetadata(FieldDefinitionInterface $field_definition, array $items) {
|
function getMetadata(FieldItemListInterface $items) {
|
||||||
return array();
|
return array();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@ namespace Drupal\edit;
|
||||||
|
|
||||||
use Drupal\Component\Plugin\PluginManagerInterface;
|
use Drupal\Component\Plugin\PluginManagerInterface;
|
||||||
use Drupal\Component\Utility\NestedArray;
|
use Drupal\Component\Utility\NestedArray;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
use Drupal\Core\Field\FieldItemListInterface;
|
||||||
use Drupal\Core\Field\FormatterPluginManager;
|
use Drupal\Core\Field\FormatterPluginManager;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -54,7 +54,7 @@ class EditorSelector implements EditorSelectorInterface {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@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
|
// Build a static cache of the editors that have registered themselves as
|
||||||
// alternatives to a certain editor.
|
// alternatives to a certain editor.
|
||||||
if (!isset($this->alternatives)) {
|
if (!isset($this->alternatives)) {
|
||||||
|
@ -91,7 +91,7 @@ class EditorSelector implements EditorSelectorInterface {
|
||||||
// Make a choice.
|
// Make a choice.
|
||||||
foreach ($editor_choices as $editor_id) {
|
foreach ($editor_choices as $editor_id) {
|
||||||
$editor = $this->editorManager->createInstance($editor_id);
|
$editor = $this->editorManager->createInstance($editor_id);
|
||||||
if ($editor->isCompatible($field_definition, $items)) {
|
if ($editor->isCompatible($items)) {
|
||||||
return $editor_id;
|
return $editor_id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
|
|
||||||
namespace Drupal\edit;
|
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.
|
* Interface for selecting an in-place editor (an Editor plugin) for a field.
|
||||||
|
@ -19,15 +19,13 @@ interface EditorSelectorInterface {
|
||||||
*
|
*
|
||||||
* @param string $formatter_type
|
* @param string $formatter_type
|
||||||
* The field's formatter type name.
|
* The field's formatter type name.
|
||||||
* @param \Drupal\Core\Field\FieldDefinitionInterface $instance
|
* @param \Drupal\Core\Field\FieldItemListInterface $items
|
||||||
* The field definition.
|
* The field values to be in-place edited.
|
||||||
* @param array $items
|
|
||||||
* The field's item values.
|
|
||||||
*
|
*
|
||||||
* @return string|null
|
* @return string|null
|
||||||
* The editor to use, or NULL to not enable in-place editing.
|
* 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.
|
* Returns the attachments for all editors.
|
||||||
|
|
|
@ -7,9 +7,9 @@
|
||||||
|
|
||||||
namespace Drupal\edit;
|
namespace Drupal\edit;
|
||||||
|
|
||||||
use Drupal\Core\Entity\EntityInterface;
|
|
||||||
use Drupal\Component\Plugin\PluginManagerInterface;
|
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\edit\Access\EditEntityFieldAccessCheckInterface;
|
||||||
use Drupal\field\FieldInstanceInterface;
|
use Drupal\field\FieldInstanceInterface;
|
||||||
|
|
||||||
|
@ -58,17 +58,18 @@ class MetadataGenerator implements MetadataGeneratorInterface {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function generateEntity(EntityInterface $entity, $langcode) {
|
public function generateEntityMetadata(EntityInterface $entity) {
|
||||||
return array(
|
return array(
|
||||||
'label' => $entity->label($langcode),
|
'label' => $entity->label(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function generateField(EntityInterface $entity, FieldDefinitionInterface $field_definition, $langcode, $view_mode) {
|
public function generateFieldMetadata(FieldItemListInterface $items, $view_mode) {
|
||||||
$field_name = $field_definition->getName();
|
$entity = $items->getEntity();
|
||||||
|
$field_name = $items->getFieldDefinition()->getName();
|
||||||
|
|
||||||
// Early-return if user does not have access.
|
// Early-return if user does not have access.
|
||||||
$access = $this->accessChecker->accessEditEntityField($entity, $field_name);
|
$access = $this->accessChecker->accessEditEntityField($entity, $field_name);
|
||||||
|
@ -78,14 +79,13 @@ class MetadataGenerator implements MetadataGeneratorInterface {
|
||||||
|
|
||||||
// Early-return if no editor is available.
|
// Early-return if no editor is available.
|
||||||
$formatter_id = entity_get_render_display($entity, $view_mode)->getRenderer($field_name)->getPluginId();
|
$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, $items);
|
||||||
$editor_id = $this->editorSelector->getEditor($formatter_id, $field_definition, $items);
|
|
||||||
if (!isset($editor_id)) {
|
if (!isset($editor_id)) {
|
||||||
return array('access' => FALSE);
|
return array('access' => FALSE);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gather metadata, allow the editor to add additional metadata of its own.
|
// 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);
|
$editor = $this->editorManager->createInstance($editor_id);
|
||||||
$metadata = array(
|
$metadata = array(
|
||||||
'label' => check_plain($label),
|
'label' => check_plain($label),
|
||||||
|
@ -93,7 +93,7 @@ class MetadataGenerator implements MetadataGeneratorInterface {
|
||||||
'editor' => $editor_id,
|
'editor' => $editor_id,
|
||||||
'aria' => t('Entity @type @id, field @field', array('@type' => $entity->entityType(), '@id' => $entity->id(), '@field' => $label)),
|
'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)) {
|
if (count($custom_metadata)) {
|
||||||
$metadata['custom'] = $custom_metadata;
|
$metadata['custom'] = $custom_metadata;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
namespace Drupal\edit;
|
namespace Drupal\edit;
|
||||||
|
|
||||||
use Drupal\Core\Entity\EntityInterface;
|
use Drupal\Core\Entity\EntityInterface;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
use Drupal\Core\Field\FieldItemListInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface for generating in-place editing metadata.
|
* Interface for generating in-place editing metadata.
|
||||||
|
@ -19,24 +19,18 @@ interface MetadataGeneratorInterface {
|
||||||
* Generates in-place editing metadata for an entity.
|
* Generates in-place editing metadata for an entity.
|
||||||
*
|
*
|
||||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||||
* The entity being edited.
|
* The entity, in the language in which one of its fields is being edited.
|
||||||
* @param string $langcode
|
|
||||||
* The name of the language for which the field is being edited.
|
|
||||||
* @return array
|
* @return array
|
||||||
* An array containing metadata with the following keys:
|
* An array containing metadata with the following keys:
|
||||||
* - label: the user-visible label for the entity in the given language.
|
* - 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.
|
* Generates in-place editing metadata for an entity field.
|
||||||
*
|
*
|
||||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
* @param \Drupal\Core\Field\FieldItemListInterface $items
|
||||||
* The entity being edited.
|
* The field values to be in-place 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 string $view_mode
|
* @param string $view_mode
|
||||||
* The view mode the field should be rerendered in.
|
* The view mode the field should be rerendered in.
|
||||||
* @return array
|
* @return array
|
||||||
|
@ -47,6 +41,6 @@ interface MetadataGeneratorInterface {
|
||||||
* - aria: the ARIA label.
|
* - aria: the ARIA label.
|
||||||
* - custom: (optional) any additional metadata that the editor provides.
|
* - 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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,8 +7,8 @@
|
||||||
|
|
||||||
namespace Drupal\edit\Plugin\InPlaceEditor;
|
namespace Drupal\edit\Plugin\InPlaceEditor;
|
||||||
|
|
||||||
|
use Drupal\Core\Field\FieldItemListInterface;
|
||||||
use Drupal\edit\EditorBase;
|
use Drupal\edit\EditorBase;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the form in-place editor.
|
* Defines the form in-place editor.
|
||||||
|
@ -22,7 +22,7 @@ class FormEditor extends EditorBase {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function isCompatible(FieldDefinitionInterface $field_definition, array $items) {
|
public function isCompatible(FieldItemListInterface $items) {
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,8 +7,8 @@
|
||||||
|
|
||||||
namespace Drupal\edit\Plugin\InPlaceEditor;
|
namespace Drupal\edit\Plugin\InPlaceEditor;
|
||||||
|
|
||||||
|
use Drupal\Core\Field\FieldItemListInterface;
|
||||||
use Drupal\edit\EditorBase;
|
use Drupal\edit\EditorBase;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the plain text in-place editor.
|
* 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
|
* @todo The processed text logic is too coupled to text fields. Figure out
|
||||||
* how to generalize to other textual field types.
|
* 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.
|
// This editor is incompatible with multivalued fields.
|
||||||
if ($field_definition->getCardinality() != 1) {
|
if ($field_definition->getCardinality() != 1) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
namespace Drupal\edit\Tests;
|
namespace Drupal\edit\Tests;
|
||||||
|
|
||||||
|
use Drupal\Core\Language\Language;
|
||||||
use Drupal\edit\Plugin\InPlaceEditorManager;
|
use Drupal\edit\Plugin\InPlaceEditorManager;
|
||||||
use Drupal\edit\EditorSelector;
|
use Drupal\edit\EditorSelector;
|
||||||
|
|
||||||
|
@ -45,13 +46,13 @@ class EditorSelectionTest extends EditTestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the FieldInstance object for the given field and returns the
|
* Returns the in-place editor that Edit selects.
|
||||||
* 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);
|
$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'], $items);
|
||||||
return $this->editorSelector->getEditor($options['type'], $field_instance, $items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -72,32 +73,34 @@ class EditorSelectionTest extends EditTestBase {
|
||||||
array()
|
array()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Pretend there is an entity with these items for the field.
|
// Create an entity with values for this text field.
|
||||||
$items = array(array('value' => 'Hello, world!', 'format' => 'full_html'));
|
$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.
|
// 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.
|
// Editor selection with text processing, cardinality 1.
|
||||||
$this->field_text_instance->settings['text_processing'] = 1;
|
$this->field_text_instance->settings['text_processing'] = 1;
|
||||||
$this->field_text_instance->save();
|
$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).
|
// Editor selection without text processing, cardinality 1 (again).
|
||||||
$this->field_text_instance->settings['text_processing'] = 0;
|
$this->field_text_instance->settings['text_processing'] = 0;
|
||||||
$this->field_text_instance->save();
|
$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
|
// Editor selection without text processing, cardinality >1
|
||||||
$this->field_text_field->cardinality = 2;
|
$this->field_text_field->cardinality = 2;
|
||||||
$this->field_text_field->save();
|
$this->field_text_field->save();
|
||||||
$items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
|
$this->assertEqual('form', $this->getSelectedEditor($this->entity->id(), $field_name), "Without text processing, cardinality >1, the 'form' editor is selected.");
|
||||||
$this->assertEqual('form', $this->getSelectedEditor($items, $field_name), "Without text processing, cardinality >1, the 'form' editor is selected.");
|
|
||||||
|
|
||||||
// Editor selection with text processing, cardinality >1
|
// Editor selection with text processing, cardinality >1
|
||||||
$this->field_text_instance->settings['text_processing'] = 1;
|
$this->field_text_instance->settings['text_processing'] = 1;
|
||||||
$this->field_text_instance->save();
|
$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()
|
array()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Pretend there is an entity with these items for the field.
|
// Create an entity with values for this text field.
|
||||||
$items = array(array('value' => 'Hello, world!', 'format' => 'filtered_html'));
|
$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.
|
// 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.
|
// Editor selection w/ cardinality 1, text format w/ associated text editor.
|
||||||
$items[0]['format'] = 'full_html';
|
$this->entity->{$field_name}->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->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
|
// Editor selection with text processing, cardinality >1
|
||||||
$this->field_textarea_field->cardinality = 2;
|
$this->field_textarea_field->cardinality = 2;
|
||||||
$this->field_textarea_field->save();
|
$this->field_textarea_field->save();
|
||||||
$items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
|
$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.");
|
||||||
$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.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -156,16 +162,18 @@ class EditorSelectionTest extends EditTestBase {
|
||||||
array()
|
array()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Pretend there is an entity with these items for the field.
|
// Create an entity with values for this text field.
|
||||||
$items = array(42, 43);
|
$this->entity = entity_create('entity_test', array());
|
||||||
|
$this->entity->{$field_name}->value = 42;
|
||||||
|
$this->entity->save();
|
||||||
|
|
||||||
// Editor selection with cardinality 1.
|
// 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.
|
// Editor selection with cardinality >1.
|
||||||
$this->field_nr_field->cardinality = 2;
|
$this->field_nr_field->cardinality = 2;
|
||||||
$this->field_nr_field->save();
|
$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.");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -96,15 +96,14 @@ class MetadataGeneratorTest extends EditTestBase {
|
||||||
|
|
||||||
// Create an entity with values for this text field.
|
// Create an entity with values for this text field.
|
||||||
$this->entity = entity_create('entity_test', array());
|
$this->entity = entity_create('entity_test', array());
|
||||||
$this->is_new = TRUE;
|
|
||||||
$this->entity->{$field_1_name}->value = 'Test';
|
$this->entity->{$field_1_name}->value = 'Test';
|
||||||
$this->entity->{$field_2_name}->value = 42;
|
$this->entity->{$field_2_name}->value = 42;
|
||||||
$this->entity->save();
|
$this->entity->save();
|
||||||
$entity = entity_load('entity_test', $this->entity->id());
|
$entity = entity_load('entity_test', $this->entity->id());
|
||||||
|
|
||||||
// Verify metadata for field 1.
|
// Verify metadata for field 1.
|
||||||
$instance_1 = field_info_instance($entity->entityType(), $field_1_name, $entity->bundle());
|
$items_1 = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_1_name);
|
||||||
$metadata_1 = $this->metadataGenerator->generateField($entity, $instance_1, Language::LANGCODE_NOT_SPECIFIED, 'default');
|
$metadata_1 = $this->metadataGenerator->generateFieldMetadata($items_1, 'default');
|
||||||
$expected_1 = array(
|
$expected_1 = array(
|
||||||
'access' => TRUE,
|
'access' => TRUE,
|
||||||
'label' => 'Simple text field',
|
'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.');
|
$this->assertEqual($expected_1, $metadata_1, 'The correct metadata is generated for the first field.');
|
||||||
|
|
||||||
// Verify metadata for field 2.
|
// Verify metadata for field 2.
|
||||||
$instance_2 = field_info_instance($entity->entityType(), $field_2_name, $entity->bundle());
|
$items_2 = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_2_name);
|
||||||
$metadata_2 = $this->metadataGenerator->generateField($entity, $instance_2, Language::LANGCODE_NOT_SPECIFIED, 'default');
|
$metadata_2 = $this->metadataGenerator->generateFieldMetadata($items_2, 'default');
|
||||||
$expected_2 = array(
|
$expected_2 = array(
|
||||||
'access' => TRUE,
|
'access' => TRUE,
|
||||||
'label' => 'Simple number field',
|
'label' => 'Simple number field',
|
||||||
|
@ -169,8 +168,8 @@ class MetadataGeneratorTest extends EditTestBase {
|
||||||
$entity = entity_load('entity_test', $this->entity->id());
|
$entity = entity_load('entity_test', $this->entity->id());
|
||||||
|
|
||||||
// Verify metadata.
|
// Verify metadata.
|
||||||
$instance = field_info_instance($entity->entityType(), $field_name, $entity->bundle());
|
$items = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($field_name);
|
||||||
$metadata = $this->metadataGenerator->generateField($entity, $instance, Language::LANGCODE_NOT_SPECIFIED, 'default');
|
$metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default');
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'access' => TRUE,
|
'access' => TRUE,
|
||||||
'label' => 'Rich text field',
|
'label' => 'Rich text field',
|
||||||
|
|
|
@ -7,8 +7,8 @@
|
||||||
|
|
||||||
namespace Drupal\edit_test\Plugin\InPlaceEditor;
|
namespace Drupal\edit_test\Plugin\InPlaceEditor;
|
||||||
|
|
||||||
|
use Drupal\Core\Field\FieldItemListInterface;
|
||||||
use Drupal\edit\EditorBase;
|
use Drupal\edit\EditorBase;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the 'wysiwyg' in-place editor.
|
* Defines the 'wysiwyg' in-place editor.
|
||||||
|
@ -23,7 +23,9 @@ class WysiwygEditor extends EditorBase {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function isCompatible(FieldDefinitionInterface $field_definition, array $items) {
|
public function isCompatible(FieldItemListInterface $items) {
|
||||||
|
$field_definition = $items->getFieldDefinition();
|
||||||
|
|
||||||
// This editor is incompatible with multivalued fields.
|
// This editor is incompatible with multivalued fields.
|
||||||
if ($field_definition->getCardinality() != 1) {
|
if ($field_definition->getCardinality() != 1) {
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -32,8 +34,7 @@ class WysiwygEditor extends EditorBase {
|
||||||
// if there is a currently active text format and that text format is the
|
// if there is a currently active text format and that text format is the
|
||||||
// 'full_html' text format.
|
// 'full_html' text format.
|
||||||
elseif ($field_definition->getSetting('text_processing')) {
|
elseif ($field_definition->getSetting('text_processing')) {
|
||||||
$format_id = $items[0]['format'];
|
if ($items[0]->format === 'full_html') {
|
||||||
if (isset($format_id) && $format_id === 'full_html') {
|
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
@ -43,9 +44,8 @@ class WysiwygEditor extends EditorBase {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function getMetadata(FieldDefinitionInterface $field_definition, array $items) {
|
function getMetadata(FieldItemListInterface $items) {
|
||||||
$format_id = $items[0]['format'];
|
$metadata['format'] = $items[0]->format;
|
||||||
$metadata['format'] = $format_id;
|
|
||||||
return $metadata;
|
return $metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,8 +8,8 @@
|
||||||
namespace Drupal\editor\Plugin\InPlaceEditor;
|
namespace Drupal\editor\Plugin\InPlaceEditor;
|
||||||
|
|
||||||
use Drupal\Component\Plugin\PluginBase;
|
use Drupal\Component\Plugin\PluginBase;
|
||||||
|
use Drupal\Core\Field\FieldItemListInterface;
|
||||||
use Drupal\edit\EditPluginInterface;
|
use Drupal\edit\EditPluginInterface;
|
||||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the formatted text in-place editor.
|
* Defines the formatted text in-place editor.
|
||||||
|
@ -24,7 +24,9 @@ class Editor extends PluginBase implements EditPluginInterface {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function isCompatible(FieldDefinitionInterface $field_definition, array $items) {
|
public function isCompatible(FieldItemListInterface $items) {
|
||||||
|
$field_definition = $items->getFieldDefinition();
|
||||||
|
|
||||||
// This editor is incompatible with multivalued fields.
|
// This editor is incompatible with multivalued fields.
|
||||||
if ($field_definition->getCardinality() != 1) {
|
if ($field_definition->getCardinality() != 1) {
|
||||||
return FALSE;
|
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
|
// if there is a currently active text format, that text format has an
|
||||||
// associated editor and that editor supports inline editing.
|
// associated editor and that editor supports inline editing.
|
||||||
elseif ($field_definition->getSetting('text_processing')) {
|
elseif ($field_definition->getSetting('text_processing')) {
|
||||||
$format_id = $items[0]['format'];
|
if ($editor = editor_load($items[0]->format)) {
|
||||||
if (isset($format_id) && $editor = editor_load($format_id)) {
|
|
||||||
$definition = \Drupal::service('plugin.manager.editor')->getDefinition($editor->editor);
|
$definition = \Drupal::service('plugin.manager.editor')->getDefinition($editor->editor);
|
||||||
if ($definition['supports_inline_editing'] === TRUE) {
|
if ($definition['supports_inline_editing'] === TRUE) {
|
||||||
return TRUE;
|
return TRUE;
|
||||||
|
@ -48,8 +49,8 @@ class Editor extends PluginBase implements EditPluginInterface {
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
function getMetadata(FieldDefinitionInterface $field_definition, array $items) {
|
function getMetadata(FieldItemListInterface $items) {
|
||||||
$format_id = $items[0]['format'];
|
$format_id = $items[0]->format;
|
||||||
$metadata['format'] = $format_id;
|
$metadata['format'] = $format_id;
|
||||||
$metadata['formatHasTransformations'] = $this->textFormatHasTransformationFilters($format_id);
|
$metadata['formatHasTransformations'] = $this->textFormatHasTransformationFilters($format_id);
|
||||||
return $metadata;
|
return $metadata;
|
||||||
|
|
|
@ -106,13 +106,13 @@ class EditIntegrationTest extends EditTestBase {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieves the FieldInstance object for the given field and returns the
|
* Returns the in-place editor that Edit selects.
|
||||||
* 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);
|
$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'], $items);
|
||||||
return $this->editorSelector->getEditor($options['type'], $field_instance, $items);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,21 +126,24 @@ class EditIntegrationTest extends EditTestBase {
|
||||||
$this->editorManager = new InPlaceEditorManager($this->container->get('container.namespaces'));
|
$this->editorManager = new InPlaceEditorManager($this->container->get('container.namespaces'));
|
||||||
$this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter'));
|
$this->editorSelector = new EditorSelector($this->editorManager, $this->container->get('plugin.manager.field.formatter'));
|
||||||
|
|
||||||
// Pretend there is an entity with these items for the field.
|
// Create an entity with values for this text field.
|
||||||
$items = array(array('value' => 'Hello, world!', 'format' => 'filtered_html'));
|
$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.
|
// 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.
|
// Editor selection w/ cardinality 1, text format w/ associated text editor.
|
||||||
$items[0]['format'] = 'full_html';
|
$this->entity->{$this->field_name}->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->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
|
// Editor selection with text processing, cardinality >1
|
||||||
$this->field_textarea_field->cardinality = 2;
|
$this->field_textarea_field->cardinality = 2;
|
||||||
$this->field_textarea_field->save();
|
$this->field_textarea_field->save();
|
||||||
$items[] = array('value' => 'Hallo, wereld!', 'format' => 'full_html');
|
$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.");
|
||||||
$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.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -160,8 +163,8 @@ class EditIntegrationTest extends EditTestBase {
|
||||||
$entity = entity_load('entity_test', $this->entity->id());
|
$entity = entity_load('entity_test', $this->entity->id());
|
||||||
|
|
||||||
// Verify metadata.
|
// Verify metadata.
|
||||||
$instance = field_info_instance($entity->entityType(), $this->field_name, $entity->bundle());
|
$items = $entity->getTranslation(Language::LANGCODE_NOT_SPECIFIED)->get($this->field_name);
|
||||||
$metadata = $this->metadataGenerator->generateField($entity, $instance, Language::LANGCODE_NOT_SPECIFIED, 'default');
|
$metadata = $this->metadataGenerator->generateFieldMetadata($items, 'default');
|
||||||
$expected = array(
|
$expected = array(
|
||||||
'access' => TRUE,
|
'access' => TRUE,
|
||||||
'label' => 'Long text field',
|
'label' => 'Long text field',
|
||||||
|
|
Loading…
Reference in New Issue