CKEditor5 Plugin Pack Read-Only Mode Patch

Problem

When CKEditor5 text fields are set to `disabled` or `readonly`, core CKEditor5 plugins properly disable their toolbar buttons, but plugins from the `ckeditor5_plugin_pack` module (such as `fullscreen`, `findAndReplace`, `emoji`, `fontColor`, etc.) remain functional and their toolbar buttons are not disabled.

Solution

This patch adds JavaScript that ensures all CKEditor5 Plugin Pack plugins respect the read-only mode when fields are disabled or readonly.

Files Changed

1. js/ckeditor5_plugin_pack.readonly.js (new file)
- JavaScript behavior that detects disabled/readonly textareas
- Enforces read-only mode on CKEditor5 editors
- Disables all toolbar buttons including plugin pack plugins

2. ckeditor5_plugin_pack.libraries.yml
- Adds a new readonly_support library definition
- Includes the new JavaScript file with proper dependencies

3. ckeditor5_plugin_pack.module
- Implements hook_page_attachments_alter() to attach the readonly support library

How to Apply

Using Composer (Recommended)

If you're using composer-patches or cweagans/composer-patches:

1. Add the patch to your composer.json:

{
  "extra": {
    "patches": {
      "drupal/ckeditor5_plugin_pack": {
        "Fix readonly mode for plugin pack plugins": "patches/ckeditor5_plugin_pack_readonly.patch"
      }
    }
  }
}

2. Place the patch file in your patches/ directory (or adjust the path accordingly)

3. Run composer update drupal/ckeditor5_plugin_pack

Manual Application

1. Navigate to the module directory:

cd docroot/modules/contrib/ckeditor5_plugin_pack

2. Apply the patch:

patch -p1 < /path/to/ckeditor5_plugin_pack_readonly.patch

3. Clear Drupal cache:

drush cr

How It Works

1. The JavaScript behavior (Drupal.behaviors.ckeditor5PluginPackReadonly) runs on page load and AJAX updates
2. It finds all textareas with CKEditor5 attached that have disabled or readonly attributes
3. For each such textarea, it:
- Gets the CKEditor5 editor instance
- Enables read-only mode if not already enabled
- Explicitly disables all toolbar buttons (including plugin pack plugins)
4. If the editor hasn't initialized yet, it waits up to 2 seconds for it to become available

Testing

After applying the patch:

1. Create or edit a node with a CKEditor5 field
2. Set the field to disabled or readonly using form alter hooks:

$form['field_name']['#attributes']['readonly'] = 'readonly';
$form['field_name']['#disabled'] = TRUE;

3. Verify that:
- All toolbar buttons (including plugin pack plugins) are disabled
- The editor content is not editable
- Plugin pack plugins like fullscreen, findAndReplace, emoji, fontColor are non-functional

Compatibility

- Drupal Version: 10.x, 11.x
- CKEditor5 Plugin Pack: 1.4+
- CKEditor5 Core Module: Any version

Notes

- The patch works alongside the core CKEditor5 module's read-only mode handling
- It enhances the core functionality to ensure plugin pack plugins also respect read-only mode
- The library is attached on all pages, but the JavaScript only activates when CKEditor5 editors with disabled/readonly attributes are detected

Related Issues

This patch addresses the issue where:
- Core CKEditor5 plugins: ✅ Properly disabled in read-only mode
- Plugin Pack plugins: ❌ Not properly disabled in read-only mode (before patch)
- Plugin Pack plugins: ✅ Properly disabled in read-only mode (after patch)

Comments

ankitjhakal created an issue. See original summary.

ankitjhakal’s picture

Assigned: ankitjhakal » Unassigned
Status: Active » Needs review
salmonek’s picture

Issue summary: View changes

Updated summary formatting.

Thank you Ankit for reporting the issue and patch I'll take a look at this in a new year.

salmonek’s picture

Status: Needs review » Postponed (maintainer needs more info)
StatusFileSize
new25.83 KB

I wanted to quckly reproduce the issue before I leave. I checked the issue went with three approaches in a hook_form_alter:

  $form["body"]["#attributes"]['disabled'] = 'disabled'; // doesn't work at all - everything including core plugins is enabled
  $form["body"]['widget'][0]["#attributes"]['disabled'] = 'disabled'; // works as expected
  $form["body"]["#disabled"] = TRUE; // works as expected

The two latter lines are working fine without applying a patch:
asd
All plugins that can change content are disabled. Remaining enabled plugins are not able to change the content (find and replace only allows to find a text) including the "Show blocks" which is available in Drupal core.
For Fullscreen it is also expected to be available to use since the plugin itself does not change the content.

I was testing on Drupal 11.3 and CKEditor 5 Plugin Pack 1.4.2
If you have an issue with Emoji and Font plugins please share more info on versions which you use. In my case those were correctly disabled.

mdusza’s picture

Status: Postponed (maintainer needs more info) » Closed (works as designed)

Hi ankitjhakal,

Closing the issue due to no activity for more than a month - feel free to reopen if you'd like to discuss this further.

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.