From b6ce6cd25824f070b62eb81fcc6da7349f149f17 Mon Sep 17 00:00:00 2001
From: Florian Weber <florian@webflo.org>
Date: Mon, 3 Oct 2011 13:24:14 +0200
Subject: [PATCH] Issue #1275936: Implements hook_notifications_message_alter
 again.

---
 notifications.event.inc    |    1 +
 notifications.message.inc  |   22 ++++++++++------------
 notifications.template.inc |   10 ++++++++++
 3 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/notifications.event.inc b/notifications.event.inc
index 0bde828..ced8931 100644
--- a/notifications.event.inc
+++ b/notifications.event.inc
@@ -526,6 +526,7 @@ class Notifications_Event extends Notifications_Entity {
     // Template to be rendered for each user
     $template = $this->get_template();
     $message = $template->build_message();
+    $message->add_event($this, array_keys($subscriptions));
     // Keep track of users sent so we don't repeat
     $results = array('skip' => array(), 'sent' => array(), 'success' => array(), 'error' => array());
     foreach ($subscriptions as $subscription) {
diff --git a/notifications.message.inc b/notifications.message.inc
index fbcbaed..c997f21 100644
--- a/notifications.message.inc
+++ b/notifications.message.inc
@@ -27,21 +27,19 @@ class Notifications_Message extends Messaging_Message {
   // Build method used for this
   public $build_method;
 
-
   /**
    * Check parameters and go for alter hook
    */
-  public function message_build() {
-    if (empty($this->built)) {
-      // Provide a specific notifications alter hook
-      drupal_alter('notifications_message', $this);
-      // Clean up Notifications objects so we don't move too much data around (Sometimes the full object gets serialized)
-      unset($this->events);
-      unset($this->events_subscriptions);
-      unset($this->subscriptions);
-      unset($this->text_parts);
-    }
-    return parent::message_build();
+  protected function do_build() {
+    parent::do_build();
+    drupal_alter('notifications_message', $this);
+    // Clean up Notifications objects so we don't move too much data around (Sometimes the full object gets serialized)
+    unset($this->events);
+    unset($this->events_subscriptions);
+    unset($this->subscriptions);
+    unset($this->text_parts);
+    // The message must be built, without errors and not for discarding
+    return $this->check_status();
   }
 
   /**
diff --git a/notifications.template.inc b/notifications.template.inc
index b73f878..0628527 100644
--- a/notifications.template.inc
+++ b/notifications.template.inc
@@ -22,6 +22,16 @@ class Notifications_Message_Template extends Messaging_Message_Template {
   function __construct($info = NULL) {
     $this->info = $info;
   }
+
+  /**
+   * Get Message_Object with this template linked
+   */
+  public function build_message($options = array()) {
+    $message = new Notifications_Message($options);
+    $message->set_template($this);
+    return $message;
+  }
+
   /**
    * Set notifications event
    */
-- 
1.7.6

