You are here

function profile2_regpath_attach_profile_fields in Profile2 Registration Path 7.2

Same name and namespace in other branches
  1. 7 profile2_regpath.inc \profile2_regpath_attach_profile_fields()
1 call to profile2_regpath_attach_profile_fields()
profile2_regpath_form_alter in ./profile2_regpath.module
Implements hook_form_alter().

File

./profile2_regpath.inc, line 11
Modifications to the user registration form.

Code

function profile2_regpath_attach_profile_fields(&$form, &$form_state, $form_id, $profile_types = NULL) {

  // Check to see if the form is being rebuilt after an AJAX request.
  // If so, we will have lost the $profile_types parameter, and need to reacquire it.
  $menu_item = menu_get_item();
  if (!$profile_types && ($menu_item['path'] == 'system/ajax' || $menu_item['path'] == 'file/ajax')) {
    $url = drupal_parse_url($form['#action']);
    $path = end(explode('/', str_replace('/register', '', $url['path'])));
    $profile_types = profile2_regpath_regpath_load_multiple(array(
      'path' => $path,
    ));
  }
  if ($profile_types != NULL) {

    // Prepare variables for roles.
    $user_roles = user_roles(TRUE);
    $roles = array();

    // Attach profile(s) to user/register form.
    foreach ($profile_types as $type_name => $value) {

      // Get profile object.
      $profile_type = profile2_get_types($type_name);

      // If this profile has not already been attached.
      if (empty($form_state['profiles'][$type_name])) {
        $profile = profile_create(array(
          'type' => $type_name,
        ));
        $form_state['profiles'][$type_name] = $profile;
        $misc = unserialize($profile_types[$type_name]->misc);

        // Wrap each profile form in a fieldset.
        if ($misc['fieldset_wrap']) {
          $form['profile_' . $type_name] = array(
            '#type' => 'fieldset',
            '#title' => check_plain($profile_type->label),
          );
        }

        // Set Form API #weight attribute for profile.
        $form['profile_' . $type_name]['#weight'] = $profile_type->weight;

        // Attach custom confirmation message to form for later display.
        if (isset($misc['confirmation_display'])) {
          $_SESSION['profile2_regpath']['confirmation_message'] = $misc['confirmation_message'];
        }
      }

      // Add appropriate user roles.
      $profile_roles = unserialize($value->roles);
      foreach ($profile_roles as $rid => $value) {

        // Add role to roles array if it isn't already there.
        if ($value != 0 && !array_key_exists($rid, $form['account']['roles'])) {
          $form['account']['roles'][$rid] = array(
            '#type' => 'checkbox',
            '#title' => check_plain($user_roles[$rid]),
            '#default_value' => TRUE,
            '#disabled' => user_access('administer users') ? FALSE : TRUE,
          );
        }
      }
    }

    // Attach the profile fields via profile2.
    // @todo Add test for AJAX fields after profile2_attach_form() has been called.
    profile2_attach_form($form, $form_state);
  }
}