You are here

function commerce_order_form_profile_type_form_alter in Commerce Core 8.2

Implements hook_form_FORM_ID_alter() for 'profile_type_form'.

File

modules/order/commerce_order.module, line 271
Defines the Order entity and associated features.

Code

function commerce_order_form_profile_type_form_alter(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\profile\Entity\ProfileTypeInterface $profile_type */
  $profile_type = $form_state
    ->getFormObject()
    ->getEntity();
  $customer_flag = $profile_type
    ->getThirdPartySetting('commerce_order', 'customer_profile_type');
  $address_has_data = FALSE;
  if ($customer_flag && !$profile_type
    ->isNew()) {

    /** @var \Drupal\commerce\ConfigurableFieldManagerInterface $configurable_field_manager */
    $configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');
    $address_field_definition = commerce_order_build_address_field_definition($profile_type
      ->id());
    $address_has_data = $configurable_field_manager
      ->hasData($address_field_definition);
  }
  $form['#tree'] = TRUE;
  $form['commerce_order'] = [
    '#type' => 'container',
    '#weight' => 1,
  ];
  $form['commerce_order']['customer_profile_type'] = [
    '#type' => 'checkbox',
    '#title' => t('Profiles of this type represent Commerce customer profiles'),
    '#description' => t("Used to store the customer's billing or shipping information."),
    '#default_value' => $customer_flag,
    // The flag is always TRUE for the profile type provided by Commerce.
    '#disabled' => $profile_type
      ->id() == 'customer' || $address_has_data,
  ];
  $form['actions']['submit']['#submit'][] = 'commerce_order_profile_type_form_submit';
}