Javascript 使用drupal 8中的Nodejs集成模块更新块中的文本
下面是我到目前为止所做的事情和我正在努力做的事情 我到目前为止所做的事Javascript 使用drupal 8中的Nodejs集成模块更新块中的文本,javascript,php,node.js,drupal-modules,drupal-8,Javascript,Php,Node.js,Drupal Modules,Drupal 8,下面是我到目前为止所做的事情和我正在努力做的事情 我到目前为止所做的事 Nodejs模块和drupal8完成。示例模块运行良好 在drupal 8中创建的简单模块由一个名为simple的表单组成 形式。在其submit函数上,调用nodejs module函数将我的消息排入通道队列 在nodejs排队消息中定义的Javascript回调函数 我一直想实现的 public function submitForm(array &$form, FormStateInterface $for
- Nodejs模块和drupal8完成。示例模块运行良好
- 在drupal 8中创建的简单模块由一个名为simple的表单组成
形式。在其submit函数上,调用nodejs module函数将我的消息排入通道队列 - 在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]));
}
(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);
- 提交文本表单时。只需更新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]));
}
(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);
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]));
}
(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的形式访问消息正文