3

I'm using Webform 6x 3.11 and have passed a nodeID into a hidden field called hidden_key. I have created a custom module to implement hook_form_alter() whereby I am trying to add additional data (along with the data that is being submitted) to the submission results.

This additional data is based on some calculations performed on the value in the hidden field, ie: getting some details about the node based on the nodeID. Here's what I currently have:

<?php
    function custom_form_proc_form_alter(&$form, &$form_state, $form_id) {
        if ($form_id == 'webform_client_form_33') {
            $form['#submit'][] = 'custom_form_proc_submit';
        }
    }

    function custom_form_proc_submit($form, &$form_state) {
        $node = node_load($form_state['values']['hidden_key']);
        $pool_title = $node->title;
        $pool_category = $node->field_category[0]['value'];

        // ?????
    }
?>

Where you see my ????? is where I am stuck. Along with whatever data gets submitted with the form, I would like to add these 2 custom fields to the submission results so that it looks like:

Fullname: Mr So-and-So
Email Address: someone@test.com
Pool Category: $pool_category
Pool Title: $pool_title
Availability: Yes

I would really appreciate your help! Thank you!

3 Answers 3

4

For webform submission it is best to use hook_webform_submission_insert($node, $submission).
$submission->data will contain all the submitted values arranged against form components ids.

See my implementation how I arranged a webform as user register form.

function user_register_webform_submission_insert($node, $submission){
// Load information about form components
$comps = $node->webform['components'];
$fields = array();

//Extract the component info to get form_key=>cid to resolve relation
foreach($comps as $cid => $comp_info){
    $fields[$comp_info['form_key']] = $cid;
}

// Load the submitted data (field values)
$data = $submission->data;

//Prepare a user info
$user = array(
    'mail' => $data[$fields['mail']]['value'][0],
    'name' => $data[$fields['name']]['value'][0],
    'pass' => user_password(8),
    'status' => 1,
    'init' => $data[$fields['mail']]['value'][0],
    'roles' => array(2, 3),
);

$account = user_save(null, $user);  
}
4
  • Hi Shoaib. Sorry Im a bit of newbie when it comes to Drupal custom dev. Could you explain a bit more? I'm really trying to grasp what you have provided in the context of what I'm trying to achieve. What do I need to do to send this custom array to the webform result table in the db? thanks for your time!
    – maGz
    Jul 26, 2011 at 9:13
  • @Shoaib I believe hook_webform_submission_insert is called after saving to the db, so I'm not sure it would really fit maGz's situation.
    – Andy
    Jul 26, 2011 at 11:13
  • @Andy you are right. Jul 26, 2011 at 13:34
  • I got this working, but can only submit my answer in another 1 hour due to my low rep. Thanks for all the help though
    – maGz
    Jul 26, 2011 at 13:36
4

I think the simplest way to do it would be to create another hidden field to store the calculated value. Then you can use hook_webform_submission_presave() to do the calculation and store the result in $submission->data[$cid] where $cid is the component ID of the destination component.

3
  • Thanks for correcting me. But I think it would not work as well. As the hook_webform_submission_presave() does not pass $submission as reference as it should. For this webform will have to fix. Jul 26, 2011 at 13:34
  • hi, my understanding is that you might as well be using hook_form_alter() instead of unnecessarily implementing hook_webform_submission_presave(). I need for the form field to be altered before it gets submitted. Anyway, I managed to solve it, but can only post my answer in an hour (low rep), so stick around and tell me if you foresee any problems with my approach.
    – maGz
    Jul 26, 2011 at 13:38
  • 1
    @Shoaib Nawaz - I think it does pass $submission as a reference (see link in the answer). It wouldn't be a very good presave hook if it didn't let you alter the submissions!
    – Andy
    Jul 27, 2011 at 17:06
1

SOLVED: okay, after playing around a bit, this was actually easy and much simpler than I anticipated.

Firstly, instead of passing the nodeID to the hidden field, I simply went with this option for those that are similarly stuck.

function custom_form_proc_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'webform_client_form_33') {
        $node = node_load($_GET["n"]);
        $form['submitted']['pool_title']['#default_value'] = $node->title;
    }
}

When the form renders, my disabled textfield pool_title is automatically populated, and submitted along with the results. Something tells me this isn't as simple as this, but if anyone foresees an issue with this approach (like with other validation/email submission/data store

2
  • Are you passing $_GET["n"] manually? (Sorry if I'm missing something.) The only disadvantage I can see to this approach is that a malicious user could change the value for pool_title that gets returned by the browser. Also, there's no access checks on the node_load which could potentially lead to unauthorised access.
    – Andy
    Jul 27, 2011 at 17:04
  • Sorry for the late reply. In the full-view of a specific content-type's node, a link is added to the bottom of the content (generated from views_attach display), which is where $_GET['n'] is generated. Upon clicking the link (www.mysite.com/forms/participate?n=67) the user will be redirected to the form. I've handled validation by overriding my webform-node-33.tpl.php to check the content-type using node_load before rendering the form, else the user gets a nasty Access prohibited message. pool_title is also a disabled field that the user can't tamper with.
    – maGz
    Jul 30, 2011 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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