On this page
Programmatically subscribing users
To programmatically subscribe a user in the Notifications framework, first you need to get the subscription type object, which you can get as long as you know the name of the subscription type (you can tell this by hovering over a subscription link of the correct type).
$substype = notifications_subscription_type_load($substype_name);
Then you need to create a subscription instance, and associate it with the user:
$subscription = $substype->instance();
$subscription->set_user($user);
Then, depending on the subscription type, you will need to set the other necessary objects on the subscription. Usually this is the node, though it could potentially be anything. You can get some clues by looking at the code for the subscription type class that you're using.
$subscription->set_node($node);
Finally, save the subscription and you're done!
$subscription->save();
Here's a complete example, for the case of adding a group content subscription to a group node. (This subscription type is supplied by og_notifications.)
$substype = notifications_subscription_type_load('group_content');
$subscription = $substype->instance()->set_user($user)->set_node($node);
$subscription->save();
Getting a user's subscriptions
To get a user's subscriptions, you need to first instantiate a field with the value you want. Here's an example for getting a user's group_content subscriptions (from OG Notifications module) for a specific organic group.
$gid = 53; //The Organic Group gid
$substype = 'group_content'; //The subscription type
$field = Notifications_Field::build('node:gid', $gid);
$subscriptions = notifications_get_subscriptions(array('uid' => $user->uid ,'type' => $substype), array($field));
Unsubscribing users
If you don't know the subscription id that you want to delete, you can unsubscribe using Notifications_Subscription::delete_multiple(). You need to pass it any conditions on the notifications_subscription table as an array in the first argument, and any field conditions (on the notifications_subscription_field) in the second argument.
Here's an example deleting a group_content subscription for a user for a particular organic group:
$conditions = array(
'uid' => $uid,
'type' => 'group_content',
);
$field_conditions = array(
'node:gid' => $gid,
);
Notifications_Subscription::delete_multiple($conditions, $field_conditions);
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion