You are here

function commerce_shipping_order_type_form_validate in Commerce Shipping 8.2

Validation handler for commerce_shipping_form_commerce_order_type_form_alter().

1 string reference to 'commerce_shipping_order_type_form_validate'
commerce_shipping_form_commerce_order_type_form_alter in ./commerce_shipping.module
Implements hook_form_FORM_ID_alter() for 'commerce_order_type_form'.

File

./commerce_shipping.module, line 186
Provides core shipping functionality.

Code

function commerce_shipping_order_type_form_validate(array $element, FormStateInterface $form_state) {

  /** @var \Drupal\commerce_order\Entity\OrderTypeInterface $order_type */
  $order_type = $form_state
    ->getFormObject()
    ->getEntity();
  $previous_value = $order_type
    ->getThirdPartySetting('commerce_shipping', 'shipment_type');
  $settings = $form_state
    ->getValue([
    'commerce_shipping',
  ]);

  /** @var \Drupal\commerce\ConfigurableFieldManagerInterface $configurable_field_manager */
  $configurable_field_manager = \Drupal::service('commerce.configurable_field_manager');

  // Don't allow shipping to be disabled if there's data in the field.
  if ($previous_value && !$settings['enable_shipping']) {
    $field_definition = commerce_shipping_build_shipment_field_definition($order_type
      ->id());
    if ($configurable_field_manager
      ->hasData($field_definition)) {
      $form_state
        ->setError($element['enable_shipping'], t('Shipping cannot be disabled until all orders with shipment data are deleted.'));
    }
  }
}