You are here

class CustomerProfileAlter in Drupal Commerce Connector for AvaTax 8

The customer profile alter for address validation.

Hierarchy

Expanded class hierarchy of CustomerProfileAlter

1 file declares its use of CustomerProfileAlter
CustomerProfileAlterTest.php in tests/src/Unit/CustomerProfileAlterTest.php
1 string reference to 'CustomerProfileAlter'
commerce_avatax.services.yml in ./commerce_avatax.services.yml
commerce_avatax.services.yml
1 service uses CustomerProfileAlter
commerce_avatax.customer_profile_alter in ./commerce_avatax.services.yml
Drupal\commerce_avatax\CustomerProfileAlter

File

src/CustomerProfileAlter.php, line 18

Namespace

Drupal\commerce_avatax
View source
class CustomerProfileAlter implements CustomerProfileAlterInterface {
  use DependencySerializationTrait;

  /**
   * AvaTax settings.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $config;

  /**
   * The AvaTax library.
   *
   * @var \Drupal\commerce_avatax\AvataxLib
   */
  protected $avataxLib;

  /**
   * The CSRF token generator.
   *
   * @var \Drupal\Core\Access\CsrfTokenGenerator
   */
  protected $csrf;

  /**
   * The time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs a new CustomerProfileAlter object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\commerce_avatax\AvataxLibInterface $avatax_lib
   *   The AvaTax library.
   * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf
   *   The CSRF token generator.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, AvataxLibInterface $avatax_lib, CsrfTokenGenerator $csrf, TimeInterface $time) {
    $this->config = $config_factory
      ->get('commerce_avatax.settings');
    $this->avataxLib = $avatax_lib;
    $this->csrf = $csrf;
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(array &$inline_form, FormStateInterface $form_state) {
    return $inline_form['#profile_scope'] === 'shipping' && (bool) $this->config
      ->get('address_validation.enable') === TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function alter(array &$inline_form, FormStateInterface $form_state) {
    assert($inline_form['#inline_form'] instanceof CustomerProfile);
    $inline_form['#attached']['library'][] = 'commerce_avatax/address';
    $inline_form['#attributes']['class'][] = 'avatax-form';

    // Determine if we have existing profile or we adding new / editing
    // existing one.
    $rendered = isset($inline_form['rendered']);

    // Used to hold the proposed address suggestion.
    $inline_form['address_suggestion'] = [
      '#type' => 'hidden',
    ];
    $endpoint = Url::fromRoute('commerce_avatax.address_validator', [], [
      'query' => [
        'token' => $this->csrf
          ->get('commerce-avatax/address-validator'),
      ],
    ])
      ->setAbsolute();

    // Set ID for JS more precise targeting.
    $js_data = [
      'inline_id' => $inline_form['#id'],
      'countries' => $this->config
        ->get('address_validation.countries'),
      'rendered' => $rendered,
      'endpoint' => $endpoint
        ->toString(),
    ];
    $profile = $inline_form['#inline_form']
      ->getEntity();
    if ($profile !== NULL) {
      $address = $profile
        ->get('address')
        ->first();
      assert($address instanceof AddressItem);
      $js_data['address'] = $address
        ->toArray();
      $js_data['country'] = $address
        ->getCountryCode() ?? $inline_form['address']['widget'][0]['address']['#default_value']['country_code'];
      $js_data['fields'] = [
        'address_line1',
        'address_line2',
        'locality',
        'administrative_area',
        'country_code',
        'postal_code',
      ];
    }
    $inline_form['#attached']['drupalSettings']['commerceAvatax'] = $js_data;

    // Add ours validation and submit handlers.
    $inline_form['#element_validate'][] = [
      $this,
      'submitForm',
    ];
  }

  /**
   * Submits the inline form.
   *
   * @param array $inline_form
   *   The inline form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  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();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomerProfileAlter::$avataxLib protected property The AvaTax library.
CustomerProfileAlter::$config protected property AvaTax settings.
CustomerProfileAlter::$csrf protected property The CSRF token generator.
CustomerProfileAlter::$time protected property The time service.
CustomerProfileAlter::alter public function Alters the inline form to add address validation logic. Overrides CustomerProfileAlterInterface::alter
CustomerProfileAlter::applies public function Gets whether the given customer profile inline form is supported. Overrides CustomerProfileAlterInterface::applies
CustomerProfileAlter::submitForm public function Submits the inline form.
CustomerProfileAlter::__construct public function Constructs a new CustomerProfileAlter object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2