You are here

public function USPSBase::buildConfigurationForm in Commerce USPS 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

array $form: An associative array containing the initial structure of the plugin form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides ShippingMethodBase::buildConfigurationForm

File

src/Plugin/Commerce/ShippingMethod/USPSBase.php, line 86

Class

USPSBase

Namespace

Drupal\commerce_usps\Plugin\Commerce\ShippingMethod

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildConfigurationForm($form, $form_state);

  // Select all services by default.
  if (empty($this->configuration['services'])) {
    $service_ids = array_keys($this->services);
    $this->configuration['services'] = array_combine($service_ids, $service_ids);
  }
  $description = $this
    ->t('Update your USPS API information.');
  if (!$this
    ->isConfigured()) {
    $description = $this
      ->t('Fill in your USPS API information.');
  }
  $form['api_information'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('API information'),
    '#description' => $description,
    '#weight' => $this
      ->isConfigured() ? 10 : -10,
    '#open' => !$this
      ->isConfigured(),
  ];
  $form['api_information']['user_id'] = [
    '#type' => 'textfield',
    '#title' => t('User ID'),
    '#default_value' => $this->configuration['api_information']['user_id'],
    '#required' => TRUE,
  ];
  $form['api_information']['password'] = [
    '#type' => 'textfield',
    '#title' => t('Password'),
    '#default_value' => $this->configuration['api_information']['password'],
    '#required' => TRUE,
  ];
  $form['api_information']['mode'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Mode'),
    '#description' => $this
      ->t('Choose whether to use test or live mode.'),
    '#options' => [
      'test' => $this
        ->t('Test'),
      'live' => $this
        ->t('Live'),
    ],
    '#default_value' => $this->configuration['api_information']['mode'],
  ];
  $form['rate_options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Rate Options'),
    '#description' => $this
      ->t('Additional options for USPS rate requests.'),
  ];
  $form['rate_options']['rate_class'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Rate Class'),
    '#description' => $this
      ->t('The rate class to use for shipping rate prices.'),
    '#default_value' => $this->configuration['rate_options']['rate_class'],
    '#options' => [
      'retail' => $this
        ->t('Retail (default)'),
      'online' => $this
        ->t('Online'),
      'commercial' => $this
        ->t('Commercial'),
      'commercial_plus' => $this
        ->t('Commercial Plus'),
    ],
  ];
  $form['options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('USPS Options'),
    '#description' => $this
      ->t('Additional options for USPS'),
  ];
  $form['options']['tracking_url'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Tracking URL base'),
    '#description' => $this
      ->t('The base URL for assembling a tracking URL. If the [tracking_code] token is omitted, the code will be appended to the end of the URL (e.g. "https://tools.usps.com/go/TrackConfirmAction?tLabels=123456789")'),
    '#default_value' => $this->configuration['options']['tracking_url'],
  ];
  $form['options']['log'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Log the following messages for debugging'),
    '#options' => [
      'request' => $this
        ->t('API request messages'),
      'response' => $this
        ->t('API response messages'),
    ],
    '#default_value' => $this->configuration['options']['log'],
  ];
  return $form;
}