CustomerProfileAlter.php in Drupal Commerce Connector for AvaTax 8
File
src/CustomerProfileAlter.php
View source
<?php
namespace Drupal\commerce_avatax;
use Drupal\address\Plugin\Field\FieldType\AddressItem;
use Drupal\commerce_order\Plugin\Commerce\InlineForm\CustomerProfile;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Access\CsrfTokenGenerator;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
class CustomerProfileAlter implements CustomerProfileAlterInterface {
use DependencySerializationTrait;
protected $config;
protected $avataxLib;
protected $csrf;
protected $time;
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;
}
public function applies(array &$inline_form, FormStateInterface $form_state) {
return $inline_form['#profile_scope'] === 'shipping' && (bool) $this->config
->get('address_validation.enable') === TRUE;
}
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';
$rendered = isset($inline_form['rendered']);
$inline_form['address_suggestion'] = [
'#type' => 'hidden',
];
$endpoint = Url::fromRoute('commerce_avatax.address_validator', [], [
'query' => [
'token' => $this->csrf
->get('commerce-avatax/address-validator'),
],
])
->setAbsolute();
$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;
$inline_form['#element_validate'][] = [
$this,
'submitForm',
];
}
public function submitForm(array &$inline_form, FormStateInterface $form_state) {
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();
}
}
}