You are here

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

Expanded class hierarchy of DangerousGoodsPlugin

File

modules/dangerous/src/Plugin/Commerce/FedEx/DangerousGoodsPlugin.php, line 24

Namespace

Drupal\commerce_fedex_dangerous\Plugin\Commerce\FedEx
View 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

Namesort descending Modifiers Type Description Overrides
DangerousGoodsPlugin::adjustPackage public function Adjust a package based on the items, shipment and profile. Overrides FedExPluginBase::adjustPackage
DangerousGoodsPlugin::buildConfigurationForm public function Form constructor. Overrides FedExPluginBase::buildConfigurationForm
DangerousGoodsPlugin::defaultConfiguration public function Provide the plugin default configuration. Overrides FedExPluginBase::defaultConfiguration
DangerousGoodsPlugin::getDangerousStatus protected function Returns the DG status of an item.
DangerousGoodsPlugin::NOT_DANGEROUS constant
DangerousGoodsPlugin::splitPackage public function Function splitPackage. Overrides FedExPluginBase::splitPackage
DangerousGoodsPlugin::submitConfigurationForm public function Form submission handler. Overrides FedExPluginBase::submitConfigurationForm
FedExPluginBase::calculateDependencies public function
FedExPluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create 1
FedExPluginBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
FedExPluginBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
FedExPluginBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
FedExPluginBase::__construct public function Creates a new FedEx Service object. Overrides PluginBase::__construct 1
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
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.