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