我使用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?
发布于 2018-11-06 10:15:17
最后,我能够使用自定义Ajax命令解决这个问题:
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;}公共函数呈现(){返回;}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 });};};$response->addCommand(new \Drupal\b47_custom_authors\Ajax\B47CustomAjax('textarea'.$fieldid,[$fieldid, $texttobeinserted]));
$fieldid是文本区域的id,@texttobeinserted是我在ckeditor中想要的文本的完整html。
这现在允许我通过我的AjaxResponse动态更新基于文本的ckeditor。默认情况下,textfield是隐藏的,根据另一个字段中的输入,我获取需要放置在textarea/ckeditor中的文本。然后还会显示表单字段。
现在起作用了。
https://drupal.stackexchange.com/questions/271760
复制相似问题