Hi there,

I need to add CSS and JS files to some Textareas, so I'm trying to create new Text Editor.

Following this article https://www.drupal.org/docs/8/api/text-editor-api

Inside my module I've created a file: src\Plugin\Editor\MyEditor.php with the default text:

<?php

namespace Drupal\my_editor\Plugin\Editor;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Render\RendererInterface;
use Drupal\editor\Plugin\EditorBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\editor\Entity\Editor;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Defines a CKEditor-based text editor for Drupal.
 *
 * @Editor(
 *   id = "MyEditor",
 *   label = @Translation("MyEditor"),
 *   is_xss_safe = FALSE,
 *   supported_element_types = {
 *     "textarea"
 *   }
 * )
 */
class MyEditor extends EditorBase{

}

As result, I can see my editor on the Format settings page under the "Text Editor" selectbox, but I got error
You must configure the selected text editor.
if I select MyEditor and save the form.

How to solve this problem? May be there is easies way to add CSS and JS styles for some TextFormats?

Thanks.

Comments

johnwebdev’s picture

EditorBase does not implement all the methods required. You also have to implement:

  public function getJSSettings(Editor $editor);
  public function getLibraries(Editor $editor);

In your Editor plugin.

Just a small tip, if you don't have any settings for your Text editor you can follow this issue or apply the patch, since it won't allow you to actually set a text editor without any settings without crashing.

https://www.drupal.org/node/2837011

Coyote6GraphX’s picture

For anyone struggling with the switch to Drupal 8 module development from Drupal 6 or 7 or someone just starting, I created a very basic example of an editor plugin, with just the bare minimum to be able to select and save upon configuration.  Spent all day yesterday reading docs trying to figure it out, so hopefully this saves some time for other people.  I posted some common pitfalls in the README.md file.

https://github.com/Coyote6/drupal-8-custom-editor-module-base