# CKEditor
First-party rich text editor by Pixel & Tonic. CKEditor 5 (currently bundled at upstream 48.x) with drag-and-drop toolbar config, nested entries, element linking, image uploads, custom styles, and HTML Purifier integration. Replaces Redactor as Craft's default rich text solution.
`craftcms/ckeditor` — Free. Current major: 5.x.
## Documentation
- GitHub (primary docs): https://github.com/craftcms/ckeditor
- Plugin Store: https://plugins.craftcms.com/ckeditor
- CKEditor 5 config reference: https://ckeditor.com/docs/ckeditor5/latest/api/module_core_editor_editorconfig-EditorConfig.html
When unsure about CKEditor options, `WebFetch` the GitHub readme.
## Common Pitfalls
- Not enabling HTML Purifier rules for custom styles — CKEditor's client-side editor allows certain markup, but HTML Purifier strips it on save if the rules don't permit it. Relax Purifier config in `config/htmlpurifier/` when adding custom styles or plugins.
- Forgetting to cache oEmbed content — with "Parse embeds" enabled, CKEditor fetches embed HTML on every request. Wrap output in `{% cache %}` tags or use Blitz.
- Nested entries without partial templates — nested entries render via `_partials/entry/{entryTypeHandle}.twig`. Missing templates produce blank output.
- Using `{{ entry.ckeditorField }}` directly when nested entries exist — the field returns an iterable chunk array, not a string. Direct output renders `Array`. Always use the chunk loop pattern.
- Configuring heading levels in both the field's `$headingLevels` setting and the config options' `heading.options` — the field setting takes precedence. Don't set `heading.options` in config unless you need fine-grained control beyond what the field exposes.
- Images as `` vs nested entries — CKEditor supports both via `$imageMode`. Using nested entries gives editors more control (alt text fields, captions, transforms) but adds rendering complexity. See Image Mode below.
- Pre-5.0 custom plugin asset bundles silently break — plugin 5.0.0 changed third-party CKEditor plugin registration to ES modules. Asset bundles built against the 4.x pattern load but their plugins never register. See Extending with Custom Plugins.
- Custom plugin missing from a field after upgrading to CKEditor 5.8.0 — packages now load per field only when one of their `$toolbarItems` is in that field's toolbar, and custom pages that create their own editors must call `Plugin::registerCkeditorPackageBundles($view)`. See Extending with Custom Plugins.
- Pre-5.0 CKEditor Config records carrying toolbar/heading settings — these have moved to field-level settings (`$toolbar`, `$headingLevels`). The v5 upgrade migrates them; double-check fields after upgrade.
## Configuration
CKEditor field settings expose toolbar arrangement, heading levels, config options, and custom styles directly on the field — no separate "CKEditor Config" record needed for these (as of 5.0.0). Shared configs that span multiple fields live in `config/ckeditor/` files (as of 5.3.0).
### Field Settings
The CKEditor field carries these settings, configurable in the field's UI or programmatically:
| Setting | Type | Purpose |
|---------|------|---------|
| `$toolbar` | array | Toolbar button arrangement. Default: `['heading', '\|', 'bold', 'italic', 'link']` |
| `$headingLevels` | array \| false | Allowed heading levels. Default: `[1..6]`. `false` disables headings entirely. |
| `$options` | string | Inline JS/JSON config (CKEditor `EditorConfig` shape) |
| `$styles` | string | Inline CSS for the editor surface |
| `$jsFile` | string | Path to a `.js` or `.json` file in `config/ckeditor/` (5.3.0+) |
| `$cssFile` | string | Path to a `.css` file in `config/ckeditor/` (5.3.0+) |
| `$imageMode` | string | `'img'` (default) or `'entries'` (5.0.0+, see Image Mode) |
| `$imageEntryTypeUid` | string | Entry type UID used when `$imageMode = 'entries'` |
| `$imageFieldUid` | string | Assets field UID inside the image entry type |
| `$advancedLinkFields` | array | Extra fields shown in the link modal (5.0.0+, see Link Configuration) |
| `$fullGraphqlData` | bool | GraphQL Mode toggle (4.8.0+, see GraphQL Mode) |
Programmatic config goes through `Field::getJson()` / `Field::setJson()`:
```php
$json = $field->getJson();
$json['toolbar'][] = 'fullscreen';
$field->setJson($json);
```
### Config Options (JSON)
Inline config goes in the field's "Config Options" textarea, or in a `.json` / `.js` file referenced by `$jsFile`:
```json
{
"table": {
"contentToolbar": [
"toggleTableCaption",
"tableColumn",
"tableRow",
"mergeTableCells",
"tableProperties",
"tableCellProperties"
]
},
"list": {
"properties": {
"styles": true,
"startIndex": true,
"reversed": true
}
}
}
```
Custom `toolbar` values are respected as of plugin 5.2.0 — earlier versions silently overrode toolbar in the config with the field-level setting. Set toolbar via the field setting where possible; reach for config-level `toolbar` only for ordering tweaks that the UI can't express.
### Config as Files (5.3.0+)
Drop shared configs into `config/ckeditor/` and reference them per-field:
```
config/
└── ckeditor/
├── default.js # CKEditor config (JS export or JSON)
├── minimal.json
└── editor-styles.css # Custom CSS for the editor surface
```
Set `$jsFile = 'default.js'` and `$cssFile = 'editor-styles.css'` on the field. The v5 upgrade migration auto-extracts shared configs from CKEditor Config records into `config/ckeditor/` files when the same config is used by 2+ fields.
### Custom Styles
Custom style classes appear in the editor's Styles dropdown:
```json
[
{ "label": "Lead Text", "element": "p", "classes": ["text-lead"] },
{ "label": "Callout", "element": "div", "classes": ["callout"] },
{ "label": "Button Link", "element": "a", "classes": ["btn", "btn-primary"] }
]
```
The CSS that styles these classes inside the editor surface lives in `$styles` (inline) or `$cssFile` (5.3.0+). `@import` works in the `$cssFile` CSS as of 5.2.1 — pull in shared font/color tokens without inlining them.
Ensure HTML Purifier permits the corresponding classes and elements on save.
## Image Mode (5.0.0+)
CKEditor fields can render images two ways:
- **`$imageMode = 'img'`** (default) — images are inserted as `
` tags directly in the content. Simplest, but no caption or extra per-image fields. Since 5.7.0 the `alt` attribute is filled from the asset's Alternative Text and stays in sync with it until an editor changes it in the alt-text modal (which also has a "Sync from asset" button); before 5.7.0 it was not populated from the asset. Since 5.9.0 a dynamic default upload subpath (`{slug}`, `{id}`, anything in braces) no longer throws `InvalidSubpathException` while editing a new element: uploads land in the user's temporary folder and `afterElementSave()` moves them into the resolved folder, creating it on a canonical save and, like the Assets field, only moving a draft's uploads when the folder already exists (craftcms/ckeditor#618).
- **`$imageMode = 'entries'`** — each image is inserted as a nested entry of a configured type. The image entry type carries an Assets field plus any other fields you want (alt text, caption, focal point overrides). Editors interact with images as element chips.
To use entries mode, configure `$imageEntryTypeUid` (the entry type UID) and `$imageFieldUid` (the Assets field UID inside that entry type). The entry type's field layout determines what editors can configure per-image.
```php
$field->imageMode = \craft\ckeditor\Field::IMAGE_MODE_ENTRIES;
$field->imageEntryTypeUid = $imageEntryType->uid;
$field->imageFieldUid = $imageAssetsField->uid;
```
Drag-and-drop uploads work in both modes. In entries mode, dropping an image creates a new nested entry with the dropped file in the configured Assets field (5.0.0; refined in 5.2.0 to work even when the toolbar has only the "Add nested content" button, no dedicated "Insert image").
## Link Configuration (5.0.0+)
The link modal was rebuilt on top of Craft's native Link fields in 5.0.0. The link picker supports the same target types as a regular Link field (URL, email, phone, asset, entry, category, custom element types registered via Link field events).
`$advancedLinkFields` opts in to additional fields in the link modal — labels, target attributes, link types beyond the defaults. The setting is an array of field handles drawn from a Link field's settings schema. Default: empty (basic link picker only).
New links default to the first available link type as of 5.4.0 (typically "Entry"), instead of leaving the type unselected.
## GraphQL Mode (4.8.0+)
Set `$fullGraphqlData = true` to expose the CKEditor field as a structured object in GraphQL, not just the rendered HTML string. Two GraphQL types ship:
- **`craft\ckeditor\gql\CkeditorMarkup`** (default) — returns the rendered markup as a string. Schema field: `myField` returns `String`.
- **`craft\ckeditor\gql\CkeditorData`** (when `$fullGraphqlData = true`) — returns both the markup and the nested entries as queryable elements. Schema fields: `myField.markup` (string), `myField.entries` (array of entries with full GraphQL surface — query their fields like any other entry).
Use `CkeditorData` when the headless front-end needs to render nested entries with the same flexibility as the Twig chunk-iteration pattern. Use `CkeditorMarkup` when the back end has already rendered everything to HTML and the front end just paints it.
## Nested Entries
CKEditor fields can embed Craft entries inline within rich text content — Craft 5's replacement for Matrix-in-rich-text patterns.
### Setup
1. Create entry types for your nested content (e.g., `imageBlock`, `codeBlock`, `quoteBlock`).
2. Edit the CKEditor field → select which entry types are available.
3. The toolbar surfaces entry types two ways:
- **Entry types with icons** get dedicated toolbar buttons (configurable via the entry type's icon setting).
- **Entry types without icons** are grouped under a single "+" / "Add nested content" button.
The 4.8.0-era "'New' Button Label" setting was removed in 5.0.0 — the "+" button is named automatically based on what the field allows.
`Field::entryType()` returns the configured nested-entries entry type when one is set, useful for programmatic introspection.
### Front-End Rendering
Always use the chunk iteration pattern — it handles both plain HTML and nested entries correctly. Direct output (`{{ entry.richContent }}`) only works when the field has zero nested entries; if nested entries exist, it renders as `Array`.
```twig
{% for chunk in entry.richContent %}
{% if chunk.type == 'markup' %}