You are here

function content_profile_registration_add_profile_form in Content Profile 6

Adds in the profile form of the given content type to the registration form

See also

content_profile_registration_alter_weights()

content_profile_registration_user_register_validate()

content_profile_registration_user_register_submit()

1 call to content_profile_registration_add_profile_form()
content_profile_registration_form_alter in modules/content_profile_registration.module
Implementation of hook_form_alter().

File

modules/content_profile_registration.module, line 105
Allows exposure and processing of content_profile node fields at user registration

Code

function content_profile_registration_add_profile_form($type, &$form, &$form_state) {

  // Initialize new node and add in its form.
  $node = array(
    'uid' => 0,
    'name' => '',
    'type' => $type,
  );

  // Get the original node form.
  $node_form = drupal_retrieve_form($type . '_node_form', $form_state, $node);
  drupal_prepare_form($type . '_node_form', $node_form, $form_state);
  $node_form += array(
    '#field_info' => array(),
  );
  $form_add = array();

  // If non-CCK form elements are hidden, only copy over the CCK stuff
  if (in_array('other', content_profile_get_settings($type, 'registration_hide'))) {
    foreach ($node_form['#field_info'] as $field_name => $info) {
      if (isset($node_form[$field_name])) {
        $form_add[$field_name] = $node_form[$field_name];
      }
    }

    // Copy over any fieldgroups
    $keys = array_keys($node_form);
    foreach ($keys as $key) {
      if (stristr($key, 'group_')) {
        $form_add[$key] = $node_form[$key];
      }
    }

    // Add the title
    $form_add['title'] = $node_form['title'];

    // Set this to the values of one node, as it might be need by some #ahah callbacks
    $form_add['#node'] = $node_form['#node'];
    $form_add['type'] = $node_form['type'];
  }
  else {
    foreach (array(
      'uid',
      'name',
      'author',
      'buttons',
      'language',
      '#theme',
      'options',
    ) as $key) {
      unset($node_form[$key]);
    }
    $form_add = $node_form;
  }

  // Hide fields as configured
  foreach (content_profile_get_settings($type, 'registration_hide') as $field_name) {
    if (module_exists('fieldgroup') && ($group_name = _fieldgroup_field_get_group($type, $field_name))) {
      unset($form_add[$group_name][$field_name]);
      if (count(element_children($form_add[$group_name])) == 0) {
        unset($form_add[$group_name]);
      }
    }
    else {
      unset($form_add[$field_name]);
    }
  }

  // Add in the new form elements into $form.
  $form += array(
    '#field_info' => array(),
  );
  $form['#field_info'] += $node_form['#field_info'];
  $form += $form_add;

  // Add in further callbacks needed, if not yet done.
  if (!isset($form['#content_profile_weights'])) {
    $form['#submit'][] = 'content_profile_registration_user_register_submit';
    $form['#validate'][] = 'content_profile_registration_user_register_validate';
    $form['#pre_render'][] = 'content_profile_registration_alter_weights';
  }

  // Care for the weights: Make use of the content types weight and sort the fields in behalf
  // The weights will be applied by the pre_render callback.
  $form += array(
    '#content_profile_weights' => array(),
  );
  $weight = content_profile_get_settings($type, 'weight') + 1;
  foreach (element_children($form_add) as $key) {
    $form['#content_profile_weights'] += array(
      $key => $weight,
    );
  }

  // Set the enctype, if necessary.
  if (isset($node_form['#attributes']['enctype'])) {
    $form['#attributes']['enctype'] = $node_form['#attributes']['enctype'];
  }
}