You are here

function profile2_attach_form in Profile 2 7.2

Same name and namespace in other branches
  1. 7 profile2.module \profile2_attach_form()

Attaches the profile forms of the profiles set in $form_state['profiles'].

Modules may alter the profile2 entity form regardless to which form it is attached by making use of hook_form_profile2_form_alter().

Parameters

$form: The form to which to attach the profile2 form. For each profile the form is added to

$form['profile_' . $profile->type];

. This helper also adds in a validation and a submit handler caring for the attached profile forms.

See also

hook_form_profile2_form_alter()

profile2_form_validate_handler()

profile2_form_submit_handler()

3 calls to profile2_attach_form()
profile2_form in contrib/profile2_page.inc
The profile edit form.
profile2_form_user_profile_form_alter in ./profile2.module
Implements hook_form_FORM_ID_alter() for the user edit form.
profile2_form_user_register_form_alter in ./profile2.module
Implements hook_form_FORM_ID_alter() for the registration form.

File

./profile2.module, line 677
Support for configurable user profiles.

Code

function profile2_attach_form(&$form, &$form_state) {
  $allowed = user_access('allow to choose profile2 revision creation') || user_access('administer profiles');
  foreach ($form_state['profiles'] as $type => $profile) {

    // Add revision fields.
    $profile_type = profile2_get_types($type);
    if ($allowed || $profile_type->data['revisions']) {
      $form['profile_' . $profile->type]['revision_information'] = array(
        '#type' => 'fieldset',
        '#title' => t('Revision information'),
        '#collapsible' => FALSE,
        '#attributes' => array(
          'class' => array(
            'user-profile-form-revision-information',
          ),
        ),
        '#weight' => 30,
      );
      $form['profile_' . $profile->type]['revision_information']['revision'] = array(
        '#type' => $allowed ? 'checkbox' : 'value',
        '#title' => t('Create new revision'),
        '#default_value' => $profile_type->data['revisions'],
      );

      //HTML element name of the checkbox, for use by #states below.
      $checkbox_name = "profile_{$profile->type}[revision_information][revision]";
      $form['profile_' . $profile->type]['revision_information']['log'] = array(
        '#type' => 'textarea',
        '#title' => t('Revision log message'),
        '#rows' => 4,
        '#description' => t('Provide an explanation of the changes you are making. This will help other authors understand your motivations.'),
        '#states' => array(
          'invisible' => array(
            'input[name="' . $checkbox_name . '"]' => array(
              'checked' => FALSE,
            ),
          ),
        ),
      );
    }

    // If this user does not yet have a profile of this type, then force a new
    // revision.
    if (is_null($profile->pid)) {
      $form['profile_' . $profile->type]['revision_information']['revision']['#default_value'] = 1;
      $form['profile_' . $profile->type]['revision_information']['log']['#value'] = t('Initial revision.');
      $form['profile_' . $profile->type]['revision_information']['#access'] = FALSE;
    }
    $form['profile_' . $profile->type]['#tree'] = TRUE;
    $form['profile_' . $profile->type]['#parents'] = array(
      'profile_' . $profile->type,
    );
    field_attach_form('profile2', $profile, $form['profile_' . $profile->type], $form_state);
    if (user_access('administer profile types')) {
      if (count(field_info_instances('profile2', $profile->type)) == 0) {
        $form['profile_' . $profile->type]['message'] = array(
          '#markup' => t('No fields have been associated with this profile type. Go to the <a href="!url">Profile types</a> page to add some fields.', array(
            '!url' => url('admin/structure/profiles'),
          )),
        );
      }
    }

    // Make sure we don't have duplicate pre render callbacks.
    $form['profile_' . $profile->type]['#pre_render'] = array_unique($form['profile_' . $profile->type]['#pre_render']);

    // Provide a central place for modules to alter the profile forms, but
    // skip that in case the caller cares about invoking the hooks.
    // @see profile2_form().
    if (!isset($form_state['profile2_skip_hook'])) {
      $hooks[] = 'form_profile2_edit_' . $type . '_form';
      $hooks[] = 'form_profile2_form';
      drupal_alter($hooks, $form, $form_state);
    }
  }
  $form['#validate'][] = 'profile2_form_validate_handler';

  // Default name of user registry form callback.
  $register_submit_callback = 'user_register_submit';

  // LoginToBoggan module replaces default user_register_submit() callback
  // with his own. So if this module enabled we need to track his callback
  // instead one that comes from the User module.
  if (module_exists('logintoboggan')) {
    $register_submit_callback = 'logintoboggan_user_register_submit';
  }

  // Search for key of user register submit callback.
  if (!empty($form['#submit']) && is_array($form['#submit'])) {
    $submit_key = array_search($register_submit_callback, $form['#submit']);
  }

  // Add these hooks only when needed, and ensure they are not added twice.
  if (isset($submit_key) && $submit_key !== FALSE && !in_array('profile2_form_before_user_register_submit_handler', $form['#submit'])) {

    // Insert submit callback right before the user register submit callback.
    // Needs for disabling email notification during user registration.
    array_splice($form['#submit'], $submit_key, 0, array(
      'profile2_form_before_user_register_submit_handler',
    ));

    // Add a submit callback right after the user register submit callback.
    // This is needed for creation of a new user profile.
    array_splice($form['#submit'], $submit_key + 2, 0, array(
      'profile2_form_submit_handler',
    ));

    // Insert submit handler right after the creation of new user profile.
    // This is needed for sending email which was blocked during registration.
    array_splice($form['#submit'], $submit_key + 3, 0, array(
      'profile2_form_after_user_register_submit_handler',
    ));
  }
  else {

    // Fallback if some contrib module removes user register submit callback
    // from form submit functions.
    $form['#submit'][] = 'profile2_form_submit_handler';
  }
}