【问题标题】:Update text in block with Nodejs integration module in drupal 8在 drupal 8 中使用 Nodejs 集成模块更新块中的文本
【发布时间】:2016-07-07 08:43:00
【问题描述】:

以下是我到目前为止所做的以及我正在尝试做的信息。

到目前为止我做了什么

  • 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);

对此有任何帮助,我将不胜感激。如果有人需要,我很乐意提供这方面的更多信息。

【问题讨论】:

    标签: javascript php node.js drupal-modules drupal-8


    【解决方案1】:

    你应该修改 $message 为

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

    以 message.data.body 的形式访问消息正文

    【讨论】:

      猜你喜欢
      • 2019-12-24
      • 2023-03-12
      • 1970-01-01
      • 2016-08-07
      • 2019-04-05
      • 2017-10-27
      • 2020-01-02
      • 1970-01-01
      • 2016-07-14
      相关资源
      最近更新 更多