-2

I have a little webform to collect email addresses for a newsletter (not handled by my website) with a user language preference and an option to either "subscribe" or "unsubscribe" the email that's being submitted. I want to delete all submissions for a given email if it is submitted with the "unsubscribe" option. So I wrote a module using some webform APIs to do that. It works and deletes the already submitted entries of the webform with an "unsubscribed" email. However I keep receiving these errors:

Notice: Trying to get property of non-object in _webform_filter_values() (line 2946 in /home/test/public_html/sites/all/modules/webform/webform.module).

Notice: Trying to get property of non-object in _webform_filter_values() (line 2952 in /home/test/public_html/sites/all/modules/webform/webform.module).

Warning: Invalid argument supplied for foreach() in _webform_filter_values() (line 2952 in /home/test/public_html/sites/all/modules/webform/webform.module).

Notice: Trying to get property of non-object in _webform_filter_values() (line 2975 in /home/test/public_html/sites/all/modules/webform/webform.module).

Warning: array_diff_key(): Argument #2 is not an array in _webform_filter_values() (line 2975 in /home/test/public_html/sites/all/modules/webform/webform.module).

Warning: Invalid argument supplied for foreach() in _webform_filter_values() (line 2976 in /home/test/public_html/sites/all/modules/webform/webform.module).

Notice: Trying to get property of non-object in _webform_filter_values() (line 2984 in /home/test/public_html/sites/all/modules/webform/webform.module).

This is my module's code:

function unsubscriber_webform_submission_insert($node, $submission) {
    if ($node->nid == 49) { 
        // Find the keys of the components
        foreach ($node->webform['components'] as $key => $value) {
            if ($value['form_key'] == 'email') {
                $email_key = $key;
            }
            if ($value['form_key'] == 'opzione') { // this is the unsubscribe option
                $opzione_key = $key;
            }
        }
        // Get the submitted email
        $usermail = $submission->data[$email_key]['value'][0];

        if ($submission->data[$opzione_key]['value'][0] == 'unsubscribe') {

            $submissions = webform_get_submissions($node->nid);

            foreach ($submissions as $sub) {
                if ($sub->data[$email_key]['value'][0] == $usermail) {
                    webform_submission_delete($node, $sub);
                }
            }
        }

    }
}

I'd like some advice about what could be wrong.

  • The way this is written, it looks like you'll also be deleting the same webform submission that triggers the hook, while it's still being processed. It's hard to say if that's what's causing your issues without doing some debugging, though. – Les Lim Nov 9 '13 at 20:28
  • Yes that was my purpose and finally I understood that deleting the last entry triggers the errors. The hook_webform_submission_insert should fire after the submissions are already written to the database, but for some reason deleting the last one triggers those errors. I ended up hacking the webform.module adding some (hopefully harmless) if conditions in places where the webform code wants to act on some arguments, but they are empty after my module deletes the last entry. Everything seems to work properly and I even implemented another function that clears up duplicates. Any advice welcome. – Federico Nov 10 '13 at 0:57
1

You shouldn't have to resort to hacking webform module. Instead, adjust the hook_webform_submission_insert() implementation so that you're only deleting submissions where $opzione_key == "subscribe". That means you won't have to worry about deleting the current submission. If you need to clean out "unsubscribe" submissions too, write a hook_cron() implementation to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.