You are here

public function FedEx::buildConfigurationForm in Commerce FedEx 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/FedEx.php, line 226

Class

FedEx
Provides the FedEx shipping method.

Namespace

Drupal\commerce_fedex\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);
  }
  $form['api_information'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('API information'),
    '#description' => $this
      ->isConfigured() ? $this
      ->t('Update your FedEx API information.') : $this
      ->t('Fill in your FedEx API information.'),
    '#weight' => $this
      ->isConfigured() ? 10 : -10,
    '#open' => !$this
      ->isConfigured(),
  ];
  $form['api_information']['api_key'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('API key'),
    '#description' => $this
      ->t('Enter your FedEx API key.'),
    '#default_value' => $this->configuration['api_information']['api_key'],
  ];
  $form['api_information']['api_password'] = [
    '#type' => 'password',
    '#title' => $this
      ->t('API password'),
    '#description' => $this
      ->t('Enter your FedEx API password only if you wish to change its value.'),
    '#default_value' => '',
  ];
  $form['api_information']['account_number'] = [
    '#type' => 'number',
    '#min' => 0,
    '#step' => 1,
    '#title' => $this
      ->t('Account number'),
    '#description' => $this
      ->t('Enter your FedEx account number.'),
    '#default_value' => $this->configuration['api_information']['account_number'],
  ];
  $form['api_information']['meter_number'] = [
    '#type' => 'number',
    '#min' => 0,
    '#step' => 1,
    '#title' => $this
      ->t('Meter number'),
    '#description' => $this
      ->t('Enter your FedEx meter number.'),
    '#default_value' => $this->configuration['api_information']['meter_number'],
  ];
  $form['api_information']['mode'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Mode'),
    '#description' => $this
      ->t('Choose whether to use the test or live mode.'),
    '#options' => [
      'test' => $this
        ->t('Test'),
      'live' => $this
        ->t('Live'),
    ],
    '#default_value' => $this->configuration['api_information']['mode'],
  ];
  $form['options'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Fedex Options'),
    '#description' => $this
      ->t('Additional options for Fedex'),
  ];
  $form['options']['packaging'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Packaging strategy'),
    '#description' => $this
      ->t('Select your packaging strategy. "All items in one box" will ignore package type and product dimensions, and assume all items go in one box. "Each item in its own box" will create a box for each line item in the order, "Calculate" Will attempt to figure out how many boxes are needed based on package type volumes and product volumes similar to commerce_fedex 7.x'),
    '#options' => [
      static::PACKAGE_ALL_IN_ONE => $this
        ->t("All items in one box"),
      static::PACKAGE_INDIVIDUAL => $this
        ->t("Each item in its own box"),
      static::PACKAGE_CALCULATE => $this
        ->t("Calculate"),
    ],
    '#default_value' => $this->configuration['options']['packaging'],
  ];
  $form['options']['rate_request_type'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Pricing options'),
    '#description' => $this
      ->t('Select the pricing option to use when requesting a rate quote. Note that discounted rates are only available when sending production requests.'),
    '#options' => [
      RateRequestType::VALUE_LIST => $this
        ->t('Standard pricing (LIST)'),
      RateRequestType::VALUE_NONE => $this
        ->t("This FedEx account's discounted pricing (ACCOUNT)"),
    ],
    '#default_value' => $this->configuration['options']['rate_request_type'],
  ];
  $form['options']['dropoff'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Dropoff type'),
    '#description' => $this
      ->t('Default dropoff/pickup location for your FedEx shipments'),
    '#options' => static::enumToList(DropoffType::getValidValues()),
    '#default_value' => $this->configuration['options']['dropoff'],
  ];
  $form['options']['insurance'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Include insurance'),
    '#description' => $this
      ->t('Include insurance value of shippable line items in FedEx rate requests'),
    '#default_value' => $this->configuration['options']['insurance'],
  ];
  $form['options']['rate_multiplier'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Rate multiplier'),
    '#description' => $this
      ->t('A number that each rate returned from FedEx will be multiplied by. For example, enter 1.5 to mark up shipping costs to 150%.'),
    '#min' => 0.1,
    '#step' => 0.1,
    '#size' => 5,
    '#default_value' => $this->configuration['options']['rate_multiplier'],
  ];
  $form['options']['round'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Round type'),
    '#description' => $this
      ->t('Choose how the shipping rate should be rounded.'),
    '#options' => [
      PHP_ROUND_HALF_UP => 'Half up',
      PHP_ROUND_HALF_DOWN => 'Half down',
      PHP_ROUND_HALF_EVEN => 'Half even',
      PHP_ROUND_HALF_ODD => 'Half odd',
    ],
    '#default_value' => $this->configuration['options']['round'],
  ];
  $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'],
  ];
  foreach ($this->fedExServiceManager
    ->getDefinitions() as $plugin_id => $definition) {

    /** @var \Drupal\commerce_fedex\Plugin\Commerce\FedEx\FedExPluginInterface $plugin */
    $plugin = $this->plugins
      ->get($plugin_id);
    $subform = [
      '#type' => 'details',
      '#title' => $definition['options_label']
        ->render(),
      '#description' => $definition['options_description']
        ->render(),
    ];
    $form[$plugin_id] = $plugin
      ->buildConfigurationForm($subform, $form_state);
    if ($form[$plugin_id] == $subform) {
      unset($form[$plugin_id]);
    }
  }
  return $form;
}