以下是我到目前为止所做的事情以及我正在尝试做的事情。
到目前为止我做了什么
- Nodejs 模块和 drupal 8 完成。示例模块工作正常。
- 在drupal 8中创建一个简单的模块,由一个名为simple
形式。在其提交函数上,称为nodejs模块函数,将我的消息排队到通道。 - 在nodejs排队消息中创建的Javascript回调函数。
我一直在努力实现的目标
- 提交文本表单时。只是为了更新 drupal 8 中的块。( 用 hello world 更新块内容。
问题
- 我的与nodejs关联的javascript回调没有被调用。
以下是我的代码。
提交函数代码
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]));
}
Javascript回调代码
(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'
),
);
以消息.数据.正文的形式访问邮件正文