You are here

THEMING.txt in Webform 5.2

Same filename and directory in other branches
  1. 5 THEMING.txt
  2. 6.3 THEMING.txt
  3. 6.2 THEMING.txt
  4. 7.3 THEMING.txt
Overview
--------
Webform supports theming similar to the Flexinode or Views modules. Any webform
may be themed on the server side, though doing so may require a reasonable
amount of knowledge about the Drupal Forms API. More information about the Forms
API may be found at http://api.drupal.org/api/file/developer/topics/forms_api.html

Theme submission emails
-----------------------
The default emails sent by webform are fairly basic. If you like, you may
customize the display of emails sent by each individual webform. This tutorial
assumes use of the phptemplate engine.

- Open your template.php file located in your theme's directory.
- Add the following lines of php code:

function phptemplate_webform_mail_message_[node id here]($form_values, $node, $sid, $cid) {
  return _phptemplate_callback('webform-mail-[node id here]', array('form_values' => $form_values, 'node' => $node, 'sid' => $sid, 'cid' => $cid));
}

- Create a new file in your theme's directory named
  "webform-mail-[node id here].tpl.php", once again replacing [node id here]
  with the node ID of the webform.
  
- Open up your new file and customize the webform email. Here's a simple template
  to get you started:
  
  <?php /* Begin sample webform mail message file */ ?>
  
  Company X Official Website Submission
  
  Message was submitted <?php echo format_date(time(), 'small') ?>
  
  From the IP address <?php echo $_SERVER['REMOTE_ADDR']; ?>
  
  The user's favorite color is <?php echo $form_values['submitted_tree']['favorite_color'] ?>
  
  The user's problem is below:
  <?php echo $form_values['submitted_tree']['problem'] ?>
  
  <?php /* End sample webform mail message file */ ?>

- To get a better idea of what variables are available to you, you can include
  the print_r function in your email. Simply include the line:
  
  <?php print_r($form_values) ?>
  
  to get a listing of all the available fields you can use in your mail.
  
- An Important Note for Webform Themers: When webform added support for fieldsets
  (i.e. nested fields), it became necessary to increase the complexity of themed
  emails. Previously, the $form_values variable only sent the values of the form in
  a flat array. Now, $form_values contains two arrays of information:
  
  $form_values['submitted'] => An array of fields and their submitted values (identical to the previous value of $form_values)
  $form_values['submitted_tree'] => An array of fields and their values structured in a recursive array

- Advanced Webform e-mail Theming: Theming the e-mail headers may also be done
  by overriding the theme_webform_mail_headers() function from webform.module.
  Just copy the code out of webform.module and change as necessary in your
  template.php file. This allows you to customize the e-mail headers.


Theme the confirmation page
---------------------------

After a user submits a webform, they are directed to a page that contains the
confirmation message set in the webform node settings (assuming the form doesn't
direct to a complete URL). These instructions let you customize the format of
the confirmation page of a single node or all webforms on your site.

- Open your template.php file located in your theme's directory.
- Add the following lines of php code:

function phptemplate_webform_confirmation_[node id here]($node, $sid) {
  return _phptemplate_callback('webform-confirmation-[node id here]', array('node' => $node, 'sid' => $sid));
}

- Create a new file in your theme's directory named
  "webform-confirmation-[node id here].tpl.php", once again replacing [node id here]
  with the node ID of the webform.
  
- Open the new file and change it's contents to the your liking. Here's an
  example that inserts some additional HTML around the confirmation message and
  gives links to edit the submission.
  
  <?php /* Begin sample webform confirmation page */ ?>
  
  <div class="confirmation-message">
    <?php print check_markup($node->webform['confirmation'], $node->format, FALSE); ?>
  </div>
  
  <ul>
    <li><a href="<?php print url('node/'. $node->nid . '/submission/'. $sid)?>">View your submission</a></li>
    <li><a href="<?php print url('node/'. $node->nid . '/submission/'. $sid .'/edit')?>">Edit your submission</a></li>
  </ul>
  
  <?php /* End sample webform confirmation page */ ?>

- You can make this apply to all webforms by using this function in template.php

function phptemplate_webform_confirmation($node, $sid) {
  return _phptemplate_callback('webform-confirmation', array('node' => $node, 'sid' => $sid));
}

- Note that the [node id here] has simply been removed to make it work with all
  webform nodes.

Theme display of an entire webform
----------------------------------

Theming a webform can be useful for rearranging elements or customizing the
appearance of multiple components at once. This tutorial assumes usage
of the phptemplate engine.

- Open your template.php file located in your theme's directory.
- Add the following lines of php code:

function phptemplate_webform_form_[node id here] ($form) {
  return _phptemplate_callback('webform-form-[node id here]', array('form' => $form));
}

- Replace "[node id here]" with the node ID of the form.

- Create a new file in your theme's directory named
  "webform-form-[node id here].tpl.php", once again replacing [node id here]
  with the node ID of the webform.
  
- Open up your new file and customize the webform however you like. Here's an
  example putting a field with the "email" key inside of another fieldset.
  
<?php
  // Create a new fieldset within the main fieldset
  // Note: All fields MUST stay within the 'submitted' fieldset
  $form['submitted']['newfieldset'] = array(
    '#type' => 'fieldset',
  );
  
  // Move the form field labeled "email" to the new fieldset
  $form['submitted']['newfieldset']['email'] = $form['submitted']['email'];
  unset($form['submitted']['email']);
  print drupal_render($form);
?>

- All webform forms have 2 main fieldsets: "submitted", and "details". Although
  you may move things around as you wish, keep all your components within the
  "submitted" fieldset. Only the "submitted" fieldset is displayed and webform
  depends on the other two to operate properly, so don't mess with them unless
  you have good reason to do so (like you're forwarding your webform to a custom
  PHP or PERL script).


File

THEMING.txt
View source
  1. Overview
  2. --------
  3. Webform supports theming similar to the Flexinode or Views modules. Any webform
  4. may be themed on the server side, though doing so may require a reasonable
  5. amount of knowledge about the Drupal Forms API. More information about the Forms
  6. API may be found at http://api.drupal.org/api/file/developer/topics/forms_api.html
  7. Theme submission emails
  8. -----------------------
  9. The default emails sent by webform are fairly basic. If you like, you may
  10. customize the display of emails sent by each individual webform. This tutorial
  11. assumes use of the phptemplate engine.
  12. - Open your template.php file located in your theme's directory.
  13. - Add the following lines of php code:
  14. function phptemplate_webform_mail_message_[node id here]($form_values, $node, $sid, $cid) {
  15. return _phptemplate_callback('webform-mail-[node id here]', array('form_values' => $form_values, 'node' => $node, 'sid' => $sid, 'cid' => $cid));
  16. }
  17. - Create a new file in your theme's directory named
  18. "webform-mail-[node id here].tpl.php", once again replacing [node id here]
  19. with the node ID of the webform.
  20. - Open up your new file and customize the webform email. Here's a simple template
  21. to get you started:
  22. Company X Official Website Submission
  23. Message was submitted
  24. From the IP address
  25. The user's favorite color is
  26. The user's problem is below:
  27. - To get a better idea of what variables are available to you, you can include
  28. the print_r function in your email. Simply include the line:
  29. to get a listing of all the available fields you can use in your mail.
  30. - An Important Note for Webform Themers: When webform added support for fieldsets
  31. (i.e. nested fields), it became necessary to increase the complexity of themed
  32. emails. Previously, the $form_values variable only sent the values of the form in
  33. a flat array. Now, $form_values contains two arrays of information:
  34. $form_values['submitted'] => An array of fields and their submitted values (identical to the previous value of $form_values)
  35. $form_values['submitted_tree'] => An array of fields and their values structured in a recursive array
  36. - Advanced Webform e-mail Theming: Theming the e-mail headers may also be done
  37. by overriding the theme_webform_mail_headers() function from webform.module.
  38. Just copy the code out of webform.module and change as necessary in your
  39. template.php file. This allows you to customize the e-mail headers.
  40. Theme the confirmation page
  41. ---------------------------
  42. After a user submits a webform, they are directed to a page that contains the
  43. confirmation message set in the webform node settings (assuming the form doesn't
  44. direct to a complete URL). These instructions let you customize the format of
  45. the confirmation page of a single node or all webforms on your site.
  46. - Open your template.php file located in your theme's directory.
  47. - Add the following lines of php code:
  48. function phptemplate_webform_confirmation_[node id here]($node, $sid) {
  49. return _phptemplate_callback('webform-confirmation-[node id here]', array('node' => $node, 'sid' => $sid));
  50. }
  51. - Create a new file in your theme's directory named
  52. "webform-confirmation-[node id here].tpl.php", once again replacing [node id here]
  53. with the node ID of the webform.
  54. - Open the new file and change it's contents to the your liking. Here's an
  55. example that inserts some additional HTML around the confirmation message and
  56. gives links to edit the submission.
  57. webform['confirmation'], $node->format, FALSE); ?>
  • - You can make this apply to all webforms by using this function in template.php
  • function phptemplate_webform_confirmation($node, $sid) {
  • return _phptemplate_callback('webform-confirmation', array('node' => $node, 'sid' => $sid));
  • }
  • - Note that the [node id here] has simply been removed to make it work with all
  • webform nodes.
  • Theme display of an entire webform
  • ----------------------------------
  • Theming a webform can be useful for rearranging elements or customizing the
  • appearance of multiple components at once. This tutorial assumes usage
  • of the phptemplate engine.
  • - Open your template.php file located in your theme's directory.
  • - Add the following lines of php code:
  • function phptemplate_webform_form_[node id here] ($form) {
  • return _phptemplate_callback('webform-form-[node id here]', array('form' => $form));
  • }
  • - Replace "[node id here]" with the node ID of the form.
  • - Create a new file in your theme's directory named
  • "webform-form-[node id here].tpl.php", once again replacing [node id here]
  • with the node ID of the webform.
  • - Open up your new file and customize the webform however you like. Here's an
  • example putting a field with the "email" key inside of another fieldset.
  • // Create a new fieldset within the main fieldset
  • // Note: All fields MUST stay within the 'submitted' fieldset
  • $form['submitted']['newfieldset'] = array(
  • '#type' => 'fieldset',
  • );
  • // Move the form field labeled "email" to the new fieldset
  • $form['submitted']['newfieldset']['email'] = $form['submitted']['email'];
  • unset($form['submitted']['email']);
  • print drupal_render($form);
  • ?>
  • - All webform forms have 2 main fieldsets: "submitted", and "details". Although
  • you may move things around as you wish, keep all your components within the
  • "submitted" fieldset. Only the "submitted" fieldset is displayed and webform
  • depends on the other two to operate properly, so don't mess with them unless
  • you have good reason to do so (like you're forwarding your webform to a custom
  • PHP or PERL script).