3

My team is working on a simple message notification using the message stack but a bit tricky to implement.

The goal is when a user is subscribed to content, a notification message will be delivered to the user's email address.

We are trying to follow this steps, doing the configuration in the browser. But the last step, we think should be added manually in a certain file as mentioned in this comment.

Questions:

  • Where should I add the code to properly send the message into email?
  • Is there a need to create a custom module to achieve it?
4
  • In D7 this would be a peace of cake (using the Message stack, combined with Rules, and zero custom code). Sure you want to do this in D8? Jan 11, 2018 at 9:14
  • Where should I add the code to properly send the message into email? Depends of when you would like to send the notification message. When a node is updated? When a new node is created? or both?
    – No Sssweat
    Jan 11, 2018 at 10:08
  • Yes, our site is running in D8. Jan 12, 2018 at 0:50
  • @NoSssweat Its either, updated, created or both. Can you site one example, im not very sure which file I will append it. Jan 12, 2018 at 1:38

1 Answer 1

5

Hooks go in the .module file, the only hooks that can also work in the .theme file are preprocess and alter hooks.

So if you want the message to be sent when a node is created or updated, use hook_ENTITY_TYPE_presave.

Create a custom module, lets call it "Send Message"

send_message.info.yml

name: 'Send Message'
description: 'Sends the message when a node of Content type Article is created or updated.'
core: 8.x
package: Message
dependencies:
  - message
type: module

send_message.module

<?php

use Drupal\node\NodeInterface;
use Drupal\message\Entity\Message;

/*
 * Implements hook_ENTITY_TYPE_presave()
 */

function send_message_node_presave(NodeInterface $node) {

   if ($node->getType() == 'article') { // checks if content type is article
      $message = Message::create(['template' => 'foo_message_type', 'uid' => $node->getOwnerId()]);
      $message->set('field_node_ref', $node);
      $message->save();
      $notifier = \Drupal::service('message_notify.sender');
      $notifier->send($message);
   }

}
8
  • Thank you for a very detailed response will try that and accept the answer once working. Jan 12, 2018 at 4:30
  • Getting this error message TypeError: Argument 1 passed to Drupal\message_notify\MessageNotifier::send() must be an instance of Drupal\message\MessageInterface, string given, called in /opt/app-root/src/modules/custom/send_message/send_message.module on line 14 in Drupal\message_notify\MessageNotifier->send() (line 48 of modules/message_notify/src/MessageNotifier.php). Jan 12, 2018 at 5:21
  • seems like it doesnt need to be a string Jan 12, 2018 at 5:25
  • As per drupal.org/node/2180171 looks like you need to create a message of some template type.
    – No Sssweat
    Jan 12, 2018 at 5:31
  • 1
    @NoSssweat don' be surprised if some day (soon?) a competing answer using D8Rules will be added by "somebody" ... Jan 12, 2018 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.