用drupal 8中的Nodejs集成模块更新文本

以下是我迄今为止所做的信息以及我正在尝试做的事情。

我到现在为止所做的

  • Nodejs模块和drupal 8完成。 示例模块工作正常。
  • 在drupal 8中创build一个简单的模块由一个简单的表单组成
    形成。 在其提交函数中,调用nodejs模块函数将我的消息排入通道。
  • 创build的Javascriptcallback函数在nodejs入队消息中定义。

我一直在努力达到什么目的

  • 提交文字表单时。 只是为了更新drupal 8中的块(用hello world更新块内容。)

问题

  • 与nodejs相关的javascriptcallback没有被调用。

以下是我的代码。

提交function代​​码

public function submitForm(array &$form, FormStateInterface $form_state) { /*$message->broadcast = TRUE; * This would normally be replaced by code that actually does something * with the title. */ $title = $form_state->getValue('title'); $message = (object) array( 'channel' => 'example', 'broadcast' => TRUE, 'callback' => 'example', 'data' => array( 'message' => 'Hello World' ), ); nodejs_enqueue_message($message); drupal_set_message(t('You specified a title of %title.', ['%title' => $title])); } 

Javascriptcallback代码

 (function ($) { Drupal.Nodejs.callbacks.example = { //grab the message and inject into the header callback: function (message) { console.log('simple example'); if(message.channel == 'example') { $('#nodejs-selector').html(message.data.body); } } }; })(jQuery); 

任何帮助,我将非常感激。 如果有人需要,我想提供更多的信息。

您应该将$ message修改为

 $message = (object) array( 'channel' => 'example', 'broadcast' => TRUE, 'callback' => 'example', 'data' => array( 'body' => 'Hello World' ), ); 

以message.data.body身份访问邮件正文