Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用drupal 8中的Nodejs集成模块更新块中的文本_Javascript_Php_Node.js_Drupal Modules_Drupal 8 - Fatal编程技术网

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的形式访问消息正文