Drupal 8 caused the collaboration between the Drupal development community and the CKEditor development community. Because of this, Drupal now ships with CKEditor out of the box as the default What You See Is What You Get (WYSIWYG) editor. The new Editor module provides an API for integrating WYSIWYG editors. Even though CKEditor is provided out of the box, contributed modules can provide integrations with other WYSIWYG editors.
Text formats control the formatting of content and WYSIWYG editor configuration for content authors. The standard Drupal installation profile provides a fully configured text format with CKEditor enabled. We will walk through the steps of recreating this text format.
In this recipe we will create a new text format with a custom CKEditor WYSIWYG configuration.
Before starting, make sure that the CKEditor module is enabled, which also requires Editor as a dependency. Editor is the module that provides an API to integrate WYSIWYG editors with text formats.
Let's create a new text format with a custom CKEditor WYSIWYG configuration:
The Filter modules provide text formats that control over how rich text fields are presented to the user. Drupal will render rich text saved in a text area based on the defined text format for the field. Text fields with (formatted) in their title will respect text format settings, others will render in plain text.
The Editor module provides a bridge to WYSIWYG editors and text formats. It alters the text format form and rendering to allow the integration of WYSIWYG editor libraries. This allows each text format to have its own configuration for its WYSIWYG editor.
Out of the box the Editor module alone does not provide an editor. The CKEditor module works with the Editor API to enable usage of that WYSIWYG editor.
Drupal can support other WYSWIG editors, such as MarkItUp or TinyMCE through contributed modules.
Drupal provides granular control of how rich text is rendered and extensible ways as well, which we will discuss further.
When string data is added to a field that supports text formats, the data is saved and preserved as it was originally entered. Enabled filters for a text format will not be applied until the content is viewed. Drupal works in such a way that it saves the original content and only filters on display.
With the Filter module enabled, you gain the ability to specify how text is rendered based on the roles of the user who created the text. It is important to understand the filters applied to a text format that uses a WYSIWYG editor. For example, if you selected the Display any HTML as plain text option, the formatting done by the WYSIWYG editor would be stripped out when viewed.
The CKEditor module provides a plugin type called CKEditorPlugin
. Plugins are small pieces of swappable functionality within Drupal 8. Plugins and plugin development are covered in Chapter 7, Plug and Play with Plugins. This type provides integration between CKEditor and Drupal 8.
The image and link capabilities are plugins defined within the CKEditor module. Additional plugins can be provided through contributed projects or custom development.
See the DrupalckeditorAnnotationCKEditorPlugin
class for the plugin definition and the suggested DrupalckeditorPluginCKEditorPluginDrupalImage
class as a working example.