首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用CKEditor API更新CKEditor文本小部件

如何使用CKEditor API更新CKEditor文本小部件
EN

Drupal用户
提问于 2018-10-30 07:48:59
回答 1查看 1.3K关注 0票数 0

我使用Drupal 8's Ajax API根据另一个字段中的输入更新表单字段。我要更新的字段之一是text_format字段,该字段使用Drupal的核心程序。

如果我正确理解了CKEditor API,我应该能够使用setData()方法(https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_.method#方法-setData)更新ckeditor小部件中的文本。ckeditor实例应该是textarea id,并且我可以在我的AjaxResponse函数中很容易地识别它。

但是,我不知道如何使用Drupal的invokeCommand()来使用CKEditor API?

EN

回答 1

Drupal用户

发布于 2018-11-06 10:15:17

最后,我能够使用自定义Ajax命令解决这个问题:

  1. 在我们的自定义模块(称为b47_custom_authors)中,我们在src/Ajax/B47CustomAjax.php /** * @file *中创建了一个自定义ajax命令,其中包含*/命名空间Drupal\b47_custom_authors\Ajax;使用Drupal\Core\Ajax\ CommandInterface;类B47CustomAjax实现CommandInterface{ /** * CSS选择器字符串。**如果命令是对来自#ajax表单元素的请求的响应,那么*该值可以为NULL。** @var string */受保护的$selector;/** *是传递给方法的参数的可选列表。** @var数组*/受保护的$arguments;/** *构造b47customajax对象。** @param字符串$selector * jQuery选择器。* @param数组$arguments *是传递给该方法的参数的可选数组。*/公共函数__construct($selector,数组$arguments = []) { $this->selector = $selector;$this->参数= $arguments;}公共函数呈现(){返回;}
  2. 自定义js (在js/b47customajax.js中)如下: Drupal.AjaxCommands.prototype.b47customajax = function (ajax,response) { // response是一个带有键// response.command的数组:命令(b47customajax) // response.selector:要使用的选择器(NULL) // response.args:要作为数组传递的参数: 0: ckeditor实例;1:要传递的文本(数据)= response.args;var ajaxckeditordata = response.args;var编辑器;var实例= CKEDITOR.instances;如果(实例){ //试图调用setData方法:https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_.method#方法-setData编辑器= instance.setData( ajaxckeditordata,{ callback: function() { this.checkDirty();// true });};};
  3. 在ajax回调函数中,我通过以下方式调用我的自定义ajax:

$response->addCommand(new \Drupal\b47_custom_authors\Ajax\B47CustomAjax('textarea'.$fieldid,[$fieldid, $texttobeinserted]));

$fieldid是文本区域的id,@texttobeinserted是我在ckeditor中想要的文本的完整html。

这现在允许我通过我的AjaxResponse动态更新基于文本的ckeditor。默认情况下,textfield是隐藏的,根据另一个字段中的输入,我获取需要放置在textarea/ckeditor中的文本。然后还会显示表单字段。

现在起作用了。

票数 1
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/271760

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档