Blog

Creating a Squeeze Page to Access a Video Using the Webform Module

Image of download button

Recently a client for an "inherited" (that is, one where we weren’t the original developer) Drupal 7 site asked us to change how a series of videos were presented. They needed to collect some demographic information from site visitors before making the videos available. This is a fairly standard use case (think whitepapers on an agency site), and the Webform Protected Downloads module works pretty well if you’re dealing with PDFs. But the client wanted to make the videos appear in a lightbox. The original developer had simply made the content type inaccessible to anonymous users and required visitors to sign up for accounts to see the videos. That was, as you can guess, a less-than-ideal solution. Depending on how Drupal's new account settings were configured, there was either too high a barrier for legitimate users or too low a barrier to keep out spambots.

In our solution, we first created a registration Webform. Then in a small custom module, we used hook_form_alter to add a second submit handler to the form, where we could set a cookie:

/**
 * Implements hook_form_alter().
 *
 * Adds a second submit handler to the learning center registration webform
 */
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'webform_client_form_123') {
    $form['#submit'][] = 'mymodule_set_cookie';
  }
}

I don’t like having the form ID hardcoded here, and if I ever re-use this code elsewhere I’ll probably come up with a more elegant solution.

/**
 * Additional submit handler for video registration webform -
 * Sets a cookie so that user doesn't have to register again to view additional
 * videos.
 */
function mymodule_set_cookie() {
  user_cookie_save(array('video-registered' => TRUE));
}

Then in a preprocess function, we rewrite the link to the video. If the user is anonymous and doesn’t have the cookie set, they get a link to the webform. Otherwise, they get a Javascript link that launches a lightbox with the video:

/**
 * Implements hook_preprocess_HOOK.
 *
 * Rewrite links to videos. For anonymous users without cookie set, link to
 * registration form, otherwise link to video in a lightbox.
 */
function mymodule_node_resource_preprocess_full(&$vars){
  $cookie_name = 'Drupal_visitor_video-registered';
  if (isset($_COOKIE[$cookie_name]) || user_is_logged_in()) {
    $url = $vars['node']->field_embedded_url[LANGUAGE_NONE][0]['safe_value'];
    $options = array(
      'attributes' => array(
        'class' => array('colorbox-resource-featured'),
      )
    );
    $featured_link = l('Watch Video', $url, $options);
  }
  else {
    $options = array(
      'query' => array(
        'destination' => current_path()
      )
    );
    $featured_link = l('Register to Watch Video', 'registration-form', $options);  
  }
  $vars['featured_link'] = $featured_link;
}