Remove Return Link on Drupal Webform Ajax Confirmations

The 'Go back to the form' link that the Drupal Webform module prints at the bottom of confirmation messages isn't always desirable, especially in cases where a form is submitted by ajax using the Webform Ajax module.

Webform Confirmation Return Link

On a recent project, the client desired to remove the 'Go back to the form' link but only on ajax-enabled forms. This behavior can be achieved in the theme layer.

  1. Copy 'webform-confirmation.tpl.php' into the theme's templates directory

Since the link code is printed by Webform in the 'webform-confirmation.tpl.php' template file, the first step is to find the 'webform-confirmation.tpl.php' file in Webform's module directory (webform/templates/webform-confirmation.tpl.php) and copy this file to the active theme's templates directory.

  1. Check if the form is ajax-enabled

The TPL file needs modified to check if the form is ajax-enabled:

<?php
 
if(isset($node->webform['webform_ajax'])) {
    if(
$node->webform['webform_ajax'] == 1) {
     
$ajax = 1;
    }
    else {
     
$ajax = 0;
    }
  }
  else {
   
$ajax = 0;
  }
?>

The first step is to check if the 'webform_ajax' variable is set. If it's not, then the form is not ajax-enabled (so $ajax = 0). If the variable is set, then the next step is to check if ajax is enabled on the form by checking the value of the 'webform_ajax' variable. If webform_ajax = 1, then ajax is enabled (so $ajax = 1). If webform_ajax ≠ 1, then ajax is not enabled (so $ajax = 0).

  1. Print the link markup only if the form is not ajax-enabled (i.e. $ajax = 0)

Next, the TPL file needs modified to only print the the return link if the form is not ajax enabled:

<?php if($ajax == 0) : ?>
  <div class="links">
    <a href="<?php print url('node/'. $node->nid) ?>"><?php print t('Go back to the form') ?></a>
  </div>
<?php endif; ?>

The TPL file will now check if ajax is enabled before printing the 'Go back to the form' link. If ajax is not enabled, the link will print. If ajax is enabled, then the link will not print.

In this example, the following projects were used:

  • Drupal 7.31
  • Webform 7.x-4.0-rc5
  • Webform Ajax 7.x-1.1
    • *Patched with http://drupal.org/files/webform_ajax-webform-4.x-support-2102029.patch

*Webform Ajax 7.x-1.1 requires a patch in order to support Webform 7.x-4.x. Support for Webform 7.x-4.x has been committed to Webform Ajax 7.x-1.x-dev and will be included in the next stable release of Webform Ajax, presumably 7.x-1.2.

The entire TPL file is provided below:

<?php
/**
 * @file
 * Customize confirmation screen after successful submission.
 *
 * This file may be renamed "webform-confirmation-[nid].tpl.php" to target a
 * specific webform e-mail on your site. Or you can leave it
 * "webform-confirmation.tpl.php" to affect all webform confirmations on your
 * site.
 *
 * Available variables:
 * - $node: The node object for this webform.
 * - $progressbar: The progress bar 100% filled (if configured). This may not
 *   print out anything if a progress bar is not enabled for this node.
 * - $confirmation_message: The confirmation message input by the webform
 *   author.
 * - $sid: The unique submission ID of this submission.
 */
?>

<?php
 
if(isset($node->webform['webform_ajax'])) {
    if(
$node->webform['webform_ajax'] == 1) {
     
$ajax = 1;
    }
    else {
     
$ajax = 0;
    }
  }
  else {
   
$ajax = 0;
  }
?>

<?php print $progressbar; ?>
<div class="webform-confirmation">
  <?php if ($confirmation_message): ?>
    <?php print $confirmation_message ?>
  <?php else: ?>
    <p><?php print t('Thank you, your submission has been received.'); ?></p>
  <?php endif; ?>
</div>
<?php if($ajax == 0) : ?>
  <div class="links">
    <a href="<?php print url('node/'. $node->nid) ?>"><?php print t('Go back to the form') ?></a>
  </div>
<?php endif; ?>

Add new comment