How to hook into elgg's notifications?

Hi, Its me again.

I want to hook into the elgg notification object before its send. Basically I want to get the recpient details, the notification message subject and body. I tried to hook into 'prepare', 'notification' hook, but I failed to retrieve the notification object details. Any idea?

 

 

  • Which version of Elgg are you using? Best would be you would post your code to allow for debugging andd suggest improvements.

  • Hi, I am using Elgg 3.3.6

    So far i havent reached anywhere.

    elgg_register_plugin_hook_handler('prepare', 'notification', 'my_notification_hook');
     
    function my_notification_hook(\Elgg\Hook $hook) {
        $recipient = $hook->getParam('recipient');
        if($recipient){
            // I want to get the notification subject & body here
        }
        return;
    }
  • Contents of the notification message should be defined with the 'prepare', 'notification:[action]:[type]:[subtype]' hook.

    There's good example to help you with this task.

  • @RVR: that is for preparing a new notification. What I want is to get a notification object before it is sending from the server and modify it / extract some data before it is send.

  • elgg_register_plugin_hook_handler('prepare', 'notification', 'my_notification_hook');
         
    function my_notification_hook(\Elgg\Hook $hook) {
        $recipient = $hook->getParam('recipient');
        $notification = $hook->getValue();
    
        if ($recipient) {
            $subject = $notification->subject;
            $body = $notification->body;
            
            // now you can alter subject and body however you want to
            
            // assign the changes to the notification object
            $notification->subject = 'modified subject';
            $notification->body = 'modified body';
        }
        
        // return the notification object at least if altered if altered
        return $notification;
    }

    I've looked at http://learn.elgg.org/en/stable/guides/hooks-list.html#notifications and the file vendor/elgg/elgg/engine/classes/Elgg/Notifications/Notification.php to figure that out. I hope it helps.

  • Thanks iionly. That's what I was looking for. Let me give a try.

Beginning Developers

Beginning Developers

This space is for newcomers, who wish to build a new plugin or to customize an existing one to their liking