You are here

function signup_confirm_email_alter_signup_form in Signup 6

Same name and namespace in other branches
  1. 6.2 modules/signup_confirm_email/signup_confirm_email.inc \signup_confirm_email_alter_signup_form()
  2. 7 modules/signup_confirm_email/signup_confirm_email.inc \signup_confirm_email_alter_signup_form()

Alter the signup form to add the e-mail confirmation functionality.

1 call to signup_confirm_email_alter_signup_form()
signup_confirm_email_form_alter in modules/signup_confirm_email/signup_confirm_email.module
Implement hook_form_alter().

File

modules/signup_confirm_email/signup_confirm_email.inc, line 14
Code to confirm the e-mail during signups. Since this code is only needed when building or submitting the signup form (or editing an existing signup), it all lives in this include file and is only loaded when needed.

Code

function signup_confirm_email_alter_signup_form(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == 'signup_edit_form') {
    $signup = $form['#signup'];

    // Only display the email confirmation fields if an authenticated user is
    // editing their own signup (anonymous signups already provide an email,
    // and we shouldn't confirm email when an admin is editing another signup).
    if (empty($user->uid) || $user->uid != $signup->uid) {
      return;
    }

    // Ensure the user has permission to edit their own signups, or the rest
    // of this is wasted effort for a form that can't be submitted.
    $node = node_load($signup->nid);
    $admin = _signup_menu_access($node, 'admin');
    $own = !empty($user->uid) && $user->uid == $signup->uid;
    $can_edit = $admin || user_access('edit own signups') && $own;
    if (!$can_edit) {
      return;
    }
  }

  // We only need to do this for authenticated users signing up
  // themselves. We already collect the (presumably current) e-mail for
  // "anonymous" signups. If an administrator is signing up another user,
  // there's no reason to include the e-mail confirmation field, either.
  if (!empty($user->uid) && empty($form['collapse']['signup_user_form']['signup_username'])) {
    drupal_add_js(drupal_get_path('module', 'signup_confirm_email') . '/signup_confirm_email.js');
    $email = array(
      '#weight' => -1,
    );
    $email['email_address'] = array(
      '#title' => t('E-mail address'),
      '#type' => 'textfield',
      '#default_value' => $user->mail,
      '#size' => 40,
    );
    $email['email_confirm'] = array(
      '#title' => t('Update e-mail address in user profile'),
      '#type' => 'checkbox',
      '#default_value' => 0,
      '#description' => t('You must confirm any changes to the e-mail address stored in your user profile by selecting this checkbox.'),
      // This div is used to hide the checkbox on page load when JS is
      // enabled, and to reveal it once the 'email_address' field is edited.
      '#prefix' => '<div class="js-hide" id="signup-confirm-email-checkbox">',
      '#suffix' => '</div>',
    );
    if ($form_id == 'signup_form') {
      $form['collapse']['email'] = $email;
      $form['#submit'][] = 'signup_email_confirm_submit';
    }
    elseif ($form_id == 'signup_edit_form') {
      $form['elements']['email'] = $email;
      $form['elements']['header']['#weight'] = -2;
      $form['elements']['save']['#submit'][] = 'signup_email_confirm_submit';
    }
    $form['#validate'][] = 'signup_email_confirm_validate';
  }
}