Download & Extend

passing CCK email or node data to a webform

Project:Webform
Version:6.x-3.9
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:closed (fixed)

Issue Summary

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?

Comments

#1

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

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

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

Status:fixed» closed (fixed)

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

#5

exobuzz... absolutely brilliant! Thanks a ton!

#6

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

Status:closed (fixed)» 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

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

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

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

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

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.

#13

Status:needs review» closed (fixed)

Closing after lack of activity. Thanks exobuzz for all your input on this! Generally support for how to write custom validation/submission is not provided in the Webform issue queue.

#14

Just an FYI for anyone else here: These validation fields hatsharpener can't find appear in Webform 2, but not Webform 3.

#15

For those looking for the validation field, as of version 3, I went the route of using webform rules module. I pass the nid in and then bake that into a hidden field. The rule picks up that NID with PHP code and then does a node_load, and then gets the email address value from the node and then puts that in the $data array so that the next action, the "send to arbitrary email address" can use it in the "to" field -- works great!

#16

Hi Capellic,

Would you mind sharing your rule / actions / code here? I'm looking for a similar output, but don't know where to start. Thanks!

Cheers,

#17

Version:6.x-2.7» 6.x-3.9

I had a site that wanted to pull email address from nodes into a field in their webform, which was displayed as a block. I looked around, but since the validation area is gone in Webform 3, it seems that we're expected to use the (now extensive) system of hooks to do what we want.

I threw together a module for the most common case: pull data from an arbitrary field on an arbitrary node into a webform field. You can specify the source NID for the data in a field on the webform (which in turn can be filled by a URL argument if you want), or just use the currently displayed node if your webform is on a block. You can specify a single webform to receive this functionality, or apply it to all webforms.

Just install the module as usual, and go to /admin/settings/webform-node-data to tell the module:

* the NID of the webform to apply to (0 for all webforms)
* the field on that webform which contains the data source's NID (or enter to get it from the currently displayed node)
* the field on the data source node which contains your "target data" (field_agent_email , in my case)
* the type of data in that field. use "email" for CCK email fields, and "value" for CCK textfields
* the destination field on the webform which should receive the data.

I'm not sure if this is worth submitting as a sub module or not, but hopefully it will help someone out there.

AttachmentSize
webform_node_data.tgz 2.75 KB

#18

Status:closed (fixed)» needs review

changing to "needs review" because something like this would be a useful addition (to the D6 version at least).

#19

Thanks ohthehugemanatee! I get this request surprisingly often. It's *very* common to get the "How do I get the title of the current node" when the webform is being displayed as a block on all pages. I'd love to see this moved into Git, at least as a sandbox project.

#20

Status:needs review» closed (fixed)

Awesome! Thanks for the vote of confidence quicksketch. http://drupal.org/sandbox/ohthehugemanatee/1221588 I'm the proud parent of a sandbox module! Here's hoping it's useful for others in the future.

#21

Where should I write this code?
And what is the submitted_tree and center in your code. Please explain it because I have the similar problem.
I am fetching the email address from the cck field of a content type and assign it to the webform to send mail to this email address.
Please also tell me how would I write the body of the email in this situation??

Any help will be appreciated.

#22

Category:support request» feature request

It's possible to make it works with Token Field Module?

In this way Webform could have a full token support without using the Patched version (Webform Patched).

At the moment I can load textfield type field... but not the token field.

Thanks!
Bye.

#23

Hi ohthehugemanatee, can't get it to work, when i pass the <active> value i get a blank page where the block is located. Any idea? Thanks!

#24

possible now?