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

  1. 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.
  2. Once the module is enabled, you can add a new webform by going to admin->content->Add Content->Webform.
    1. On this page, you can set the title of the webform. The body field can be left blank.
    2. Click URL Path Settings below the body and type the page name and path you would like the form to appear on
  3. On the next page you can add components to your webform
    1. Components are things like textareas, file upload forms, date fields etc.
    2. These as well as their labels will appear on your form (except those with the type 'hidden')
    3. Content in hidden fields is accessible by the form application, but not viewable or editable by the user.
  4. For a contact form, you should add the following fields
    1. To (fieldset) - This is an non-editable field that displays the name of the recipient
    2. From (textfield) - This is where the user will type their own name
    3. Email (textfield) - This is where the user will type their own email
    4. Subject (textfield) - A subject can be added
    5. Message (textarea) - This is where the message goes
    6. To Email (hidden) - this will not be displayed on the form, but will retrieve the recipients email from the drupal database
  5. Click the E-Mails link below the Webform tabs
  6. Select "Component Value", and chose the field that you created for the recipients email ("To Email" in this example)
  7. Click Add
  8. 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
  1. Sending user id (the node id of the recipients profile) as GET data from the users profile when the link "send email" is clicked
  2. Retrieving the recipients email address and name from the database based on the NID from the url
  3. 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
  1. In the view edit menu, add a new field for "NID"
  2. Rearrange the fields so that "NID" is at the top of the list
  3. Click the email field
  4. Click "Rewrite Results"
  5. Check the box labeled "Rewrite the output of this field"
  6. In the textarea, type the path to your Webform followed by "?nid=[nid]"
  7. Click Apply
  8. Save the view
  9. 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
  1. Now we need to enter the template files and play around with some PHP
  2. The template files are located in /sites/all/[subtheme name]/ and the templates folder within that directory
  3. Copy the template file "node.tpl.php" from the base theme templates folder to the "templates" folder within the subtheme directory
  4. In the copied file, type at the top
<?php echo "<pre>" ; print_r(get_defined_vars()); echo "

"; ?>

    1. 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!)
  1. Go back a directory to your subtheme directory and open the file Template.php
    1. This is where we write the function to modify the form data
  2. 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) 
 		{
  1. Now you need to check the variable array you created. To do this, refresh the page containing the contact form
  2. 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']
  1. You should also note the structure of the array that contains the recipients name as well as the nid of the page node.
  2. 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);
	}

}

  1. 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