Webforms
From Stadm
Jump to navigationJump to search
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 "<pre>" ; 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) {
- Now you need to check the variable array you created. To do this, refresh the page containing the contact form
- The array hierarchy should appear at the top of the page above the content you. Search the page for the recipients field name that you created in the webform and note the structure of the array it is contained in.
- This should start with "['submitted']"
- Note -- the quotation marks within the brackets will not appear in the node array, but should be included in our script as written above.
- An example would be $form['submitted']['recipient']['#value'] and $form['submitted']['recipient']['#webform_component']['value']
- You should also note the structure of the array that contains the recipients name as well as the nid of the page node.
- Within the switch we created on Templates.php, type (changing capitalized parts to your field names as instructed):
case 'webform_client_form_[WEBFORM_NODE_HERE'] : $touser = node_load($_GET['nid']); $email = [RECIPIENT_EMAIL_LOCATION_IN_NODE]; // (ex. $touser->field_e_mail['und'][0]['value']) $name = [RECIPIENT_NAME_LOCATION_IN_NODE]; // ($touser->title) $form['submitted']['RECIPEINT_NAME_FIELD_TITLE_IN_LOWER_CASE']['#value'] = $name . " " . $lname; $form['submitted']['RECIPIENT_NAME_FIELD_TITLE_IN_LOWER_CASE']['#value'] = $email; $form['submitted']['RECIPIENT_NAME_FIELD_TITLE_IN_LOWER_CASE']['#webform_component']['value'] = $email; break; } } else if($current_url == "URL_TO_WEBFORM_LOCATION") // (ex. "/webform") { 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); }
}
- Test the form to see if it works!
Customization
If you want to customize the text body of the emails that are sent you can do that in content->WEBFORM_NAME->edit->Webform tab->E-Mails-Form Settings->Advanced