I am writing a module that makes use of the notifications api. It provides a subscription that never has 'fields'.

However, when I go to save them I get:

warning: Invalid argument supplied for foreach() in notifications.module on line 379

Attached is a patch that makes the 'foreach fields as name => value' section conditional on there being fields

(sorry - patch cleans up line endings again) Important part is (line 189 of the patch):

-  foreach ($subscription->fields as $name => $value) {
-    db_query("INSERT INTO {notifications_fields} (sid, field, value) VALUES(%d, '%s', '%s')", $subscription->sid, $name, $value);
+  if (is_array($subscription->fields)) {
+    foreach ($subscription->fields as $name => $value) {
+      db_query("INSERT INTO {notifications_fields} (sid, field, value) VALUES(%d, '%s', '%s')", $subscription->sid, $name, $value);
+    }
CommentFileSizeAuthor
notifications.module.patch12.35 KBandremolnar

Comments

jose reyero’s picture

Status: Needs review » Fixed

We are not really contemplating such case and more of the api may fail for subscriptions with no fields.

(Unless we actually get a plug-in which really uses that)

Anyway that won't impact anything else and we try this to be as reusable as possible for everybody, so the patch is in.

andremolnar’s picture

Thanks.

Here is the use case:

The plug-in I'm working on is specific to a site, so not very useful to anyone else (otherwise I'd be glad to share it). But, basically, the subscription events are triggered by queries that run on cron.
The subscription type has no specific fields.
subscription_save is called as each new user signs up without fields.

Anonymous’s picture

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.