Community Documentation

Adding and displaying your own notice from your custom module

Last updated March 20, 2008. Created by kenuck on March 20, 2008.
Log in to edit this page.

Lets assume you have a module that creates a cake, we'll call this module 'cake'.
Everytime a user adds a cake you want that user's friends to know and then be displayed on the Notice Feed or Mini Feed.

Action: When user adds new cake
After doing the insert into the database, you'll want to record this into the notice board.

<?php
   
If (module_exists(“notice”)){
       
$new_id = db_last_insert_id('cake_table','cake_id');
       
$notice_array = array(
           
'nb_type'    => NOTICE_NORMAL,
           
'module_type'    => “cake”,
           
'source_uid'    => $user_id, // the person that just added the cake
           
'prime_id'    => $new_id,
           
'op'        => “cake_add”,
        );
       
noticeapi_notice_add(“cake”,$notice_array);
    }
?>

Now we want to be able to display the action in a feed. For this we need to hook into notice

<?php
// $record will contain the information we just added in the above example .
function cake_notice($op, $type, $record){
    switch(
$op){
        case
'feed':
           
$SQL = “SELECT C.*
                   
FROM {cake_table} C
                    WHERE C
.cake_id = %d
            “
;
           
$record = db_fetch_object(db_query($SQL,$record->prime_id));
           
$output .= '<div claass=”notice-'.$record->module_type.'”>';
           
$output .= 'somebody added a new cake called '. $record->title;
           
$output .= '</div>';
            return
$output;
            break;
    };
}
?>

In other words, use the notice function to insert your own information and then use the hook to format the feed display for your module type.

About this page

Drupal version
Drupal 6.x

Site Building Guide

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here