Webforms
From Stadm
Revision as of 11:09, 17 August 2012 by Zack (talk | contribs) (→Manipulating the Form Using hook_mod_alter)
Out of the box, the Drupal Webform module provides a content type that builds a form on a page with several customizable elements including text, email and textarea for emailing content to a static email address configurable through the webform E-Mail settings in the Webform content type. Creating a contact form that sends emails to a variable address can be done using hook_form_alter.
Setup
- Webform should be enabled, and in its module configuration settings, Webform-enabled content types should be set to Webform. The Default Email Values in this menu can be left as they are.
- Once the module is enabled, you can add a new webform by going to admin->content->Add Content->Webform.
- On this page, you can set the title of the webform. The body field can be left blank.
- Click URL Path Settings below the body and type the page name and path you would like the form to appear on
- On the next page you can add components to your webform
- Components are things like textareas, file upload forms, date fields etc.
- These as well as their labels will appear on your form (except those with the type 'hidden')
- Content in hidden fields is accessible by the form application, but not viewable or editable by the user.
- For a contact form, you should add the following fields
- To (fieldset) - This is an non-editable field that displays the name of the recipient
- From (textfield) - This is where the user will type their own name
- Email (textfield) - This is where the user will type their own email
- Subject (textfield) - A subject can be added
- Message (textarea) - This is where the message goes
- To Email (hidden) - this will not be displayed on the form, but will retrieve the recipients email from the drupal database
- Click the E-Mails link below the Webform tabs
- Select "Component Value", and chose the field that you created for the recipients email ("To Email" in this example)
- Click Add
- The form is now set up. You can view it by navigating to the URL that you gave the form in step 2(2).
Manipulating the Form Using hook_mod_alter
- hook_mod_alter allows us to extend the functionality of Webform
- For our purposes, we need to make message submissions send to a staff member based on the users selection
- There are three steps to this process
- Sending user id (the node id of the recipients profile) as GET data from the users profile when the link "send email" is clicked
- Retrieving the recipients email address and name from the database based on the NID from the url
- Replacing the default recipient for in the form with the true recipients email
- To send the NID as get data, go to the people or profile view set up for people
- In the view edit menu, add a new field for "NID"
- Rearrange the fields so that "NID" is at the top of the list
- Click the email field
- Click "Rewrite Results"
- Check the box labeled "Rewrite the output of this field"
- In the textarea, type the path to your Webform followed by "?nid=[nid]"
- Click Apply
- Save the view
- Check the link to make sure it takes you to the Webform and contains "?nid=" and then a number at the end of the URL
- Now we need to enter the template files and play around with some PHP
- The template files are located in /sites/all/[subtheme name]/ and the templates folder within that directory
- Copy the template file "node.tpl.php" from the base theme templates folder to the "templates" folder within the subtheme directory
- In the copied file, type at the top
<?php echo "
" ; print_r(get_defined_vars()); echo "
"; ?>
- This will display the array of variables that are available to the webform on every node page (remember to remove this code when you are done!)
- Go back a directory to your subtheme directory and open the file Template.php
- This is where we write the function to modify the form data
- Type the following (replaceing SUBTHEME_NAME with the name of your subtheme, URL_PATH with the path to your webform)
function [SUBTHEME_NAME_form_alter(&$form, $form_state, $form_id)
{ $current_url = url($_GET['q']); if(isset($_GET['nid']) && ($current_url == 'URL_PATH')) { switch ($form_id) { case 'webform_client_form_2516' : $touser = node_load($_GET['uid']); $email = $touser->field_e_mail['und'][0]['value']; $name = $touser->title;
$lname = $touser->field_bio_last_name['und'][0]['value'];
$form['submitted']['to']['#value'] = $name . " " . $lname; $form['submitted']['recipient']['#value'] = $email; $form['submitted']['recipient']['#webform_component']['value'] = $email; break; } } else if($current_url == "/contact-staff") { drupal_set_message($message = "You have not specified a user to email. Please return to <a href='people'>People</a> to chose a recipient.", $type = 'warning', $repeat = FALSE); hide($form); } }