abstract class USPSBase in Commerce USPS 8
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod\ShippingMethodBase implements ShippingMethodInterface, ContainerFactoryPluginInterface
- class \Drupal\commerce_usps\Plugin\Commerce\ShippingMethod\USPSBase implements SupportsTrackingInterface
- class \Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod\ShippingMethodBase implements ShippingMethodInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of USPSBase
File
- src/
Plugin/ Commerce/ ShippingMethod/ USPSBase.php, line 16
Namespace
Drupal\commerce_usps\Plugin\Commerce\ShippingMethodView source
abstract class USPSBase extends ShippingMethodBase implements SupportsTrackingInterface {
/**
* The USPSRateRequest class.
*
* @var \Drupal\commerce_usps\USPSRateRequestInterface
*/
protected $uspsRateService;
/**
* Constructs a new ShippingMethodBase object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\commerce_shipping\PackageTypeManagerInterface $package_type_manager
* The package type manager.
* @param \Drupal\state_machine\WorkflowManagerInterface $workflow_manager
* The workflow manager.
* @param \Drupal\commerce_usps\USPSRateRequestInterface $usps_rate_request
* The rate request service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, PackageTypeManagerInterface $package_type_manager, WorkflowManagerInterface $workflow_manager, USPSRateRequestInterface $usps_rate_request) {
$plugin_definition = $this
->preparePluginDefinition($plugin_definition);
parent::__construct($configuration, $plugin_id, $plugin_definition, $package_type_manager, $workflow_manager);
$this->uspsRateService = $usps_rate_request;
$this->uspsRateService
->setConfig($configuration);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.commerce_package_type'), $container
->get('plugin.manager.workflow'), $container
->get('commerce_usps.usps_rate_request'));
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'api_information' => [
'user_id' => '',
'password' => '',
'mode' => 'test',
],
'rate_options' => [
'rate_class' => 'retail',
],
'options' => [
'tracking_url' => 'https://tools.usps.com/go/TrackConfirmAction?tLabels=[tracking_code]',
'log' => [],
],
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
$this->configuration['api_information']['user_id'] = $values['api_information']['user_id'];
$this->configuration['api_information']['password'] = $values['api_information']['password'];
$this->configuration['api_information']['mode'] = $values['api_information']['mode'];
$this->configuration['rate_options']['rate_class'] = $values['rate_options']['rate_class'];
$this->configuration['options']['log'] = $values['options']['log'];
}
parent::submitConfigurationForm($form, $form_state);
}
/**
* Prepares the service array keys to support integer values.
*
* @param array $plugin_definition
* The plugin definition provided to the class.
*
* @return array
* The prepared plugin definition.
*/
private function preparePluginDefinition(array $plugin_definition) {
// Cache and unset the parsed plugin definitions for services.
$services = $plugin_definition['services'];
unset($plugin_definition['services']);
// Loop over each service definition and redefine them with
// integer keys that match the UPS API.
// TODO: Remove once core issue has been addressed.
// See: https://www.drupal.org/node/2904467 for more information.
foreach ($services as $key => $service) {
// Remove the "_" from the service key.
$key_trimmed = str_replace('_', '', $key);
$plugin_definition['services'][$key_trimmed] = $service;
}
// Sort the options alphabetically.
uasort($plugin_definition['services'], function (TranslatableMarkup $a, TranslatableMarkup $b) {
return $a
->getUntranslatedString() < $b
->getUntranslatedString() ? -1 : 1;
});
return $plugin_definition;
}
/**
* Ensure a package type exists on the shipment.
*
* @param \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment
* The commerce shipment entity.
*/
protected function setPackageType(ShipmentInterface $shipment) {
if (!$shipment
->getPackageType()) {
$shipment
->setPackageType($this
->getDefaultPackageType());
}
}
/**
* Returns a tracking URL for USPS shipments.
*
* @param \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment
* The commerce shipment.
*
* @return mixed
* The URL object or FALSE.
*/
public function getTrackingUrl(ShipmentInterface $shipment) {
$code = $shipment
->getTrackingCode();
if (!empty($code)) {
// If the tracking code token exists, replace it with the code.
if (strstr($this->configuration['options']['tracking_url'], '[tracking_code]')) {
$url = str_replace('[tracking_code]', $code, $this->configuration['options']['tracking_url']);
}
else {
// Otherwise, append the tracking code to the end of the URL.
$url = $this->configuration['options']['tracking_url'] . $code;
}
return Url::fromUri($url);
}
return FALSE;
}
/**
* Determine if we have the minimum information to connect to USPS.
*
* @return bool
* TRUE if there is enough information to connect, FALSE otherwise.
*/
protected function isConfigured() {
$api_config = $this->configuration['api_information'];
if (empty($api_config['user_id']) || empty($api_config['password'])) {
return FALSE;
}
return TRUE;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ShippingMethodBase:: |
protected | property | The package type manager. | |
ShippingMethodBase:: |
protected | property | The parent config entity. | |
ShippingMethodBase:: |
protected | property | The shipping services. | |
ShippingMethodBase:: |
protected | property | The workflow manager. | |
ShippingMethodBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
|
ShippingMethodBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ShippingMethodBase:: |
public | function |
Gets the default package type. Overrides ShippingMethodInterface:: |
|
ShippingMethodBase:: |
public | function |
Gets the shipping method label. Overrides ShippingMethodInterface:: |
|
ShippingMethodBase:: |
public | function |
Gets the shipping services. Overrides ShippingMethodInterface:: |
|
ShippingMethodBase:: |
public | function |
Gets the shipment workflow ID. Overrides ShippingMethodInterface:: |
|
ShippingMethodBase:: |
public | function |
Selects the given shipping rate for the given shipment. Overrides ShippingMethodInterface:: |
|
ShippingMethodBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ShippingMethodBase:: |
public | function |
Sets the parent entity. Overrides ParentEntityAwareInterface:: |
|
ShippingMethodBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
ShippingMethodInterface:: |
public | function | Calculates rates for the given shipment. | 3 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
USPSBase:: |
protected | property | The USPSRateRequest class. | |
USPSBase:: |
public | function |
Form constructor. Overrides ShippingMethodBase:: |
|
USPSBase:: |
public static | function |
Creates an instance of the plugin. Overrides ShippingMethodBase:: |
1 |
USPSBase:: |
public | function |
Gets default configuration for this plugin. Overrides ShippingMethodBase:: |
|
USPSBase:: |
public | function |
Returns a tracking URL for USPS shipments. Overrides SupportsTrackingInterface:: |
|
USPSBase:: |
protected | function | Determine if we have the minimum information to connect to USPS. | |
USPSBase:: |
private | function | Prepares the service array keys to support integer values. | |
USPSBase:: |
protected | function | Ensure a package type exists on the shipment. | |
USPSBase:: |
public | function |
Form submission handler. Overrides ShippingMethodBase:: |
|
USPSBase:: |
public | function |
Constructs a new ShippingMethodBase object. Overrides ShippingMethodBase:: |