You are here

function invite_form_alter in Invite 6.2

Same name and namespace in other branches
  1. 5.2 invite.module \invite_form_alter()
  2. 5 invite.module \invite_form_alter()
  3. 7.2 invite.module \invite_form_alter()

Implementation of hook_form_alter().

File

./invite.module, line 340
Allows your users to send and track invitations to join your site.

Code

function invite_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'user_admin_settings':

      // Add new registration mode 'by invitation only'. By prepending the
      // option value with a numeric value, other modules still work as
      // expected, as long as they are using the non-strict PHP comparison
      // operator (since '1-inviteonly' == 1 yields TRUE). To determine the real
      // setting use invite_user_registration_by_invite_only().
      //
      // However, setting the new mode is only allowed if no other module
      // has overridden the menu access handler for the user registration form.
      $item = menu_get_item('user/register');
      if (in_array($item['access_callback'], array(
        'user_register_access',
        'invite_user_register_access',
      ))) {
        $form['registration']['user_register']['#options']['1-inviteonly'] = t('New user registration by invitation only.');
      }

      // Clear menu cache on submit to allow our custom access handler to
      // snap in.
      $form['#submit'][] = 'menu_rebuild';
      break;
    case 'user_register':

      // In order to prevent caching of the preset e-mail address, we have to
      // disable caching for user/register.
      $GLOBALS['conf']['cache'] = CACHE_DISABLED;
      $invite = invite_load_from_session();

      // Legacy url support (user/register/regcode).
      if (!$invite && ($code = arg(2))) {
        if ($invite = invite_load($code)) {
          if (invite_validate($invite)) {
            $_SESSION[INVITE_SESSION] = $invite->reg_code;
          }
        }
      }
      if ($invite) {

        // Preset the e-mail field.
        if (isset($form['account'])) {
          $field =& $form['account'];
        }
        else {
          $field =& $form;
        }
        if (isset($field['mail'])) {
          $field['mail']['#default_value'] = $invite->email;
        }
      }
      break;
    case 'user_login_block':

      // Remove temptation for non members to try and register.
      if (invite_user_registration_by_invite_only()) {
        $new_items = array();
        $new_items[] = l(t('Request new password'), 'user/password', array(
          'attributes' => array(
            'title' => t('Request new password via e-mail.'),
          ),
        ));
        $form['links']['#value'] = theme('item_list', $new_items);
      }
      break;
  }
}