Commit bc084e18 authored by catch's avatar catch

Issue #2327941 by Wim Leers, Sonal.Sangale: Simplify in-place editor selection...

Issue #2327941 by Wim Leers, Sonal.Sangale: Simplify in-place editor selection (Was: Refactor PlainTextEditor/WysiwygEditor/Editor in-place editors now that text fields no longer have configurable filteredness)
parent b313cdcd
......@@ -67,6 +67,20 @@ function editor_element_info_alter(&$types) {
/**
* Implements hook_form_FORM_ID_alter() for \Drupal\filter\FilterFormatListBuilder.
*
* Implements hook_field_formatter_info_alter().
*
* @see quickedit_field_formatter_info_alter()
*/
function editor_field_formatter_info_alter(&$info) {
// Update \Drupal\text\Plugin\Field\FieldFormatter\TextDefaultFormatter's
// annotation to indicate that it supports the 'editor' in-place editor
// provided by this module.
$info['text_default']['quickedit'] = ['editor' => 'editor'];
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function editor_form_filter_admin_overview_alter(&$form, FormStateInterface $form_state) {
// @todo Cleanup column injection: https://www.drupal.org/node/1876718.
......
......@@ -12,8 +12,7 @@
* Defines the formatted text in-place editor.
*
* @InPlaceEditor(
* id = "editor",
* alternativeTo = {"plain_text"}
* id = "editor"
* )
*/
class Editor extends PluginBase implements InPlaceEditorInterface {
......@@ -31,16 +30,14 @@ public function isCompatible(FieldItemListInterface $items) {
// This editor is compatible with formatted ("rich") text fields; but only
// if there is a currently active text format, that text format has an
// associated editor and that editor supports inline editing.
elseif (in_array($field_definition->getType(), array('text', 'text_long', 'text_with_summary'), TRUE)) {
if ($editor = editor_load($items[0]->format)) {
$definition = \Drupal::service('plugin.manager.editor')->getDefinition($editor->getEditor());
if ($definition['supports_inline_editing'] === TRUE) {
return TRUE;
}
elseif ($editor = editor_load($items[0]->format)) {
$definition = \Drupal::service('plugin.manager.editor')->getDefinition($editor->getEditor());
if ($definition['supports_inline_editing'] === TRUE) {
return TRUE;
}
return FALSE;
}
return FALSE;
}
/**
......
......@@ -27,14 +27,6 @@ class InPlaceEditor extends Plugin {
*/
public $id;
/**
* An array of in-place editors plugin IDs that have registered themselves as
* alternatives to this in-place editor.
*
* @var array
*/
public $alternativeTo;
/**
* The name of the module providing the in-place editor plugin.
*
......
......@@ -50,21 +50,8 @@ public function __construct(PluginManagerInterface $editor_manager, FormatterPlu
* {@inheritdoc}
*/
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)) {
$editors = $this->editorManager->getDefinitions();
foreach ($editors as $alternative_editor_id => $editor) {
if (isset($editor['alternativeTo'])) {
foreach ($editor['alternativeTo'] as $original_editor_id) {
$this->alternatives[$original_editor_id][] = $alternative_editor_id;
}
}
}
}
// Check if the formatter defines an appropriate in-place editor. For
// example, text formatters displaying untrimmed text can choose to use the
// example, text formatters displaying plain text can choose to use the
// 'plain_text' editor. If the formatter doesn't specify, fall back to the
// 'form' editor, since that can work for any field. Formatter definitions
// can use 'disabled' to explicitly opt out of in-place editing.
......
......@@ -21,16 +21,7 @@ public function isCompatible(FieldItemListInterface $items) {
$field_definition = $items->getFieldDefinition();
// This editor is incompatible with multivalued fields.
if ($field_definition->getFieldStorageDefinition()->getCardinality() != 1) {
return FALSE;
}
// This editor is incompatible with formatted ("rich") text fields.
elseif (in_array($field_definition->getType(), array('text', 'text_long', 'text_with_summary'), TRUE)) {
return FALSE;
}
else {
return TRUE;
}
return $field_definition->getFieldStorageDefinition()->getCardinality() == 1;
}
/**
......
......@@ -18,3 +18,16 @@ function quickedit_test_quickedit_render_field(EntityInterface $entity, $field_n
'#suffix' => '</div>',
);
}
/**
* Implements hook_field_formatter_info_alter().
*
* @see quickedit_field_formatter_info_alter()
* @see editor_field_formatter_info_alter()
*/
function quickedit_test_field_formatter_info_alter(&$info) {
// Update \Drupal\text\Plugin\Field\FieldFormatter\TextDefaultFormatter's
// annotation to indicate that it supports the 'wysiwyg' in-place editor
// provided by this module.
$info['text_default']['quickedit'] = ['editor' => 'wysiwyg'];
}
......@@ -10,7 +10,6 @@
*
* @InPlaceEditor(
* id = "wysiwyg",
* alternativeTo = {"plain_text"}
* )
*/
class WysiwygEditor extends InPlaceEditorBase {
......@@ -28,12 +27,7 @@ public function isCompatible(FieldItemListInterface $items) {
// This editor is compatible with formatted ("rich") text fields; but only
// if there is a currently active text format and that text format is the
// 'full_html' text format.
elseif (in_array($field_definition->getType(), array('text', 'text_long', 'text_with_summary'), TRUE)) {
if ($items[0]->format === 'full_html') {
return TRUE;
}
return FALSE;
}
return $items[0]->format === 'full_html';
}
/**
......
......@@ -15,9 +15,6 @@
* "text",
* "text_long",
* "text_with_summary",
* },
* quickedit = {
* "editor" = "plain_text"
* }
* )
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment