passing CCK email or node data to a webform

exobuzz - June 18, 2009 - 17:42
Project:Webform
Version:6.x-2.7
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:needs review
Description

I have bumped into a variety of posts with this problem. Seems to be it is a basic requirement that you can pass the destination address from a node in drupal to the webform system, but I can't see how. Passing the email in the URL is not an option, as you are just asking for your form to be abused then by spammers. I would like to pass through a node ID, and then write a small piece of php so that webform can get the email address from this node. I know how to make the php to get the datam, but how can I integrate this with webform?

Unless I have missed something?

#1

exobuzz - June 18, 2009 - 18:32

I found the validation php box and have got as far as the code below. So i take the value of a hidden field and load the node to get the email address out. I now want to stick this address back into the form data so it can be submitted to that address but I have no idea which part of $form or $form_state to modify as the value appears multiple times. is it possible to change the form in the validation stage?

<?php
  $nid
= $form['submitted']['centre']['#value'];
  if (
$nid == '' ) {
   
form_set_error('centre','No such centre');
  } else {
   
$node = node_load($nid);
    if (
is_array($node->field_email) ) {
     
// where can i change the value of this form field? it exists in $form_state as well as $form and in multiple places!
      //$form['submitted']['centre']['#value'] = $node->field_email[0]['email'];
   
} else {
     
form_set_error('centre','No valid contact email or no such centre');
    }
  }
?>

Has anyone else done something similar. I find no documentation on how to do this. The code above is worked out from lots of debug output etc.

#2

exobuzz - June 18, 2009 - 18:50

I'm getting closer. I can change the value of the form now, but the form still goes to "the id" that was set before

validation code

<?php
  $nid
= $form_state['values']['submitted_tree']['centre'];
  if (
$nid == '' ) {
   
form_set_error('centre','No such centre');
  } else {
   
$mynode = node_load($nid);
    if ( !
is_array($mynode->field_email) ) {
     
form_set_error('centre','No valid contact email or no such centre');
    }
  }
?>

processing code

<?php
 
// set the field to the actual email address for the centre
 
$nid = $form_state['values']['submitted_tree']['centre'];
 
$mynode = node_load($nid);
 
$form_state['values']['submitted_tree']['centre'] = $mynode->field_email[0]['email'];
?>

any help would be most appreciated

#3

exobuzz - June 18, 2009 - 19:50
Status:active» fixed

SOLVED!

for the process code i use the following

<?php
 
// get the email for the node
 
$nid = $form_state['values']['submitted_tree']['centre'];
 
$mynode = node_load($nid);
 
// set the destination for the form to the email
 
$node->webform['email'] = $mynode->field_email[0]['email'];
?>

hopefully others will find this useful too.

#4

System Message - July 2, 2009 - 20:00
Status:fixed» closed

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

#5

2ndmile - September 3, 2009 - 18:20

exobuzz... absolutely brilliant! Thanks a ton!

#6

Kaory - September 25, 2009 - 01:19

This is exactly the logic I need ... still doesn't seem to work for me.
It seems like I can't modify that field : $node->webform['email']

In my case, the mail is deduced this way : node url (HTTP_REFERER) -> node id -> author id -> author email

Actually, my validation block is empty. Could this be the reason ?

Thanks in advance for any advice.

EDIT : well I used a hidden field to catch the node id in $_GET (%get[nid]) instead of using the HTTP_REFERER and it works just fine! Thank you for the trick!

#7

empipa - November 17, 2009 - 01:23
Status:closed» needs review

Please exobuzz, could you describe the whole process? I can´t seem to make it work. A lot of people need this so, i think, this should be some standard configuration. Be able to pull the email from anywhere.
OK, please describe: where did you create the hidden field. Whats the name of the hidden field?
Where do you write both codes?
Why am i getting the message "no such centre"?

Thanks

#8

exobuzz - November 19, 2009 - 17:55

my code is tailor made for my scenario. It will differ depending, so you need to understand the code to modify it. I take a value from a session variable and put it in a hidden field in the form. This is a standard operation. I create the session variable on another drupal page, however you could pass it as a parameter also.

in my case I pass a node id into the form. the node contains an email field that I want the form to go to.

In the validation code, I check that the hidden field (in my case centre) contains something. If it does, I do a node load on it, and then check if there is the email field. If not I give an error. You would want to change the error and field name accordingly

In the Processing code, I get the node id that was previously validated, load the node, and extract the email address. I then set the destination of the form to this address.

You are getting the no such centre error, as you have nothing in a field called "centre" in your form. change this to reflect your field values.

Feel free to petition for some built in functionality like this.

#9

empipa - November 20, 2009 - 00:28

thank you exobuzz, but still stuck.
I have the same needs as you. I need to collect the email from the node based on his id.

I created a hidden field in the form called centre too. But what code do i put in it, and what value? What do i do with this field?

How do i create this session variable. That´s where i´m stuck. I need the code and where do i write it.
In my case every page of a certain content type will have this session variable retrieved to be sent an email to.

In this content type i also have a field called "field_emailestabelecimento". This is where the email is.

#10

exobuzz - November 23, 2009 - 16:32

you can pass the node id to the webform in a querystring which might be easiest and then get the webform to put this in a hidden field. See "Setting default values from the url" on http://drupal.org/node/296453

#11

hatsharpener - January 2, 2010 - 00:54

exobuzz, just curious where you're putting the "validation code" and the "process code." I follow your logic and the code makes sense, but I'm not sure where to put it. Are you altering the webform_components.inc in the module, or are you creating a template for the node? I know PHP, but altering a Drupal module is new to me.

Thanks!

#12

exobuzz - January 2, 2010 - 15:51

There are textarea boxes provided by webform for the form you are working on in the configuration tab under "Webform advanced settings". No need to manually edit any files.

 
 

Drupal is a registered trademark of Dries Buytaert.