background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Adding terms and conditions to the user registration form

Eric.London's picture

The following will show you how to add a terms and conditions form element and admin settings page to display a required terms and conditions form element on the user registration form. First, I added an admin settings page.

<?php
function MYMODULE_admin_settings_form() {
 
$form = array();
 
// ...code...
 
$form['MYMODULE_terms_conditions'] = array(
   
'#type'             => 'textarea',
   
'#title'            => t('Terms and Conditions'),
   
'#default_value'    => variable_get('MYMODULE_terms_conditions', ''),
   
'#required'         => FALSE,
  );
 
// ...code...
 
return system_settings_form($form);
}
?>

Next, I modified the user registration form.

<?php
function MYMODULE_form_alter($form_id, &$form) {
 
// ...code...
 
if ($form_id == 'user_register') {
   
$terms = variable_get('MYMODULE_terms_conditions', '');
    if (
strlen($terms)) {
     
// add terms and conditions
     
$form['terms'] = array(
       
'#type'    => 'checkbox',
       
'#required' => TRUE,
       
'#weight' => 9,
       
'#title' => "I agree to the <a id='termsAndConditionsLink'>terms and conditions</a><div style='display:none;' id='termsAndConditionsText'>$terms</div>",
      );
    }
  }
 
// ...code...
}
?>

Lastly, I added some jQuery to my module's javascript include file.

<?php
$(document).ready(function(){
  $(
'a#termsAndConditionsLink').click(function() {
    if ($(
'div#termsAndConditionsText').css('display') == 'none') {
      $(
'div#termsAndConditionsText').slideDown();
    } else {
      $(
'div#termsAndConditionsText').slideUp();
    }
  });
});
?>

couple things missing

hey,

thanks a lot for the post, it was very helpful. there were a couple things missing that i noticed when i implemented this into a site i'm working on.

-first, needed to implement hook_menu to register the path admin/settings/MYMODULE:

<?php
function MYMODULE_menu () {
 
$items['admin/settings/MYMODULE'] = array (
   
'title' => t('MYMODULE Settings'),
   
'description' => t('Change settings for the MYMODULE module.'),
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array ('MYMODULE_admin_settings_form'),
   
'access arguments' => array ('administer site configuration'),
   
'type' => MENU_NORMAL_ITEM,
  );
  return
$items;
}
?>

-next, in MYMODULE_admin_settings_form i needed to return: system_settings_form ($form) inorder to get the submit buttons on that form.

-also, in my case i needed to require the checkbox be checked, and for some reason, drupal was not catching the fact that the checkbox wasn't checked. so i added validation to the field where in defining the form under hook_form_alter, I added the '#element_validate' property set it to this function:

<?php
function MYMODULE_user_register_terms_validate ($element, $form_state) {
 
  if(!
$form_state['values']['terms']) {
   
form_error($element, t('You must agree to the terms and conditions to continue.'));
  }
}
?>

hope that helps others.

~matt

Eric.London's picture

thanks

Hi Matt,

Thanks for adding the menu hook, I can't believe I forgot to include that. Looks like I need to hire an editor :)

Regards,
-Eric