From b4523247bf504f901fade4129dcacccd270d38d8 Mon Sep 17 00:00:00 2001
From: Nejc <nejc.koporec@agiledrop.com>
Date: Wed, 28 Feb 2018 14:12:21 +0100
Subject: [PATCH] Added example module

---
 modules/message_notify_example/README.md           | 10 ++++++++++
 .../message_notify_example.info.yml                |  9 +++++++++
 .../message_notify_example.module                  | 23 ++++++++++++++++++++++
 3 files changed, 42 insertions(+)
 create mode 100644 modules/message_notify_example/README.md
 create mode 100644 modules/message_notify_example/message_notify_example.info.yml
 create mode 100644 modules/message_notify_example/message_notify_example.module

diff --git a/modules/message_notify_example/README.md b/modules/message_notify_example/README.md
new file mode 100644
index 0000000..83655a2
--- /dev/null
+++ b/modules/message_notify_example/README.md
@@ -0,0 +1,10 @@
+Message notify example module is intended for developers and site builders
+wanting to understand the key concepts and features of Message notify module.
+
+To see example in action:
+- Enable the Message-notify example module
+- Set up email sending in your drupal site (you can set up core mail module or use contrib modules, such as smtp module)
+- Add a comment to a node, and an email will be sent to the node author
+
+NOTE: By default the module will sent an blank email to the author.You need to configute display view.
+      - Go to MANAGE DISPLAY -> NOTIFY - EMAIL BODY & NOTIFY - EMAIL SUBJECT (Custom display settings for notify must be enabled to see this tabs)
diff --git a/modules/message_notify_example/message_notify_example.info.yml b/modules/message_notify_example/message_notify_example.info.yml
new file mode 100644
index 0000000..83e591c
--- /dev/null
+++ b/modules/message_notify_example/message_notify_example.info.yml
@@ -0,0 +1,9 @@
+name: 'Message notify example'
+description: 'Message notify example'
+core: 8.x
+package: Message
+dependencies:
+  - message
+  - message_notify
+  - message_example
+type: module
\ No newline at end of file
diff --git a/modules/message_notify_example/message_notify_example.module b/modules/message_notify_example/message_notify_example.module
new file mode 100644
index 0000000..de07115
--- /dev/null
+++ b/modules/message_notify_example/message_notify_example.module
@@ -0,0 +1,23 @@
+<?php
+
+use Drupal\comment\Entity\Comment;
+use Drupal\message\Entity\Message;
+
+/**
+ * Implements hook_comment_insert().
+ *
+ * Send a message to the node author when a new comment is created.
+ */
+function message_notify_example_comment_insert(Comment $comment) {
+  // Get node where comment was posted.
+  $node = $comment->get('entity_id')->first()->get('entity')->getTarget()->getValue();
+  // Call message notify service.
+  $notifier = \Drupal::service('message_notify.sender');
+  // Create a message with node author as message creator.
+  $message = Message::create(['template' => 'example_create_comment', 'uid' => $node->getOwnerId()]);
+  $message->set('field_comment_reference', $comment);
+  $message->set('field_published', $comment->isPublished());
+  $message->save();
+  // Send message to message creator(in this case = node author).
+  $notifier->send($message);
+}
\ No newline at end of file
--
2.15.1
