Commit aed10c0f authored by catch's avatar catch

Issue #2326721 by rpayanm, subhojit777, tim.plunkett, Wim Leers,...

Issue #2326721 by rpayanm, subhojit777, tim.plunkett, Wim Leers, harshil.maradiya, vijaycs85, Xano, Hjarnmastara: EditorPluginInterface should extend PluginFormInterface
parent c1d947b6
......@@ -144,7 +144,7 @@
};
// Create hidden CKEditor with all features enabled, retrieve metadata.
// @see \Drupal\ckeditor\Plugin\Editor\CKEditor::settingsForm.
// @see \Drupal\ckeditor\Plugin\Editor\CKEditor::buildConfigurationForm().
var hiddenCKEditorID = 'ckeditor-hidden';
if (CKEDITOR.instances[hiddenCKEditorID]) {
CKEDITOR.instances[hiddenCKEditorID].destroy(true);
......
......@@ -168,12 +168,13 @@ function editor_form_filter_format_form_alter(&$form, FormStateInterface $form_s
// Add editor-specific validation and submit handlers.
if ($editor) {
/** @var $plugin \Drupal\editor\Plugin\EditorPluginInterface */
$plugin = $manager->createInstance($editor->getEditor());
$settings_form = array();
$settings_form['#element_validate'][] = array($plugin, 'settingsFormValidate');
$form['editor']['settings']['subform'] = $plugin->settingsForm($settings_form, $form_state, $editor);
$settings_form['#element_validate'][] = array($plugin, 'validateConfigurationForm');
$form['editor']['settings']['subform'] = $plugin->buildConfigurationForm($settings_form, $form_state);
$form['editor']['settings']['subform']['#parents'] = array('editor', 'settings');
$form['actions']['submit']['#submit'][] = array($plugin, 'settingsFormSubmit');
$form['actions']['submit']['#submit'][] = array($plugin, 'submitConfigurationForm');
}
$form['#validate'][] = 'editor_form_filter_admin_format_validate';
......
......@@ -31,21 +31,51 @@ public function getDefaultSettings() {
/**
* {@inheritdoc}
*
* @todo Remove in Drupal 9.0.0.
*/
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
@trigger_error('The ' . __METHOD__ . ' method is deprecated since version 8.3.x and will be removed in 9.0.0.', E_USER_DEPRECATED);
return $form;
}
/**
* {@inheritdoc}
*
* @todo Remove in Drupal 9.0.0.
*/
public function settingsFormValidate(array $form, FormStateInterface $form_state) {
@trigger_error('The ' . __METHOD__ . ' method is deprecated since version 8.3.x and will be removed in 9.0.0.', E_USER_DEPRECATED);
}
/**
* {@inheritdoc}
*
* @todo Remove in Drupal 9.0.0.
*/
public function settingsFormSubmit(array $form, FormStateInterface $form_state) {
@trigger_error('The ' . __METHOD__ . ' method is deprecated since version 8.3.x and will be removed in 9.0.0.', E_USER_DEPRECATED);
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
return $this->settingsForm($form, $form_state, $form_state->get('editor'));
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
return $this->settingsFormValidate($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
return $this->settingsFormSubmit($form, $form_state);
}
}
......@@ -3,7 +3,7 @@
namespace Drupal\editor\Plugin;
use Drupal\Component\Plugin\PluginInspectionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\PluginFormInterface;
use Drupal\editor\Entity\Editor;
/**
......@@ -12,12 +12,17 @@
* Modules implementing this interface may want to extend the EditorBase class,
* which provides default implementations of each method where appropriate.
*
* If the editor's behavior depends on extensive options and/or external data,
* then the implementing module can choose to provide a separate, global
* configuration page rather than per-text-format settings. In that case, this
* form should provide a link to the separate settings page.
*
* @see \Drupal\editor\Annotation\Editor
* @see \Drupal\editor\Plugin\EditorBase
* @see \Drupal\editor\Plugin\EditorManager
* @see plugin_api
*/
interface EditorPluginInterface extends PluginInspectionInterface {
interface EditorPluginInterface extends PluginInspectionInterface, PluginFormInterface {
/**
* Returns the default settings for this configurable text editor.
......@@ -28,53 +33,6 @@ interface EditorPluginInterface extends PluginInspectionInterface {
*/
public function getDefaultSettings();
/**
* Returns a settings form to configure this text editor.
*
* If the editor's behavior depends on extensive options and/or external data,
* then the implementing module can choose to provide a separate, global
* configuration page rather than per-text-format settings. In that case, this
* form should provide a link to the separate settings page.
*
* @param array $form
* An empty form array to be populated with a configuration form, if any.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The state of the entire filter administration form.
* @param \Drupal\editor\Entity\Editor $editor
* A configured text editor object.
*
* @return array
* A render array for the settings form.
*/
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor);
/**
* Validates the settings form for an editor.
*
* The contents of the editor settings are located in
* $form_state->getValue(array('editor', 'settings')). Calls to $form_state->setError()
* should reflect this location in the settings form.
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function settingsFormValidate(array $form, FormStateInterface $form_state);
/**
* Modifies any values in the form state to prepare them for saving.
*
* Values in $form_state->getValue(array('editor', 'settings')) are saved by
* Editor module in editor_form_filter_admin_format_submit().
*
* @param array $form
* An associative array containing the structure of the form.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
*/
public function settingsFormSubmit(array $form, FormStateInterface $form_state);
/**
* Returns JavaScript settings to be attached.
*
......
<?php
namespace Drupal\Tests\editor\Unit;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
use Drupal\editor\Plugin\EditorBase;
use Drupal\Tests\UnitTestCase;
/**
* @coversDefaultClass \Drupal\editor\Plugin\EditorBase
* @group editor
*/
class EditorBaseTest extends UnitTestCase {
/**
* @covers ::buildConfigurationForm
* @covers ::validateConfigurationForm
* @covers ::submitConfigurationForm
*/
public function testBc() {
$form_state = new FormState();
$form_state->set('editor', $this->prophesize(Editor::class)->reveal());
$editor_plugin = new BcEditor([], 'editor_plugin', []);
// settingsForm() is deprecated in favor of buildConfigurationForm().
$this->assertSame(
$editor_plugin->settingsForm([], clone $form_state, $this->prophesize(Editor::class)->reveal()),
$editor_plugin->buildConfigurationForm([], clone $form_state)
);
// settingsFormValidate() is deprecated in favor of
// validateConfigurationForm().
$form = [];
$form_state_a = clone $form_state;
$form_state_b = clone $form_state;
$editor_plugin->settingsFormValidate($form, $form_state_a, $this->prophesize(Editor::class)->reveal());
$editor_plugin->validateConfigurationForm($form, $form_state_b);
$this->assertEquals($form_state_a, $form_state_b);
// settingsFormSubmit() is deprecated in favor of submitConfigurationForm().
$form = [];
$form_state_a = clone $form_state;
$form_state_b = clone $form_state;
$editor_plugin->settingsFormSubmit($form, $form_state_a, $this->prophesize(Editor::class)->reveal());
$editor_plugin->submitConfigurationForm($form, $form_state_b);
$this->assertEquals($form_state_a, $form_state_b);
}
}
class BcEditor extends EditorBase {
public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
return ['foo' => 'bar'];
}
public function settingsFormValidate(array $form, FormStateInterface $form_state) {
$form_state->setValue('foo', 'bar');
}
public function settingsFormSubmit(array $form, FormStateInterface $form_state) {
$form_state->setValue('bar', 'baz');
}
public function getJSSettings(Editor $editor) {
return [];
}
public function getLibraries(Editor $editor) {
return [];
}
}
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