I currently have 2 webforms displayed on a page - these are inserted dynamically from a node.tpl.php file.

However, I also have a cck field on the current node for an email address - I would like to be able to pass this email value directly to the "Email To" field of the webform.

This would create a new email recipient depending on which node you were viewing. For example:

The "About Us" page contained the field "email: hello@mydomain.com" - therefore the webform loaded in the sidebar would be sent to this address.

The "Location" page contains the field "email: location@mydomain.com" - now this would be the email address that receives any submissions from that page.

The idea is to have 1 webform and dynamically change the email its sent to based on a cck field. Is this possible at all?

Thanks, Garry.

up vote 0 down vote accepted
  1. In your webform add an "email field" for the recipient (say email_recipient).

  2. Go to node/123/webform/emails to set the recipient to be your previously created field.

  3. Create a hook_form_alter to put the email address you your webform field.

    <?php
    function CUSTOM_form_alter(&$form, &$form_state, $form_id) {
      //you need to set up a CUSTOM module to store this.
      //use devel with dsm($form); to figure the name of each field
      if ($form['#id'] == 'webform-client-form-123') {
        //load the node from the url
        if (arg(0) == 'node' && is_numeric(arg(1))) {
          $node_email = node_load(arg(1));
          //set your webform field to your email
          $form['submitted']['con_email_recipient']['#default_value'] 
               = $node_email->field_email_recipient[0]['value'];
        }
      }
    }
    ?>
    

Add some CSS to hide the field, and it should work, code untested but I'm pretty confident about the steps, also have a look at Working with Default Values.

  • Thanks for the response, I have setup my custom module and added the recipient email field to my form. However, I am receiving an error with your code "Fatal error: Can't use function return value in write context in..." I also don't see where my CCK email field is being inserted into the webform. Do you have anymore instructions? Cheers, Garry. – Garry Jun 16 '11 at 9:12
  • @Garry which line cause the error ? Do you see your recipient field being populated with your CCK ? – tostinni Jun 16 '11 at 14:04

I know this is old, but in case anyone else comes up with this problem, the problem is this line:

if (arg(0) = 'node' && is_numeric(arg(1))) {

should be

    if (arg(0) == 'node' && is_numeric(arg(1))) {
  • Your post not attempt to answer this question, explain in brief!! – Bala Sep 26 '13 at 7:33
  • Yep I fixed it, thanks for the bug report ;) but it should be a comment on my answer as yours isn't a proper answer. – tostinni Aug 29 '14 at 23:06

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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