Returning an object rather than array?
| Project: | Webform |
| Version: | 5.x-2.1.1 |
| Component: | Miscellaneous |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | closed |
Jump to:
In May (using 5.x-2.0), I spent a lot of time trying to figure out how to use the "Confirmation or Redirect" field to do some redirection based on form data submitted after capturing that data, and was happy when I got the following code to work in the Redirect code field:
<?php
include_once(drupal_get_path('module', 'webform') .'/webform_submissions.inc');
$nid = arg(1);
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
$go_here = 'node/' . $submission['data'][3]['value'][0];
drupal_goto($path = $go_here, $query = NULL, $fragment = NULL, $http_response_code = 301);
?>The $submission['data'][3]['value'][0] part of the associative array comes from a form select created in webform. This is perhaps immediately obvious to some.
After upgrading to 5.x-2.1 and 5.x-2.1.1, the forms using that code always redirected to /node. Eventually, I tracked it down to the result of the webform_get_submission() function returning an object rather than an associative array. I don't know if I've got my terminology correct, but in my way of looking at it, there is an array in the object which I needed to access now, rather than an array only.
I've modified the code for redirecting to:
<?php
include_once(drupal_get_path('module', 'webform') .'/webform_submissions.inc');
$nid = arg(1);
$sid = $_GET['sid'];
$submission = webform_get_submission($nid, $sid);
$subsdata = $submission->data;
$go_here = 'node/' . $subsdata[3]['value'][0];
drupal_goto($path = $go_here, $query = NULL, $fragment = NULL, $http_response_code = 301);
?>Two questions:
- Anyone care to critique the code I've done? I've never worked with drupal objects before and would like to know the professional way to deal with them.
- Was the change from arrays to objects documented somewhere which should have been glaringly apparent to me?
Thanks in advance. I like webform a lot!

#1
Let's answer #2 first :)
I shouldn't have changed the webform_get_submission() function from under people in the 2.0 to 2.1 upgrade (new versions are supposed to be released for API changes, that is, a 3.x version). However, since 2.0 just came out, I didn't want to make a whole new release just for this change. Blame my impatience.
The reason this change was made was because webform_get_submissions() (plural) returned an array of submission objects, which webform_get_submission() (singular) returned a submission array. The inconsistency was making me batty. I don't believe the change was documented.
As for #1, the code looks fine but you're doing it in the entirely wrong location. :)
There's a handy page in the Webform handbook (http://drupal.org/handbook/modules/webform) for doing this exact task. It also takes up a lot less processing because you already have the form values available: http://drupal.org/node/257100
#2
Thanks Nate - I saw that posting, but couldn't figure out where to put the code. Even though it says "Additional Processing". I guess it's because the "Webform advanced settings" is collapsed by default so I was just blind to it after a while.
So, the next thing I discovered is that it the value of
$node->webform['confirmation']must be a fully qualified URL e.g. http://domain.dom/xxxx -/node/xxxdoesn't cut it.I will put in a comment on that page you refer to.