class DangerousGoodsPlugin in Commerce FedEx 8
Providex the FedEx Dangerous Goods Service Plugin.
Plugin annotation
@CommerceFedExPlugin(
id = "dangerous",
label = @Translation("FedEx Dangerous Goods"),
options_label = @Translation("Dangerous Goods Shipment Options"),
options_description = @Translation("Enter your global shipping options for Dangerous Goods shipments")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\commerce_fedex\Plugin\Commerce\FedEx\FedExPluginBase implements FedExPluginInterface, ContainerFactoryPluginInterface uses StringTranslationTrait
- class \Drupal\commerce_fedex_dangerous\Plugin\Commerce\FedEx\DangerousGoodsPlugin
- class \Drupal\commerce_fedex\Plugin\Commerce\FedEx\FedExPluginBase implements FedExPluginInterface, ContainerFactoryPluginInterface uses StringTranslationTrait
Expanded class hierarchy of DangerousGoodsPlugin
File
- modules/
dangerous/ src/ Plugin/ Commerce/ FedEx/ DangerousGoodsPlugin.php, line 24
Namespace
Drupal\commerce_fedex_dangerous\Plugin\Commerce\FedExView source
class DangerousGoodsPlugin extends FedExPluginBase {
const NOT_DANGEROUS = 0;
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'contact_number' => '',
] + parent::defaultConfiguration();
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['contact_number'] = [
'#type' => 'textfield',
'#title' => $this
->t("Contact Number"),
'#description' => $this
->t('Enter the Phone number of your dangerous goods/hazardous materials contact person'),
'#default_value' => $this->configuration['contact_number'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
parent::submitConfigurationForm($form, $form_state);
$this->configuration['contact_number'] = $form_state
->getValue('contact_number');
}
/**
* {@inheritdoc}
*/
public function adjustPackage(RequestedPackageLineItem $package, array $shipment_items, ShipmentInterface $shipment) {
$status = $this
->getDangerousStatus(reset($shipment_items));
if ($status === static::NOT_DANGEROUS) {
return $package;
}
$special_services_requested = $package
->getSpecialServicesRequested();
if (empty($special_services_requested)) {
$special_services_requested = new PackageSpecialServicesRequested();
}
$special_services_requested
->addToSpecialServiceTypes(PackageSpecialServiceType::VALUE_DANGEROUS_GOODS);
$dangerous_goods_detail = $special_services_requested
->getDangerousGoodsDetail();
if (empty($dangerous_goods_detail)) {
$dangerous_goods_detail = new DangerousGoodsDetail();
}
$dangerous_goods_detail
->setAccessibility($status);
$special_services_requested
->setDangerousGoodsDetail($dangerous_goods_detail);
$package
->setSpecialServicesRequested($special_services_requested);
return $package;
}
/**
* {@inheritdoc}
*/
public function splitPackage(array $shipment_items, ShipmentInterface $shipment) {
$packages = [];
foreach ($shipment_items as $shipment_item) {
$packages[$this
->getDangerousStatus($shipment_item)][] = $shipment_item;
}
return array_values($packages);
}
/**
* Returns the DG status of an item.
*
* @param \Drupal\commerce_shipping\ShipmentItem $shipment_item
* The item to check.
*
* @return mixed
* The DG status.
*/
protected function getDangerousStatus(ShipmentItem $shipment_item) {
$storage = \Drupal::entityTypeManager()
->getStorage('commerce_order_item');
/** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
$order_item = $storage
->load($shipment_item
->getOrderItemId());
$purchased_entity = $order_item
->getPurchasedEntity();
if (!$purchased_entity
->hasField('fedex_dangerous_accessibility') || $purchased_entity
->get('fedex_dangerous_accessibility')
->isEmpty()) {
return static::NOT_DANGEROUS;
}
return $purchased_entity
->get('fedex_dangerous_accessibility')->value;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DangerousGoodsPlugin:: |
public | function |
Adjust a package based on the items, shipment and profile. Overrides FedExPluginBase:: |
|
DangerousGoodsPlugin:: |
public | function |
Form constructor. Overrides FedExPluginBase:: |
|
DangerousGoodsPlugin:: |
public | function |
Provide the plugin default configuration. Overrides FedExPluginBase:: |
|
DangerousGoodsPlugin:: |
protected | function | Returns the DG status of an item. | |
DangerousGoodsPlugin:: |
constant | |||
DangerousGoodsPlugin:: |
public | function |
Function splitPackage. Overrides FedExPluginBase:: |
|
DangerousGoodsPlugin:: |
public | function |
Form submission handler. Overrides FedExPluginBase:: |
|
FedExPluginBase:: |
public | function | ||
FedExPluginBase:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
1 |
FedExPluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
FedExPluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
FedExPluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
FedExPluginBase:: |
public | function |
Creates a new FedEx Service object. Overrides PluginBase:: |
1 |
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. | |
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. |