Ckeditor 字数统计插件,用于针对特定字段



我正在使用CKEditor-所见即所得的HTML编辑器Drupal 7模块和字数统计插件。我已经在 ckeditor 插件目录中添加了字数统计插件文件并让它工作,但是我的配置最大字数和字符数等对于所有字段都是相同的。我想为特定字段指定不同的最大字数和字符数。这是我的配置:

ckeditor.config.js

使用CKEDITOR.replace 我做了以下操作:

CKEDITOR.replace('edit-body-und-0-summary',
{
extraPlugins: 'wordcount',
wordcount: {
showCharCount: true,
maxCharCount: 123
}
});

这有效,但在刷新页面后,它又回到了全局最大字数和字符数。Textrea IDedit-body-und-0-summary,但此标记在页面的其他位置重复出现。

我如何向文本区域标签添加唯一 ID 并为不同的字段提供不同的字数,例如 "摘要"和"正文"字段

我想通了,希望这对其他人有所帮助。

我在我的 Adminimal 子主题中添加了以下内容,以将类添加到摘要文本区域:

function adminimal_sub_form_alter(&$form, &$form_state, $form_id) { 
if (isset($form['#node']) && $form['#node']->type == 'article') {
// Add class to body textarea for various content types. This is used for the Ckeditor wordcount plugin to target summary textarea. 
$form['body']['und']['0']['summary']['#attributes']['class']['0'] = 'article-summary';
}
}

然后下载了 Ckeditor 4 字数统计插件并将其保存在插件目录(sites/all/libraries/ckeditor/plugins(中。

在Drupal UI中,我转到了CKEditor过滤HTML(编辑(>配置,在高级选项下的自定义Javascript配置中,我为我的ckeditor.config添加了一条路径.js(config.customConfig = '/sites/all/themes/global/js/ckeditor.config.js';)。

这是我的ckeditor.config.js:

CKEDITOR.editorConfig = function(config) {
if (this.element.hasClass('article-summary')) {
config.wordcount = {
showCharCount: true,
maxCharCount: 170
}
}
// Make CKEditor's edit area as high as the textarea would be.
if (this.element.$.rows > 0) {
config.height = this.element.$.rows * 20 + 'px';
}
}

在这里,我可以设置最大字数并加载更多,但对于这个项目,只需要最大字数。这样,如果Drupal Ckeditor模块得到更新,这个文件就不会受到影响。

最新更新