You are here

public function TaxRateFormBase::save in Ubercart 8.4

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

uc_tax/src/Form/TaxRateFormBase.php, line 146

Class

TaxRateFormBase
Defines the tax rate add/edit form.

Namespace

Drupal\uc_tax\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $tax_rate = $this
    ->getEntity();

  // Save rate.
  $rate = $form_state
    ->getValue('rate');
  if (substr($rate, -1) == '%') {

    // Rate given in percentage, so convert it to a fraction for storage.
    $tax_rate
      ->setRate(floatval($rate) / 100.0);
  }

  // Save machine names of product types and line item types.
  $tax_rate
    ->setProductTypes(array_filter($form_state
    ->getValue('product_types')));
  $tax_rate
    ->setLineItemTypes(array_filter($form_state
    ->getValue('line_item_types')));

  // @todo When Rules is working in D8 ..
  // Update the name of the associated conditions.
  // $conditions = rules_config_load('uc_tax_' . $form_state->getValue('id'));
  // if ($conditions) {
  //   $conditions->label = $form_state->getValue('name');
  //   $conditions->save();
  // }
  $status = $tax_rate
    ->save();

  // Create an edit link.
  $edit_link = Link::fromTextAndUrl($this
    ->t('Edit'), $tax_rate
    ->toUrl())
    ->toString();
  if ($status == SAVED_UPDATED) {

    // If we edited an existing entity...
    $this
      ->messenger()
      ->addMessage($this
      ->t('Tax rate %label has been updated.', [
      '%label' => $tax_rate
        ->label(),
    ]));
    $this
      ->logger('uc_tax')
      ->notice('Tax rate %label has been updated.', [
      '%label' => $tax_rate
        ->label(),
      'link' => $edit_link,
    ]);
  }
  else {

    // If we created a new entity...
    $this
      ->messenger()
      ->addMessage($this
      ->t('Tax rate %label has been added.', [
      '%label' => $tax_rate
        ->label(),
    ]));
    $this
      ->logger('uc_tax')
      ->notice('Tax rate %label has been added.', [
      '%label' => $tax_rate
        ->label(),
      'link' => $edit_link,
    ]);
  }

  // Redirect the user back to the listing route after the save operation.
  $form_state
    ->setRedirect('entity.uc_tax_rate.collection');
}