在Drupal 9中为CKEditor 5添加特殊字符遇插件找不到错误
Drupal 9 中 CKEditor 5 相关问题解决方案
一、添加特殊字符的实现方式
Drupal 9 有专用钩子来扩展 CKEditor 5,无需直接照搬纯前端官方文档,具体操作如下:
- 启用官方特殊字符插件:通过
hook_ckeditor5_plugin_info_alter()钩子修改配置,启用SpecialCharacters插件,还可指定默认显示的字符组。示例代码:
function mymodule_ckeditor5_plugin_info_alter(array &$plugins) { if (isset($plugins['specialCharacters'])) { $plugins['specialCharacters']['enabled'] = TRUE; // 配置默认加载的字符组,比如数学符号、箭头等 $plugins['specialCharacters']['configuration']['groups'] = ['math', 'arrows']; } }
- 自定义字符组:若需新增专属特殊字符,可编写自定义 CKEditor 5 插件,通过
hook_ckeditor5_plugins()注册插件后,在 JS 代码中扩展SpecialCharacters的字符组。 - 后台配置调整:在 Drupal 文本格式设置中,找到对应 CKEditor 5 编辑器,启用「特殊字符」按钮并添加到工具栏。
二、解决 CKEditorError: plugincollection-plugin-not-found {"plugin":null} 错误
该错误多因自定义插件注册或构建环节出错,按以下步骤排查:
- 检查插件注册钩子:确保
hook_ckeditor5_plugins()中插件的id与 JS 里注册的插件名称完全一致,class路径和library定义准确。示例:
function mymodule_ckeditor5_plugins() { return [ 'mymodule_custom_plugin' => [ 'id' => 'mymoduleCustomPlugin', // 必须匹配JS插件名 'class' => '\Drupal\mymodule\Plugin\CKEditor5Plugin\MyCustomPlugin', 'library' => 'mymodule/ckeditor5.custom_plugin', 'label' => t('My Custom Plugin'), 'elements' => [], ], ]; }
- 验证构建文件:确认
yarn run build生成的编译后 JS 文件(通常在js/build/目录)已正确生成,且模块的libraries.yml正确引用:
ckeditor5.custom_plugin: js: js/build/custom-plugin.js: {} dependencies: - ckeditor5/ckeditor5 - core/drupal
- 检查 JS 插件代码:确保插件正确继承 CKEditor 5 的
Plugin类,且注册名称与钩子一致。示例:
import { Plugin } from 'ckeditor5/src/core'; export default class MyCustomPlugin extends Plugin { static get pluginName() { return 'mymoduleCustomPlugin'; // 与PHP钩子id一致 } init() { // 插件初始化逻辑 } } ClassicEditor.builtinPlugins.push(MyCustomPlugin);
- 清除缓存:执行
drush cr或后台清除所有缓存,确保新配置被加载。 - 检查模块依赖:确认
.info.yml中已声明依赖ckeditor5:
name: 'My Custom Module' type: module description: 'Custom CKEditor 5 plugin for Drupal' core_version_requirement: ^9 || ^10 dependencies: - ckeditor5:ckeditor5
内容的提问来源于stack exchange,提问作者Lukáš Sýkora
相关产品推荐
相关产品推荐

