Hey,

is there any documentation on how to programatically subscribe a user to a content type subscription? I'm trying to find documentation on the Notifications API, but it seems sparse.

what I'm trying to accomplish is:

- user registers for an account
- new users automatically is subscribed to content type of "forum"

which functions should I be looking at? how do I build the subscription object? Any help appreciated! Thanks.

Comments

zeezhao’s picture

In D6, used notifications_save_subscription(). Not sure if its the same in D7.

See these old links for some ideas on usage:
http://drupal.org/node/805742
http://drupal.org/node/791204
http://drupal.org/node/954754

webflo’s picture

Here is a Drupal 7 example.


// Create a content subscription.
$subscription = notifications_subscription('content_thread')
  ->instance()
  ->set_node($node)
  ->set_user($account)
  ->set_interval($interval)
  ->set_method($method);

notifications_save_subscription($subscription);

Take a look at Notifications Rules.
http://drupal.org/project/notifications_rules
#1241652: Port Notifications Rules to Drupal 7
My Sandbox: http://drupal.org/sandbox/webflo/1241646

webflo’s picture

agerard’s picture

if I did this:

/**
* Implementation of hook_user_insert()
*/
function notifications_user_insert(&$edit, $account, $category) {
  $subscription = notifications_subscription('content_type')
  ->instance()
  ->add_field('node:type', 'article')
  ->set_user($account) // acting user
  ->set_method('mail'); // optional
 
  notifications_save_subscription($subscription);
}

but wanted to set_user([new account]) - not 'acting user', which could be admin - is that available to me here?

Thanks again!

agerard’s picture

Anyone?? Still trying to figure out how to auto-add subscription for new users when they are added by admin (since the code above just results in duplicate subs for admin). Apparently this is an issue in 6 as well, where the module supports much more 'out of the box' [see "1) Option for new users to be subscribed by default; 2) Batch add subscriptions for multiple users (per username or role)"]. The module is great and I appreciate the efforts of all contributors, hope to someday have enough knowledge to give something back but right now I'm running as fast as I can just to keep up...I could really use some (more) clues on this question. Thanks!