You are here

public function ProductAttributeForm::save in Commerce Core 8.2

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

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

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

modules/product/src/Form/ProductAttributeForm.php, line 394

Class

ProductAttributeForm

Namespace

Drupal\commerce_product\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $status = $this->entity
    ->save();
  $original_variation_types = $form_state
    ->getValue('original_variation_types', []);
  $variation_types = array_filter($form_state
    ->getValue('variation_types', []));
  $disabled_variation_types = $form_state
    ->getValue('disabled_variation_types', []);
  $variation_types = array_unique(array_merge($disabled_variation_types, $variation_types));
  $selected_variation_types = array_diff($variation_types, $original_variation_types);
  $unselected_variation_types = array_diff($original_variation_types, $variation_types);
  if ($selected_variation_types) {
    foreach ($selected_variation_types as $selected_variation_type) {
      $this->attributeFieldManager
        ->createField($this->entity, $selected_variation_type);
    }
  }
  if ($unselected_variation_types) {
    foreach ($unselected_variation_types as $unselected_variation_type) {
      $this->attributeFieldManager
        ->deleteField($this->entity, $unselected_variation_type);
    }
  }
  if ($this->moduleHandler
    ->moduleExists('content_translation')) {
    $translation_manager = \Drupal::service('content_translation.manager');

    // Logic from content_translation_language_configuration_element_submit().
    $enabled = $form_state
      ->getValue('enable_value_translation');
    if ($translation_manager
      ->isEnabled('commerce_product_attribute_value', $this->entity
      ->id()) != $enabled) {
      $translation_manager
        ->setEnabled('commerce_product_attribute_value', $this->entity
        ->id(), $enabled);
      $this->entityTypeManager
        ->clearCachedDefinitions();
      \Drupal::service('router.builder')
        ->setRebuildNeeded();
    }
  }
  if ($status == SAVED_NEW) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Created the %label product attribute.', [
      '%label' => $this->entity
        ->label(),
    ]));

    // Send the user to the edit form to create the attribute values.
    $form_state
      ->setRedirectUrl($this->entity
      ->toUrl('edit-form'));
  }
  else {
    $this
      ->saveValues($form, $form_state);
    $this
      ->messenger()
      ->addMessage($this
      ->t('Updated the %label product attribute.', [
      '%label' => $this->entity
        ->label(),
    ]));
    $form_state
      ->setRedirectUrl($this->entity
      ->toUrl('collection'));
  }
}