You are here

public function ProductTypeForm::validateForm in Commerce Core 8.2

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

modules/product/src/Form/ProductTypeForm.php, line 157

Class

ProductTypeForm

Namespace

Drupal\commerce_product\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $this
    ->validateTraitForm($form, $form_state);
  if (empty($form_state
    ->getValue('variationType'))) {
    $id = $form_state
      ->getValue('id');
    if (!empty($this->entityTypeManager
      ->getStorage('commerce_product_variation_type')
      ->load($id))) {
      $form_state
        ->setError($form['variationType'], $this
        ->t('A product variation type with the machine name @id already exists. Select an existing product variation type or change the machine name for this product type.', [
        '@id' => $id,
      ]));
    }
    if ($this->moduleHandler
      ->moduleExists('commerce_order')) {
      $order_item_type_ids = $this
        ->getOrderItemTypeIds();
      if (empty($order_item_type_ids)) {
        $form_state
          ->setError($form['variationType'], $this
          ->t('A new product variation type cannot be created, because no order item types were found. Select an existing product variation type or retry after creating a new order item type.'));
      }
    }
  }
}