You are here

inline_registration.module in Inline Registration 7

Allow anonymous users to register via the node/add page.

File

inline_registration.module
View source
<?php

/**
 * @file
 * Allow anonymous users to register via the node/add page.
 */

/**
 * Implements hook_form_alter().
 */
function inline_registration_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($user->uid == 0 && isset($form['#node']) && variable_get('inline_registration_' . $form['#node']->type, 0)) {
    $form['register'] = array(
      '#type' => 'fieldset',
      '#title' => t('Login or Register as a New User'),
      '#description' => t('You are not currently logged in. In order to post this item please !login or provide the following details to register.', array(
        '!login' => l(t('login now'), 'user/login', array(
          'query' => drupal_get_destination(),
        )),
      )),
      '#weight' => variable_get('inline_registration_weight_' . $form['#node']->type, 0),
    );

    // Let other modules to alter this inline user_register form
    // with using hook_form_alter() or hook_form_FORM_ID_alter().
    $reg_form_id = 'user_register_form';
    $reg_form = drupal_retrieve_form($reg_form_id, $form_state);
    $reg_form['#id'] = $reg_form_id;
    $hooks = array(
      'form',
      'form_' . $reg_form_id,
    );
    drupal_alter($hooks, $reg_form, $form_state, $reg_form_id);
    $form['register']['form'] = $reg_form;

    // If the email_registration module is installed, the registration form
    // needs a bit more processing.
    if (module_exists('email_registration')) {

      // Calling drupal_prepare_form recursively calls all form_alter hooks and
      // causes warnings, so it cannot be used here. Instead, call the named
      // hook_form_FORM_ID_alter function from email_registration directly.
      email_registration_form_user_register_form_alter($form['register']['form'], $form_state, 'user_register_form');
    }

    // If the logintoboggan module is installed, the registration form
    // needs a bit more processing.
    if (module_exists('logintoboggan')) {

      // Calling drupal_prepare_form recursively calls all form_alter hooks and
      // causes warnings, so it cannot be used here. Instead, call the named
      // hook_form_FORM_ID_alter function from email_registration directly.
      logintoboggan_form_user_register_form_alter($form['register']['form'], $form_state);
    }

    // If the registration_toboggan module is installed, the registration form
    // needs a bit more processing.
    if (module_exists('registration_toboggan')) {

      // Calling drupal_prepare_form recursively calls all form_alter hooks and
      // causes warnings, so it cannot be used here. Instead, call the named
      // hook_form_FORM_ID_alter function from email_registration directly.
      registration_toboggan_form_user_register_form_alter($form['register']['form'], $form_state, 'user_register_form');
    }

    // Remove the user_register submit button in favor
    // of the node submit button.
    unset($form['register']['form']['actions']['submit']);

    // Rename the user field to remind the user that
    // this is the registration form and not a login field.
    $form['register']['form']['account']['name']['#title'] = t('Choose a Username');

    // Add our own validation and submit function to the node_form
    $form['#validate'][] = 'inline_registration_validate';
    if (is_array($form['#submit'])) {

      // And ensure our submit function is called first
      // (so the node is authored by the newly created user).
      array_unshift($form['#submit'], 'inline_registration_submit');
    }
    else {
      $form['#submit'] = array(
        'inline_registration_submit',
      );
    }
  }
  if ($form_id == 'node_type_form') {
    $form['inline_registration'] = array(
      '#type' => 'fieldset',
      '#title' => t('Inline registration'),
      '#description' => t('Setting for publishing this content from anonymous user, and automatically create account for this.'),
      '#weight' => 20,
      '#collapsible' => TRUE,
      '#collapsed' => variable_get('inline_registration_' . $form['#node_type']->type, 0) ? FALSE : TRUE,
      // Put it in the menu settings.
      '#group' => 'additional_settings',
    );
    $form['inline_registration']['inline_registration'] = array(
      '#type' => 'checkbox',
      '#title' => t('Inline registration'),
      '#default_value' => variable_get('inline_registration_' . $form['#node_type']->type, 0),
      '#description' => t('Enable user creation from this content.'),
    );
    $form['inline_registration']['inline_registration_weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight of field'),
      '#default_value' => variable_get('inline_registration_weight_' . $form['#node_type']->type, -10),
      '#description' => t("Select weight for this field into content creation form."),
      '#delta' => 50,
    );
  }
}

/**
 * Validation routine for inline registration form.
 */
function inline_registration_validate($form, &$form_state) {

  // For some reason the name is empty and a scrypt works
  // incorrectly so copy it from other variable.
  $form_state['values']['name'] = $form_state['input']['name'];

  // If we integrated with email_registration, name won't be there.
  if (module_exists('email_registration') && !isset($form_state['input']['name'])) {
    $form_state['values']['name'] = $form_state['input']['mail'];
  }
  else {

    // For some reason the name is empty and a script works
    // incorrectly so copy it from other variable.
    $form_state['values']['name'] = $form_state['input']['name'];
  }
  unset($form_state['uid']);
  if (module_exists('logintoboggan')) {
    logintoboggan_user_register_validate($form['register']['form'], $form_state);
  }
  else {

    // Validate using user module's validation routine.
    user_account_form_validate($form['register']['form'], $form_state);
  }
}

/**
 * Submit routine for inline registration form.
 */
function inline_registration_submit($form, &$form_state) {
  if (variable_get('user_email_verification', TRUE)) {
    $status_save = 1;
  }
  elseif (variable_get('user_register', '') == 1) {
    $status_save = 1;
  }
  else {
    $status_save = $form_state['values']['status'];
  }
  $form_state['values']['status'] = $status_save;
  unset($form_state['values']['uid']);
  if (module_exists('logintoboggan')) {
    logintoboggan_user_register_submit($form['register']['form'], $form_state);
  }
  else {
    user_register_submit($form['register']['form'], $form_state);
  }
  $form_state['values']['name'] = $form_state['user']->name;
  $form_state['values']['uid'] = $form_state['user']->uid;
  $form_state['values']['status'] = $status_save;
}

/**
 * Implements hook_node_insert().
 */
function inline_registration_node_insert($node) {
  if (!empty($node->vid)) {
    $num_updated = db_update('node_revision')
      ->fields(array(
      'uid' => $node->uid,
    ))
      ->condition('vid', $node->vid, '=')
      ->execute();
  }
}

Functions

Namesort descending Description
inline_registration_form_alter Implements hook_form_alter().
inline_registration_node_insert Implements hook_node_insert().
inline_registration_submit Submit routine for inline registration form.
inline_registration_validate Validation routine for inline registration form.