You are here

public function ConfigSettingsForm::buildForm in Drupal Commerce Connector for AvaTax 8

Form constructor.

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

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/ConfigSettingsForm.php, line 82

Class

ConfigSettingsForm
Configuration form for AvaTax settings.

Namespace

Drupal\commerce_avatax\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('commerce_avatax.settings');
  $form['configuration'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Configuration'),
    '#open' => TRUE,
    '#id' => 'configuration-wrapper',
  ];
  $form['configuration']['api_mode'] = [
    '#type' => 'radios',
    '#title' => $this
      ->t('API mode:'),
    '#default_value' => $config
      ->get('api_mode'),
    '#options' => [
      'development' => $this
        ->t('Development'),
      'production' => $this
        ->t('Production'),
    ],
    '#required' => TRUE,
    '#description' => $this
      ->t('The mode to use when calculating taxes.'),
  ];
  $form['configuration']['account_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Account ID:'),
    '#default_value' => $config
      ->get('account_id'),
    '#required' => TRUE,
    '#description' => $this
      ->t('The account ID to use when calculating taxes.'),
  ];
  $form['configuration']['license_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('License key:'),
    '#default_value' => $config
      ->get('license_key'),
    '#required' => TRUE,
    '#description' => $this
      ->t('The license key to send to AvaTax when calculating taxes.'),
  ];
  $form['configuration']['company_code'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Company code:'),
    '#default_value' => $config
      ->get('company_code'),
    '#required' => TRUE,
    '#description' => $this
      ->t('The default company code to send to AvaTax when calculating taxes, if company code is not set on the store of a given order.'),
  ];
  $form['configuration']['validate'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Validate credentials'),
    '#limit_validation_errors' => [],
    '#ajax' => [
      'callback' => [
        $this,
        'validateCredentials',
      ],
      'wrapper' => 'configuration-wrapper',
    ],
  ];
  $form['configuration']['address_validation'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Address validation'),
    '#open' => TRUE,
    '#tree' => TRUE,
  ];
  $address_validation_settings = $config
    ->get('address_validation');
  $form['configuration']['address_validation']['enable'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Validate addresses in checkout'),
    '#description' => $this
      ->t('If checked, addresses entered in checkout will be validated.'),
    '#default_value' => $address_validation_settings['enable'],
  ];
  $form['configuration']['address_validation']['countries'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Countries'),
    '#default_value' => $address_validation_settings['countries'],
    '#options' => [
      'CA' => $this
        ->t('Canada'),
      'US' => $this
        ->t('United States'),
    ],
    '#description' => $this
      ->t('Restricts the address validation to the selected countries. If unchecked, US/CA addresses will be validated by default.'),
    '#states' => [
      'visible' => [
        ':input[name="address_validation[enable]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['configuration']['address_validation']['postal_code_match'] = [
    '#type' => 'checkbox',
    '#title' => t('Match on postal code'),
    '#description' => t('Postal codes are 9 digits, but most people enter the first 5 digits, do you want AvaTax to match all 9 digits?'),
    '#default_value' => $address_validation_settings['postal_code_match'],
    '#states' => [
      'visible' => [
        ':input[name="address_validation[enable]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['configuration']['advanced'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Advanced'),
    '#open' => TRUE,
  ];
  $form['configuration']['advanced']['disable_tax_calculation'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Disable tax calculation'),
    '#description' => $this
      ->t("Enable this option if you don't want to use AvaTax for the tax calculation."),
    '#default_value' => $config
      ->get('disable_tax_calculation'),
  ];
  $form['configuration']['advanced']['disable_commit'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Disable document committing.'),
    '#description' => $this
      ->t('Enable this option if you are only using the AvaTax service to display taxes and a backend system is performing the final commit of the tax document.'),
    '#default_value' => $config
      ->get('disable_commit'),
    '#states' => [
      'invisible' => [
        ':input[name="disable_tax_calculation"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['configuration']['advanced']['logging'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable logging'),
    '#description' => $this
      ->t('Enables detailed AvaTax transaction logging.'),
    '#default_value' => $config
      ->get('logging'),
    '#states' => [
      'invisible' => [
        ':input[name="disable_tax_calculation"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['configuration']['advanced']['shipping_tax_code'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Shipping Tax Code'),
    '#default_value' => $config
      ->get('shipping_tax_code'),
    '#required' => TRUE,
    '#description' => $this
      ->t('Browse shipping codes in Avalara\'s <a href="@tax-code-finder" target="_blank">tax code finder</a>.', [
      '@tax-code-finder' => 'https://taxcode.avatax.avalara.com/search?category=Freight&tab=decision_tree',
    ]),
    '#access' => $this->moduleHandler
      ->moduleExists('commerce_shipping'),
    '#states' => [
      'invisible' => [
        ':input[name="disable_tax_calculation"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['configuration']['advanced']['customer_code_field'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Customer code field'),
    '#default_value' => $config
      ->get('customer_code_field'),
    '#options' => [
      'mail' => $this
        ->t('Email'),
      'uid' => $this
        ->t('Customer ID'),
    ],
    '#required' => TRUE,
    '#description' => $this
      ->t('The "customerCode" field to use when the actual customer code field is empty (this setting affects authenticated users only).'),
    '#states' => [
      'invisible' => [
        ':input[name="disable_tax_calculation"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  return parent::buildForm($form, $form_state);
}