You are here

ajax_register.module in Ajax Login/Register 7.3

File

ajax_register.module
View source
<?php

/**
 * Implements hook_menu().
 */
function ajax_register_menu() {
  $items = array();
  $items['ajax_register/%/%'] = array(
    'title' => 'Request new password',
    'page callback' => 'ajax_register_ajax_page_callback',
    'page arguments' => array(
      1,
      2,
    ),
    'access callback' => 'ajax_register_ajax_page_access',
    'access arguments' => array(
      1,
      2,
    ),
    'type' => MENU_CALLBACK,
    'file' => 'ajax_register.pages.inc',
  );
  return $items;
}

/**
 * Implements hook_form_alter().
 */
function ajax_register_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'user_login_block':
      $registration_allowed = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL);
      if ($registration_allowed) {
        $user_register_link = array(
          '#type' => 'link',
          '#title' => t('Create new account'),
          '#href' => 'ajax_register/register/nojs',
          '#id' => 'ajax_link',
          '#attributes' => array(
            'class' => array(
              'use-ajax',
            ),
            'title' => t('Create a new user account.'),
          ),
        );
      }
      $user_password_link = array(
        '#type' => 'link',
        '#title' => t('Request new password'),
        '#href' => 'ajax_register/password/nojs',
        '#id' => 'ajax_link',
        '#attributes' => array(
          'class' => array(
            'use-ajax',
          ),
          'title' => t('Request new password via e-mail.'),
        ),
      );
      $items[] = render($user_register_link);
      $items[] = render($user_password_link);
      $form['links'] = array(
        '#theme' => 'item_list',
        '#items' => $items,
      );

    // No break here!
    case 'user_login':
      $form['messages'] = array(
        '#markup' => '<div id="ajax-register-login-messages"></div>',
      );
      $form['actions']['submit']['#ajax'] = array(
        'callback' => 'ajax_register_login_ajax_callback',
      );
      break;
    case 'user_pass':
      $form['messages'] = array(
        '#markup' => '<div id="ajax-register-pass-messages"></div>',
      );
      $user_login_link = array(
        '#type' => 'link',
        '#title' => t('Back to login'),
        '#href' => 'ajax_register/login/nojs',
        '#id' => 'ajax_link',
        '#attributes' => array(
          'class' => array(
            'use-ajax',
          ),
          'title' => t('Back to login form.'),
        ),
      );
      $items[] = render($user_login_link);
      $form['links'] = array(
        '#theme' => 'item_list',
        '#items' => $items,
      );
      $form['actions']['submit']['#ajax'] = array(
        'callback' => 'ajax_register_pass_ajax_callback',
      );
      $form['#after_build'][] = 'ajax_register_user_pass_after_build';
      break;
    case 'user_register_form':
      $form['messages'] = array(
        '#markup' => '<div id="ajax-register-reg-messages"></div>',
      );
      $form['actions']['submit']['#ajax'] = array(
        'callback' => 'ajax_register_register_ajax_callback',
      );
      break;
  }
}

/**
 * Add user.pages.inc for enabling user_pass_validate() and user_pass_submit().
 */
function ajax_register_user_pass_after_build($form, &$form_state) {
  module_load_include('pages.inc', 'user');
  return $form;
}

/**
 * Ajax callback for user register form.
 */
function ajax_register_register_ajax_callback() {
  $commands = array();
  if (!form_get_errors()) {
    $commands[] = ajax_command_replace('#user-register-form', theme('status_messages'));
  }
  else {
    $commands[] = ajax_command_html('#ajax-register-reg-messages', theme('status_messages'));
  }
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}

/**
 * Ajax callback for user pass form.
 */
function ajax_register_pass_ajax_callback() {
  $commands = array();
  if (!form_get_errors()) {
    $commands[] = ajax_command_replace('#user-pass', theme('status_messages'));
  }
  else {
    $commands[] = ajax_command_html('#ajax-register-pass-messages', theme('status_messages'));
  }
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}

/**
 * Ajax callback for user login form.
 */
function ajax_register_login_ajax_callback() {
  $commands = array();
  if (!form_get_errors()) {

    // Set up success message.
    drupal_set_message(t('Login successful. You are now being redirected.'));

    // Reload current page.
    ctools_include('ajax');
    ctools_add_js('ajax-responder');
    $commands[] = ctools_ajax_command_reload();
  }

  // Add messages to login form.
  $commands[] = ajax_command_html('#ajax-register-login-messages', theme('status_messages'));

  // Return ajax commands.
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}

/**
 * Check access to view ajax register pages.
 */
function ajax_register_ajax_page_access($form, $type) {
  $forms = array(
    'login',
    'register',
    'password',
  );
  $types = array(
    'ajax',
    'nojs',
  );
  if (in_array($form, $forms) && in_array($type, $types) && user_is_anonymous()) {
    return TRUE;
  }
  return FALSE;
}

Functions

Namesort descending Description
ajax_register_ajax_page_access Check access to view ajax register pages.
ajax_register_form_alter Implements hook_form_alter().
ajax_register_login_ajax_callback Ajax callback for user login form.
ajax_register_menu Implements hook_menu().
ajax_register_pass_ajax_callback Ajax callback for user pass form.
ajax_register_register_ajax_callback Ajax callback for user register form.
ajax_register_user_pass_after_build Add user.pages.inc for enabling user_pass_validate() and user_pass_submit().