You are here

function gprofile_form_group_membership_form_alter in Group 7

Implements hook_form_FORM_ID_alter().

File

modules/gprofile/gprofile.module, line 28
Main module code for the Group member profiles project.

Code

function gprofile_form_group_membership_form_alter(&$form, &$form_state) {
  $group_membership = $form_state['group_membership'];
  foreach (gprofile_types() as $name => $gprofile_type) {

    // We use GroupMembership::hasPermission because we could be dealing with
    // an unsaved group membership here (join or request forms).
    if ($group_membership
      ->hasPermission("use {$name} profile")) {
      $field_name = "gprofile_{$name}";
      $form[$field_name] = array(
        '#title' => $gprofile_type
          ->label(),
        '#type' => 'fieldset',
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        // Required for field_attach_form().
        // We can't set just #parents because form_builder checks for #tree
        // as well, otherwise not properly setting #parents further down the
        // line.
        '#tree' => TRUE,
        '#parents' => array(
          $field_name,
        ),
      );

      // Try to load the existing GProfile for this member.
      $gprofile = gprofile_load($group_membership->gid, $group_membership->uid, $name);

      // If unavailable, create a blank one instead.
      if (empty($gprofile)) {
        $values = array(
          'mid' => !empty($group_membership->mid) ? $group_membership->mid : -1,
          'type' => $name,
        );
        $gprofile = entity_create('gprofile', $values);
      }

      // Load fields based on profile type.
      field_attach_form('gprofile', $gprofile, $form[$field_name], $form_state, entity_language('gprofile', $gprofile));

      // Add the profile to the form state.
      $form_state['gprofiles'][$field_name] = $gprofile;
    }
  }

  // If we added form elements, we need to handle their submissions too.
  if (!empty($form_state['gprofiles'])) {
    $form['#validate'][] = 'gprofile_group_membership_validate';
    $form['#submit'][] = 'gprofile_group_membership_submit';
  }
}