You are here

public function PriceListForm::save in Commerce Pricelist 8.2

Same name and namespace in other branches
  1. 8 src/Form/PriceListForm.php \Drupal\commerce_pricelist\Form\PriceListForm::save()

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

src/Form/PriceListForm.php, line 142

Class

PriceListForm

Namespace

Drupal\commerce_pricelist\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\commerce_pricelist\Entity\PriceListInterface $price_list */
  $price_list = $this->entity;

  // Don't persist customer values that are not going to be used.
  $customer_eligibility = $form_state
    ->getValue('customer_eligibility');
  if ($customer_eligibility === 'everyone') {
    $price_list
      ->set('customers', NULL);
    $price_list
      ->setCustomerRoles([]);
  }
  elseif ($customer_eligibility === 'customers') {
    $price_list
      ->setCustomerRoles([]);
  }
  elseif ($customer_eligibility === 'customer_roles') {
    $price_list
      ->set('customers', NULL);
  }
  $price_list
    ->save();
  $this
    ->postSave($price_list, $this->operation);
  $this
    ->messenger()
    ->addMessage($this
    ->t('Saved the %label price list.', [
    '%label' => $price_list
      ->label(),
  ]));
  if (!empty($form_state
    ->getTriggeringElement()['#continue'])) {
    $form_state
      ->setRedirect('entity.commerce_pricelist_item.collection', [
      'commerce_pricelist' => $price_list
        ->id(),
    ]);
  }
  else {
    $form_state
      ->setRedirect('entity.commerce_pricelist.collection');
  }
}