View source
<?php
namespace Drupal\commerce_shipping\Plugin\Commerce\ShippingMethod;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\PackageTypeManagerInterface;
use Drupal\commerce_shipping\ShippingRate;
use Drupal\commerce_shipping\ShippingService;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\state_machine\WorkflowManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ShippingMethodBase extends PluginBase implements ContainerFactoryPluginInterface, ShippingMethodInterface {
protected $packageTypeManager;
protected $workflowManager;
protected $services = [];
protected $parentEntity;
public function __construct(array $configuration, $plugin_id, $plugin_definition, PackageTypeManagerInterface $package_type_manager, WorkflowManagerInterface $workflow_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->packageTypeManager = $package_type_manager;
$this->workflowManager = $workflow_manager;
foreach ($this->pluginDefinition['services'] as $id => $label) {
$this->services[$id] = new ShippingService($id, (string) $label);
}
$this
->setConfiguration($configuration);
}
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'));
}
public function setParentEntity(EntityInterface $parent_entity) {
$this->parentEntity = $parent_entity;
}
public function getLabel() {
return (string) $this->pluginDefinition['label'];
}
public function getDefaultPackageType() {
$package_type_id = $this->configuration['default_package_type'];
return $this->packageTypeManager
->createInstance($package_type_id);
}
public function getServices() {
return array_intersect_key($this->services, array_flip($this->configuration['services']));
}
public function getWorkflowId() {
return $this->configuration['workflow'];
}
public function calculateDependencies() {
return [];
}
public function defaultConfiguration() {
return [
'default_package_type' => 'custom_box',
'services' => [],
'workflow' => $this->pluginDefinition['workflow'],
];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$package_types = $this->packageTypeManager
->getDefinitionsByShippingMethod($this->pluginId);
$package_types = array_map(function ($package_type) {
return $package_type['label'];
}, $package_types);
$services = array_map(function ($service) {
return $service
->getLabel();
}, $this->services);
if (empty($this->configuration['services'])) {
$service_ids = array_keys($services);
$this->configuration['services'] = array_combine($service_ids, $service_ids);
}
$workflows = $this->workflowManager
->getGroupedLabels('commerce_shipment');
$workflows = reset($workflows);
$form['default_package_type'] = [
'#type' => 'select',
'#title' => $this
->t('Default package type'),
'#options' => $package_types,
'#default_value' => $this->configuration['default_package_type'],
'#required' => TRUE,
'#access' => count($package_types) > 1,
];
$form['services'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Shipping services'),
'#options' => $services,
'#default_value' => $this->configuration['services'],
'#required' => TRUE,
'#access' => count($services) > 1,
];
$form['workflow'] = [
'#type' => 'select',
'#title' => $this
->t('Shipment workflow'),
'#options' => $workflows,
'#default_value' => $this->configuration['workflow'],
'#required' => TRUE,
'#access' => count($workflows) > 1,
];
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
$values = $form_state
->getValue($form['#parents']);
$workflow = $this->workflowManager
->createInstance($values['workflow']);
if (!$workflow
->getTransition('finalize')) {
$form_state
->setError($form['workflow'], $this
->t('The @workflow workflow does not have a "Finalize" transition.', [
'@workflow' => $workflow
->getLabel(),
]));
}
if (!$workflow
->getTransition('cancel')) {
$form_state
->setError($form['workflow'], $this
->t('The @workflow workflow does not have a "Cancel" transition.', [
'@workflow' => $workflow
->getLabel(),
]));
}
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
$values = $form_state
->getValue($form['#parents']);
if (!empty($values['services'])) {
$values['services'] = array_filter($values['services']);
$this->configuration['default_package_type'] = $values['default_package_type'];
$this->configuration['services'] = array_keys($values['services']);
}
$this->configuration['workflow'] = $values['workflow'];
}
}
public function selectRate(ShipmentInterface $shipment, ShippingRate $rate) {
$shipment
->setShippingMethodId($rate
->getShippingMethodId());
$shipment
->setShippingService($rate
->getService()
->getId());
$shipment
->setOriginalAmount($rate
->getOriginalAmount());
$shipment
->setAmount($rate
->getAmount());
}
}