使用 Drupal 8 中的 Nodejs 集成模块更新块中的文本

Update text in block with Nodejs integration module in drupal 8

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

到目前为止我做了什么

我一直在努力实现的目标

问题

下面是我的代码。

提交功能代​​码

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

访问邮件正文