Configuring the WYSIWYG editor

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.

Getting ready

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.

How to do it…

Let's create a new text format with a custom CKEditor WYSIWYG configuration:

  1. Visit Configuration and head to Text formats and editors under the Content authoring heading.
  2. Click on Add text format to begin creating the new text format:
    How to do it…
  3. Enter a name for the text format, such as editor format.
  4. Select which roles have access to this format – this allows you to have granular control over what users can use when authoring content.
  5. Select CKEditor from the Text editor select list. The configuration form for CKEditor will then be loaded.
  6. You may now use an in-place editor to drag buttons onto the provided toolbar to configure your CKEditor toolbar:
    How to do it…
  7. Select any of the Enabled filters you would like, except for Display any HTML as Plain text. That would be counter intuitive to using a WYSIWYG editor!

How it works…

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.

Note

The text formats and editors screen warns of a security risk due to improper configuration. That is because you could grant an anonymous user access to a text format that allows full HTML, or allow image sources to be from remote URLs.

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.

There's more…

Drupal provides granular control of how rich text is rendered and extensible ways as well, which we will discuss further.

Filter module

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.

CKEditor plugins

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.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset