You are here

function commerce_customer_customer_profile_form in Commerce Core 7

Form callback: create or edit a customer profile.

Parameters

$profile: The profile object to edit or for a create form an empty profile object with only a profile type defined.

1 string reference to 'commerce_customer_customer_profile_form'
commerce_customer_ui_forms in modules/customer/commerce_customer_ui.module
Implements hook_forms().

File

modules/customer/includes/commerce_customer_profile.forms.inc, line 16
Forms for creating / editing and deleting customer profiles.

Code

function commerce_customer_customer_profile_form($form, &$form_state, $profile) {

  // Ensure this include file is loaded when the form is rebuilt from the cache.
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'commerce_customer') . '/includes/commerce_customer_profile.forms.inc';

  // Ensure the owner name is accessible if the uid is set.
  if (!empty($profile->uid) && ($owner = user_load($profile->uid))) {
    $profile->name = $owner->name;
  }
  if (empty($profile->created)) {
    $profile->date = format_date(REQUEST_TIME, 'custom', 'Y-m-d H:i:s O');
  }

  // Add the field related form elements.
  $form_state['customer_profile'] = $profile;
  field_attach_form('commerce_customer_profile', $profile, $form, $form_state);
  $form['additional_settings'] = array(
    '#type' => 'vertical_tabs',
    '#weight' => 99,
  );

  // Add the user account and e-mail fields.
  $form['user'] = array(
    '#type' => 'fieldset',
    '#title' => t('User information'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'commerce_customer') . '/commerce_customer.js',
        array(
          'type' => 'setting',
          'data' => array(
            'anonymous' => variable_get('anonymous', t('Anonymous')),
          ),
        ),
      ),
    ),
    '#weight' => 20,
  );
  $form['user']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Owned by'),
    '#description' => t('Leave blank for %anonymous.', array(
      '%anonymous' => variable_get('anonymous', t('Anonymous')),
    )),
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
    '#default_value' => !empty($profile->name) ? $profile->name : '',
    '#weight' => -1,
  );

  // Add the status of the profile.
  $form['profile_status'] = array(
    '#type' => 'fieldset',
    '#title' => t('Status'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#group' => 'additional_settings',
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'commerce_customer') . '/commerce_customer.js',
      ),
    ),
    '#weight' => 30,
  );
  $form['profile_status']['status'] = array(
    '#type' => 'radios',
    '#title' => t('Status'),
    '#description' => t('Disabled profiles will not be visible to customers in options lists.'),
    '#options' => array(
      '1' => t('Active'),
      '0' => t('Disabled'),
    ),
    '#default_value' => $profile->status,
    '#required' => TRUE,
    '#disabled' => !commerce_customer_profile_type_load($profile->type),
    '#weight' => 35,
  );

  // Disable the status field if the customer profile type has been disabled.
  if (!commerce_customer_profile_type_load($profile->type)) {
    $form['profile_status']['status']['#disabled'] = TRUE;
    $form['profile_status']['status']['#description'] .= '<br />' . t('This profile is of a type that is no longer available, so its status cannot be adjusted.');
  }

  // We add the form's #submit array to this button along with the actual submit
  // handler to preserve any submit handlers added by a form callback_wrapper.
  $submit = array();
  if (!empty($form['#submit'])) {
    $submit += $form['#submit'];
  }
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save profile'),
    '#submit' => array_merge($submit, array(
      'commerce_customer_customer_profile_form_submit',
    )),
  );

  // We append the validate handler to #validate in case a form callback_wrapper
  // is used to add validate handlers earlier.
  $form['#validate'][] = 'commerce_customer_customer_profile_form_validate';
  return $form;
}