You are here

class USPSSettingsForm in Ubercart 8.4

Configures USPS settings.

Hierarchy

Expanded class hierarchy of USPSSettingsForm

1 string reference to 'USPSSettingsForm'
uc_usps.routing.yml in shipping/uc_usps/uc_usps.routing.yml
shipping/uc_usps/uc_usps.routing.yml

File

shipping/uc_usps/src/Form/USPSSettingsForm.php, line 13

Namespace

Drupal\uc_usps\Form
View source
class USPSSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'uc_usps_admin_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'uc_usps.settings',
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $usps_config = $this
      ->config('uc_usps.settings');

    // Put fieldsets into vertical tabs.
    $form['usps-settings'] = [
      '#type' => 'vertical_tabs',
      '#attached' => [
        'library' => [
          'uc_usps/uc_usps.scripts',
        ],
      ],
    ];

    // Container for credential forms.
    $form['credentials'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Credentials'),
      '#description' => $this
        ->t('Account number and authorization information.'),
      '#group' => 'usps-settings',
    ];
    $form['credentials']['user_id'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('USPS user ID'),
      '#description' => $this
        ->t('To acquire or locate your user ID, refer to the <a href=":url">USPS documentation</a>.', [
        ':url' => Url::fromUri('https://www.drupal.org/node/1308256')
          ->toString(),
      ]),
      '#default_value' => $usps_config
        ->get('user_id'),
    ];
    $form['domestic'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('USPS Domestic'),
      '#description' => $this
        ->t('Set the conditions that will return a USPS quote.'),
      '#group' => 'usps-settings',
    ];
    $form['domestic']['online_rates'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display USPS "online" rates'),
      '#default_value' => $usps_config
        ->get('online_rates'),
      '#description' => $this
        ->t('Show your customer standard USPS rates (default) or discounted "online" rates.  Online rates apply only if you, the merchant, pay for and print out postage from the USPS <a href="https://cns.usps.com/labelInformation.shtml">Click-N-Ship</a> web site.'),
    ];
    $form['domestic']['env_services'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('USPS envelope services'),
      '#default_value' => $usps_config
        ->get('env_services'),
      '#options' => USPSUtilities::envelopeServices(),
      '#description' => $this
        ->t('Select the USPS services that are available to customers. Be sure to include the services that the Postal Service agrees are available to you.'),
    ];
    $form['domestic']['services'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('USPS parcel services'),
      '#default_value' => $usps_config
        ->get('services'),
      '#options' => USPSUtilities::services(),
      '#description' => $this
        ->t('Select the USPS services that are available to customers. Be sure to include the services that the Postal Service agrees are available to you.'),
    ];
    $form['international'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('USPS International'),
      '#description' => $this
        ->t('Set the conditions that will return a USPS International quote.'),
      '#group' => 'usps-settings',
    ];
    $form['international']['intl_env_services'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('USPS international envelope services'),
      '#default_value' => $usps_config
        ->get('intl_env_services'),
      '#options' => USPSUtilities::internationalEnvelopeServices(),
      '#description' => $this
        ->t('Select the USPS services that are available to customers. Be sure to include the services that the Postal Service agrees are available to you.'),
    ];
    $form['international']['intl_services'] = [
      '#type' => 'checkboxes',
      '#title' => $this
        ->t('USPS international parcel services'),
      '#default_value' => $usps_config
        ->get('intl_services'),
      '#options' => USPSUtilities::internationalServices(),
      '#description' => $this
        ->t('Select the USPS services that are available to customers. Be sure to include the services that the Postal Service agrees are available to you.'),
    ];

    // Container for quote options.
    $form['quote_options'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Quote options'),
      '#description' => $this
        ->t('Preferences that affect computation of quote.'),
      '#group' => 'usps-settings',
    ];
    $form['quote_options']['all_in_one'] = [
      '#type' => 'radios',
      '#title' => $this
        ->t('Product packages'),
      '#default_value' => $usps_config
        ->get('all_in_one'),
      '#options' => [
        0 => $this
          ->t('Each product in its own package'),
        1 => $this
          ->t('All products in one package'),
      ],
      '#description' => $this
        ->t('Indicate whether each product is quoted as shipping separately or all in one package. Orders with one kind of product will still use the package quantity to determine the number of packages needed, however.'),
    ];

    // Insurance.
    $form['quote_options']['insurance'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Package insurance'),
      '#default_value' => $usps_config
        ->get('insurance'),
      '#description' => $this
        ->t('When enabled, the quotes presented to the customer will include the cost of insurance for the full sales price of all products in the order.'),
      '#disabled' => TRUE,
    ];

    // Delivery Confirmation.
    $form['quote_options']['delivery_confirmation'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Delivery confirmation'),
      '#default_value' => $usps_config
        ->get('delivery_confirmation'),
      '#description' => $this
        ->t('When enabled, the quotes presented to the customer will include the cost of delivery confirmation for all packages in the order.'),
      '#disabled' => TRUE,
    ];

    // Signature Confirmation.
    $form['quote_options']['signature_confirmation'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Signature confirmation'),
      '#default_value' => $usps_config
        ->get('signature_confirmation'),
      '#description' => $this
        ->t('When enabled, the quotes presented to the customer will include the cost of signature confirmation for all packages in the order.'),
      '#disabled' => TRUE,
    ];

    // Container for markup forms.
    $form['markups'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Markups'),
      '#description' => $this
        ->t('Modifiers to the shipping weight and quoted rate.'),
      '#group' => 'usps-settings',
    ];
    $form['markups']['rate_markup_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Rate markup type'),
      '#default_value' => $usps_config
        ->get('rate_markup_type'),
      '#options' => [
        'percentage' => $this
          ->t('Percentage (%)'),
        'multiplier' => $this
          ->t('Multiplier (×)'),
        'currency' => $this
          ->t('Addition (@currency)', [
          '@currency' => $this
            ->config('uc_store.settings')
            ->get('currency.symbol'),
        ]),
      ],
    ];
    $form['markups']['rate_markup'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Shipping rate markup'),
      '#default_value' => $usps_config
        ->get('rate_markup'),
      '#description' => $this
        ->t('Markup shipping rate quote by dollar amount, percentage, or multiplier.'),
    ];

    // Form to select type of weight markup.
    $form['markups']['weight_markup_type'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Weight markup type'),
      '#default_value' => $usps_config
        ->get('weight_markup_type'),
      '#options' => [
        'percentage' => $this
          ->t('Percentage (%)'),
        'multiplier' => $this
          ->t('Multiplier (×)'),
        'mass' => $this
          ->t('Addition (@mass)', [
          '@mass' => '#',
        ]),
      ],
      '#disabled' => TRUE,
    ];

    // Form to select weight markup amount.
    $form['markups']['weight_markup'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Shipping weight markup'),
      //'#default_value' => $usps_config->get('weight_markup'),
      '#default_value' => 0,
      '#description' => $this
        ->t('Markup shipping weight on a per-package basis before quote, by weight amount, percentage, or multiplier.'),
      '#disabled' => TRUE,
    ];

    // Taken from system_settings_form(). Only, don't use its submit handler.
    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save configuration'),
    ];
    $form['actions']['cancel'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Cancel'),
      '#url' => Url::fromRoute('entity.uc_quote_method.collection'),
    ];
    if (!empty($_POST) && $form_state
      ->getErrors()) {
      $this
        ->messenger()
        ->addError($this
        ->t('The settings have not been saved because of the errors.'));
    }
    if (!isset($form['#theme'])) {
      $form['#theme'] = 'system_settings_form';
    }
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (!is_numeric($form_state
      ->getValue('rate_markup'))) {
      $form_state
        ->setErrorByName('rate_markup', $this
        ->t('Rate markup must be a numeric value.'));
    }
    if (!is_numeric($form_state
      ->getValue('weight_markup'))) {
      $form_state
        ->setErrorByName('weight_markup', $this
        ->t('Weight markup must be a numeric value.'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $usps_config = $this
      ->config('uc_usps.settings');
    $values = $form_state
      ->getValues();
    $usps_config
      ->set('user_id', $values['user_id'])
      ->set('online_rates', $values['online_rates'])
      ->set('env_services', $values['env_services'])
      ->set('services', $values['services'])
      ->set('intl_env_services', $values['intl_env_services'])
      ->set('intl_services', $values['intl_services'])
      ->set('rate_markup_type', $values['rate_markup_type'])
      ->set('rate_markup', $values['rate_markup'])
      ->set('weight_markup_type', $values['weight_markup_type'])
      ->set('weight_markup', $values['weight_markup'])
      ->set('all_in_one', $values['all_in_one'])
      ->set('insurance', $values['insurance'])
      ->set('delivery_confirmation', $values['delivery_confirmation'])
      ->set('signature_confirmation', $values['signature_confirmation'])
      ->save();
    $this
      ->messenger()
      ->addMessage($this
      ->t('The configuration options have been saved.'));

    // @todo Still need these two lines?
    // cache_clear_all();
    // drupal_theme_rebuild();
    parent::submitForm($form, $form_state);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBase::create public static function Instantiates a new instance of this class. Overrides FormBase::create 13
ConfigFormBase::__construct public function Constructs a \Drupal\system\ConfigFormBase object. 11
ConfigFormBaseTrait::config protected function Retrieves a configuration 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
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.
USPSSettingsForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
USPSSettingsForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
USPSSettingsForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
USPSSettingsForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
USPSSettingsForm::validateForm public function Form validation handler. Overrides FormBase::validateForm