You are here

public function CustomerProfileAlter::submitForm in Drupal Commerce Connector for AvaTax 8

Submits the inline form.

Parameters

array $inline_form: The inline form.

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

File

src/CustomerProfileAlter.php, line 134

Class

CustomerProfileAlter
The customer profile alter for address validation.

Namespace

Drupal\commerce_avatax

Code

public function submitForm(array &$inline_form, FormStateInterface $form_state) {

  // Do not perform additional saves to the profile unless the element was
  // completed submitted. This submit handler executes on validation and
  // therefore runs during AJAX processing.
  if (!$form_state
    ->isSubmitted()) {
    return;
  }
  assert($inline_form['#inline_form'] instanceof CustomerProfile);
  $inline_form_values = $form_state
    ->getValue($inline_form['#parents']);
  $address_suggestion = $inline_form_values['address_suggestion'] ?? 'original';
  if ($address_suggestion !== 'original') {
    $profile = $inline_form['#inline_form']
      ->getEntity();
    $address = $profile
      ->get('address')
      ->first();
    assert($address instanceof AddressItem);
    $suggestion = Json::decode(base64_decode($address_suggestion));
    $values = array_merge($address
      ->toArray(), $suggestion);
    $address
      ->setValue($values);
    $profile
      ->save();
  }
}