Verified Commit e7159707 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3421011 by sorlov, Wim Leers, quietone, kalpanajaiswal, mstrelan,...

Issue #3421011 by sorlov, Wim Leers, quietone, kalpanajaiswal, mstrelan, Ruturaj Chaubey, smustgrave, larowlan: Convert Editor plugin discovery to attributes
parent a52189b3
Loading
Loading
Loading
Loading
Loading
+16 −15
Original line number Diff line number Diff line
@@ -20,9 +20,11 @@
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\ckeditor5\SmartDefaultSettings;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Validation\Plugin\Validation\Constraint\PrimitiveTypeConstraint;
use Drupal\editor\Attribute\Editor;
use Drupal\editor\EditorInterface;
use Drupal\editor\Entity\Editor;
use Drupal\editor\Entity\Editor as EditorEntity;
use Drupal\editor\Plugin\EditorBase;
use Drupal\filter\FilterFormatInterface;
use Psr\Log\LoggerInterface;
@@ -33,20 +35,19 @@
/**
 * Defines a CKEditor 5-based text editor for Drupal.
 *
 * @Editor(
 *   id = "ckeditor5",
 *   label = @Translation("CKEditor 5"),
 *   supports_content_filtering = TRUE,
 *   supports_inline_editing = TRUE,
 *   is_xss_safe = FALSE,
 *   supported_element_types = {
 *     "textarea"
 *   }
 * )
 *
 * @internal
 *   Plugin classes are internal.
 */
#[Editor(
  id: 'ckeditor5',
  label: new TranslatableMarkup('CKEditor 5'),
  supports_content_filtering: TRUE,
  supports_inline_editing: TRUE,
  is_xss_safe: FALSE,
  supported_element_types: [
    'textarea',
  ]
)]
class CKEditor5 extends EditorBase implements ContainerFactoryPluginInterface {

  /**
@@ -532,7 +533,7 @@ public static function assessActiveTextEditorAfterBuild(array $element, FormStat
   */
  public static function validateSwitchingToCKEditor5(array $form, FormStateInterface $form_state): void {
    if (!$form_state->get('ckeditor5_is_active') && $form_state->get('ckeditor5_is_selected')) {
      $minimal_ckeditor5_editor = Editor::create([
      $minimal_ckeditor5_editor = EditorEntity::create([
        'format' => NULL,
        'editor' => 'ckeditor5',
      ]);
@@ -943,7 +944,7 @@ public function submitConfigurationForm(array &$form, FormStateInterface $form_s
  /**
   * {@inheritdoc}
   */
  public function getJSSettings(Editor $editor) {
  public function getJSSettings(EditorEntity $editor) {
    $toolbar_items = $editor->getSettings()['toolbar']['items'];
    $plugin_config = $this->ckeditor5PluginManager->getCKEditor5PluginConfig($editor);

@@ -965,7 +966,7 @@ public function getJSSettings(Editor $editor) {
  /**
   * {@inheritdoc}
   */
  public function getLibraries(Editor $editor) {
  public function getLibraries(EditorEntity $editor) {
    $plugin_libraries = $this->ckeditor5PluginManager->getEnabledLibraries($editor);

    if ($this->moduleHandler->moduleExists('locale')) {
+53 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\editor\Attribute;

use Drupal\Component\Plugin\Attribute\Plugin;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines an Editor attribute object.
 *
 * Plugin Namespace: Plugin\Editor
 *
 * For a working example, see \Drupal\ckeditor5\Plugin\Editor\CKEditor5
 *
 * @see \Drupal\editor\Plugin\EditorPluginInterface
 * @see \Drupal\editor\Plugin\EditorBase
 * @see \Drupal\editor\Plugin\EditorManager
 * @see hook_editor_info_alter()
 * @see plugin_api
 */
#[\Attribute(\Attribute::TARGET_CLASS)]
class Editor extends Plugin {

  /**
   * Constructs an Editor object.
   *
   * @param string $id
   *   The plugin ID.
   * @param \Drupal\Core\StringTranslation\TranslatableMarkup $label
   *   The human-readable name of the text editor, translated
   * @param bool $supports_content_filtering
   *   Whether the editor supports "allowed content only" filtering.
   * @param bool $supports_inline_editing
   *   Whether the editor supports the inline editing provided by the Edit
   *   module.
   * @param bool $is_xss_safe
   *   Whether this text editor is not vulnerable to XSS attacks.
   * @param string[] $supported_element_types
   *   On which form element #types this text editor is capable of working.
   * @param class-string|null $deriver
   *   (optional) The deriver class.
   */
  public function __construct(
    public readonly string $id,
    public readonly TranslatableMarkup $label,
    public readonly bool $supports_content_filtering,
    public readonly bool $supports_inline_editing,
    public readonly bool $is_xss_safe,
    public readonly array $supported_element_types,
    public readonly ?string $deriver = NULL,
  ) {}

}
+2 −1
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\editor\Attribute\Editor;

/**
 * Configurable text editor manager.
@@ -28,7 +29,7 @@ class EditorManager extends DefaultPluginManager {
   *   The module handler to invoke the alter hook with.
   */
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
    parent::__construct('Plugin/Editor', $namespaces, $module_handler, 'Drupal\editor\Plugin\EditorPluginInterface', 'Drupal\editor\Annotation\Editor');
    parent::__construct('Plugin/Editor', $namespaces, $module_handler, 'Drupal\editor\Plugin\EditorPluginInterface', Editor::class, 'Drupal\editor\Annotation\Editor');
    $this->alterInfo('editor_info');
    $this->setCacheBackend($cache_backend, 'editor_plugins');
  }
+15 −14
Original line number Diff line number Diff line
@@ -3,23 +3,24 @@
namespace Drupal\editor_test\Plugin\Editor;

use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
use Drupal\editor\Attribute\Editor;
use Drupal\editor\Entity\Editor as EditorEntity;
use Drupal\editor\Plugin\EditorBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a Tyrannosaurus-Rex powered text editor for testing purposes.
 *
 * @Editor(
 *   id = "trex",
 *   label = @Translation("TRex Editor"),
 *   supports_content_filtering = TRUE,
 *   supports_inline_editing = TRUE,
 *   is_xss_safe = FALSE,
 *   supported_element_types = {
 *     "textarea",
 *   }
 * )
 */
#[Editor(
  id: 'trex',
  label: new TranslatableMarkup('TRex Editor'),
  supports_content_filtering: TRUE,
  supports_inline_editing: TRUE,
  is_xss_safe: FALSE,
  supported_element_types: [
    'textarea',
  ]
)]
class TRexEditor extends EditorBase {

  /**
@@ -44,7 +45,7 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
  /**
   * {@inheritdoc}
   */
  public function getJSSettings(Editor $editor) {
  public function getJSSettings(EditorEntity $editor) {
    $js_settings = [];
    $settings = $editor->getSettings();
    if ($settings['stumpy_arms']) {
@@ -56,7 +57,7 @@ public function getJSSettings(Editor $editor) {
  /**
   * {@inheritdoc}
   */
  public function getLibraries(Editor $editor) {
  public function getLibraries(EditorEntity $editor) {
    return [
      'editor_test/trex',
    ];
+16 −15
Original line number Diff line number Diff line
@@ -3,24 +3,25 @@
namespace Drupal\editor_test\Plugin\Editor;

use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
use Drupal\editor\Attribute\Editor;
use Drupal\editor\Entity\Editor as EditorEntity;
use Drupal\editor\Plugin\EditorBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Defines a Unicorn-powered text editor for Drupal (for testing purposes).
 *
 * @Editor(
 *   id = "unicorn",
 *   label = @Translation("Unicorn Editor"),
 *   supports_content_filtering = TRUE,
 *   supports_inline_editing = TRUE,
 *   is_xss_safe = FALSE,
 *   supported_element_types = {
 *     "textarea",
 *     "textfield",
 *   }
 * )
 */
#[Editor(
  id: 'unicorn',
  label: new TranslatableMarkup('Unicorn Editor'),
  supports_content_filtering: TRUE,
  supports_inline_editing: TRUE,
  is_xss_safe: FALSE,
  supported_element_types: [
    'textarea',
    'textfield',
  ]
)]
class UnicornEditor extends EditorBase {

  /**
@@ -61,7 +62,7 @@ public function validateImageUploadSettings(array $element, FormStateInterface $
  /**
   * {@inheritdoc}
   */
  public function getJSSettings(Editor $editor) {
  public function getJSSettings(EditorEntity $editor) {
    $js_settings = [];
    $settings = $editor->getSettings();
    if ($settings['ponies_too']) {
@@ -73,7 +74,7 @@ public function getJSSettings(Editor $editor) {
  /**
   * {@inheritdoc}
   */
  public function getLibraries(Editor $editor) {
  public function getLibraries(EditorEntity $editor) {
    return [
      'editor_test/unicorn',
    ];