You are here

function hybridauth_form_user_register_form_alter in HybridAuth Social Login 7

Implements hook_form_FORM_ID_alter().

Prefills the registration form with values acquired via HybridAuth.

File

./hybridauth.module, line 180

Code

function hybridauth_form_user_register_form_alter(&$form, &$form_state) {

  // Only alter if the user is signing in using HybridAuth (the HybridAuth session may
  // still be set if admin has signed in using HybridAuth and is adding a user, so
  // check that this is not the case).
  if (!isset($_SESSION) || !isset($_SESSION['hybridauth']) || $_SESSION['hybridauth']['stat'] != 'ok' || user_access('administer users')) {
    return;
  }
  $profile = $_SESSION['hybridauth']['profile'];

  // Use the nickname returned by HybridAuth.
  $form['account']['name']['#default_value'] = $profile['displayName'];
  $mail = '';
  if (!empty($profile['email'])) {
    $mail = $profile['email'];
  }

  // Use the email returned by HybridAuth, if any.
  $form['account']['mail']['#default_value'] = $mail;

  // If email verification is not required, hide the password field and
  // just fill with random password to avoid confusion.
  if (!empty($profile['emailVerified']) || !variable_get('user_email_verification', TRUE) || variable_get('hybridauth_bypass_email_verification', FALSE)) {
    $form['account']['pass']['#type'] = 'hidden';
    $form['account']['pass']['#value'] = user_password();
  }
  $form['hybridauth_id'] = array(
    '#type' => 'value',
    '#default_value' => $profile['identifier'],
  );
  $form['id_display'] = array(
    '#type' => 'item',
    '#weight' => 10,
    '#title' => t('Your account ID'),
    '#description' => t('This @provider account will be linked to your site account after registration.', array(
      '@provider' => $_SESSION['hybridauth_provider_info']['name'],
    )),
    '#markup' => check_plain(_hybridauth_encode_authname($_SESSION['hybridauth_provider_info']['id'], $profile['identifier'])),
  );

  // replace the default user_register form submit handler with our own (because
  // we need to honor verifiedEmail and hybridauth_bypass_email_verification, save the
  // HybridAuth user picture, etc.)
  $form['#submit'][array_search('user_register_submit', $form['#submit'])] = 'hybridauth_user_register_submit';

  /*
  $field_map = variable_get('hybridauth_profile_fields_map', array());
  if (empty($field_map)) {
    return;
  }

  // Use HybridAuth data to pre-fill profile fields.
  if(module_exists('profile')) {
    // Build an array of HybridAuth field ID's keyed by profile field name
    $map = array();
    foreach ($field_map as $mid => $field_mapping) {
      // Make sure it's a valid mapping.
      if (!isset($field_mapping['fid'])) {
        continue;
      }

      if($field_mapping['set'] == 'profile') {
        $map[$field_mapping['field']] = $field_mapping['fid'];
      }
    }
    // Search for profile fields and initialize them with HybridAuth profile data.
    if(!empty($map)) {
      foreach ($form as $category_key => $category) {
        if (is_array($category)) {
          foreach ($category as $field_key => $field) {
            if (substr($field_key, 0, 8) == 'profile_' && isset($map[$field_key])) {
              $form[$category_key][$field_key]['#default_value'] = _hybridauth_data_map($_SESSION['hybridauth'], $map[$field_key]);
            }
          }
        }
      }
    }
  }

  // Use HybridAuth data to pre-fill Profile2 fields.
  if(module_exists('profile2')) {
    // Build a map keyed by $bundle.$field (we cannot key by $field, as
    // fields in different bundles can have the same name).
    $map = array();
    foreach ($field_map as $mid => $field_mapping) {
      // Make sure it's a valid mapping.
      if (!isset($field_mapping['fid'])) {
        continue;
      }

      if($field_mapping['set'] == 'profile2') {
        $map[$field_mapping['bundle'] . $field_mapping['field']] = $field_mapping['fid'];
      }
    }
    if(!empty($map)) {
      foreach (field_info_instances('profile2') as $bundle => $fields) {
        foreach ($fields as $field => $array) {
          if (isset($map[$bundle.$field]) && $mapped_value = _hybridauth_data_map($_SESSION['hybridauth'], $map[$bundle.$field])) {
            $form['profile_' . $bundle][$field][LANGUAGE_NONE][0]['value']['#default_value'] = $mapped_value;
          }
        }
      }
    }
  }

  // Use HybridAuth data to pre-fill User fields.
  $map = array();
  foreach ($field_map as $mid => $field_mapping) {
    // Make sure it's a valid mapping.
    if (!isset($field_mapping['fid'])) {
      continue;
    }

    if($field_mapping['set'] == 'user') {
      $map[$field_mapping['field']] = $field_mapping['fid'];
    }
  }
  if(!empty($map)) {
    foreach (field_info_instances('user') as $bundle => $fields) {
      foreach ($fields as $field => $array) {
        if (isset($map[$field]) && $mapped_value = _hybridauth_data_map($_SESSION['hybridauth'], $map[$field])) {
          $form[$field][LANGUAGE_NONE][0]['value']['#default_value'] = $mapped_value;
        }
      }
    }
  }
  */
}