FaNtAMode FaNtAMode - 1 year ago 106
Node.js Question

Update text in block with Nodejs integration module in drupal 8

Below is information what I have done till now and what I am trying to do.

What I have done till now

  • Nodejs module and drupal 8 done. Example modules are working fine.

  • Created a simple module in drupal 8 consists of a form called simple

    form. On its submit function, called nodejs module function to enqueue my message to the channel.

  • Javascript callback function created defined in nodejs enqueue message.

What I have been trying to acheive

  • When submitting a text form. Just to update the block in drupal 8.( Update the block content with hello world.)


  • My javascript callbacks associated with nodejs aren't being called.

Below are my codes.

Submit Function Code

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'
drupal_set_message(t('You specified a title of %title.', ['%title' => $title]));

Javascript callback code

(function ($) {
Drupal.Nodejs.callbacks.example = {
//grab the message and inject into the header
callback: function (message) {
console.log('simple example');
if( == 'example') {

Any help on this, I would be very grateful. I would love to provide more information on this, if anyone needed.

Answer Source

You should modify $message as

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

To access the message body as

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download