1
votes

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.)

Problem

  • 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'
      ),
);
nodejs_enqueue_message($message);
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(message.channel == 'example') {
            $('#nodejs-selector').html(message.data.body);
        }
    }
};
})(jQuery);

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

1

1 Answers

1
votes

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 message.data.body