I'm trying to get a node-type subscription created on accounts after registration by using hook_user_insert(). Based on your code here, I've been trying the following code.

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

However, I keep getting the following error on user registration:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS NULL ) AND( (f1.type = 'node:type') AND (f1.value = 'article') )' at line 3: SELECT s.sid AS sid FROM {notifications_subscription} s INNER JOIN {notifications_subscription_fields} f0 ON s.sid = f0.sid INNER JOIN {notifications_subscription_fields} f1 ON s.sid = f1.sid WHERE (s.uid = :db_condition_placeholder_0) AND (s.type = :db_condition_placeholder_1) AND (s.send_interval = :db_condition_placeholder_2) AND (s.module = :db_condition_placeholder_3) AND (conditions = :db_condition_placeholder_4) AND ( IS NULL ) AND( (f1.type = :db_condition_placeholder_5) AND (f1.value = :db_condition_placeholder_6) ); Array ( [:db_condition_placeholder_0] => 30 [:db_condition_placeholder_1] => content_thread [:db_condition_placeholder_2] => -1 [:db_condition_placeholder_3] => notifications [:db_condition_placeholder_4] => 2 [:db_condition_placeholder_5] => node:type [:db_condition_placeholder_6] => article ) in Notifications_Subscription::load_multiple() (line 137 of /Users/kris/Documents/CODE/_sandbox/d7/sites/all/modules/contrib/notifications/notifications.subscription.inc).

It makes no difference whether I use default or send as the value for set_method, or If I remove it altogether.

Comments

kriskhaira’s picture

Issue summary: View changes

Formatting of error message

kriskhaira’s picture

Title: Getting error trying to get content subscriptions to work with hook_user_insert() » Error trying to set up node-type auto-subscriptions using hook_user_insert()
webflo’s picture

Status: Active » Closed (works as designed)

Content-Type subscription is "content_type" not "content_thread". This snippet works flawless.

/**
 * 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);
}
kriskhaira’s picture

Thanks! That worked, and I also managed to make it work for multiple custom content types by doing this:

function notifications_autosubscribe_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);
  // repeat:
  $subscription = notifications_subscription('content_type')
  ->instance()
  ->add_field('node:type', 'page')
  ->set_user($account) // acting user
  ->set_method('mail'); // optional
  notifications_save_subscription($subscription);
}
agerard’s picture

This looks like exactly what I need, but not having had the guts to try to figure out where hook_anything_goes(), I'm at a loss where this would be placed. - ok, got that, but in cases where admin is creating accounts, ideally this would be the new user, not admin. Is that going to be possible, or does that id not exist yet?

kriskhaira’s picture

agerard, you might want to look at the values being inserted through the form and use the value for the user ID. Look here for clues.
http://drupal.org/node/1195380

sam6’s picture

Sorry for the naïve question (I am coming from Wordpress, and Drupal development is quite different): To which file should I add that code, in order to have that functionality?

Thanks!

sam6’s picture

Ok, I got it, the code by webflo:

/**
 * 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);
}

needs to be added into the file notifications_user.module

Also, the code by kriskhaira :

<?php
function notifications_autosubscribe_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);
  // repeat:
  $subscription = notifications_subscription('content_type')
  ->instance()
  ->add_field('node:type', 'page')
  ->set_user($account) // acting user
  ->set_method('mail'); // optional
  notifications_save_subscription($subscription);
}
?>

did not work for me. The following worked:


/**
 * 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);

  // repeat:
  $subscription = notifications_subscription('content_type')
  ->instance()
  ->add_field('node:type', 'page')
  ->set_user($account) // acting user
  ->set_method('mail'); // optional
  notifications_save_subscription($subscription);


}

Boy, it took me the whole day to find out! Drupal is so different from Wordpress. Kind of harder to learn, too. I guess it comes with the power :) Just wish I could have had at least some directions.

Thank you anyway!

sam6’s picture

Issue summary: View changes

Added a note about the set_method value