Commit 9e7e79ba authored by bojanz's avatar bojanz

Issue #2934142 by bojanz, StryKaizer, trebormc: Shipping method entity does...

Issue #2934142 by bojanz, StryKaizer, trebormc: Shipping method entity does not support translations
parent 2bc72c04
......@@ -63,7 +63,7 @@ services:
commerce_shipping.shipment_manager:
class: Drupal\commerce_shipping\ShipmentManager
arguments: ['@entity_type.manager', '@event_dispatcher', '@logger.channel.commerce_shipping']
arguments: ['@entity_type.manager', '@entity.repository', '@event_dispatcher', '@logger.channel.commerce_shipping']
logger.channel.commerce_shipping:
parent: logger.channel_base
......
......@@ -36,13 +36,18 @@ use Drupal\Core\Field\BaseFieldDefinition;
* "route_provider" = {
* "default" = "Drupal\entity\Routing\AdminHtmlRouteProvider",
* },
* "translation" = "Drupal\content_translation\ContentTranslationHandler",
* "translation" = "Drupal\commerce_shipping\ShippingMethodTranslationHandler",
* "views_data" = "Drupal\commerce\CommerceEntityViewsData",
* },
* base_table = "commerce_shipping_method",
* data_table = "commerce_shipping_method_field_data",
* admin_permission = "administer commerce_shipping_method",
* translatable = TRUE,
* translation = {
* "content_translation" = {
* "access_callback" = "content_translation_translate_access"
* },
* },
* entity_keys = {
* "id" = "shipping_method_id",
* "label" = "name",
......@@ -54,12 +59,26 @@ use Drupal\Core\Field\BaseFieldDefinition;
* "add-form" = "/admin/commerce/shipping-methods/add",
* "edit-form" = "/admin/commerce/shipping-methods/manage/{commerce_shipping_method}",
* "delete-form" = "/admin/commerce/shipping-methods/manage/{commerce_shipping_method}/delete",
* "collection" = "/admin/commerce/shipping-methods"
* "collection" = "/admin/commerce/shipping-methods",
* "drupal:content-translation-overview" = "/admin/commerce/shipping-methods/manage/{commerce_shipping_method}/translations",
* "drupal:content-translation-add" = "/admin/commerce/shipping-methods/manage/{commerce_shipping_method}/translations/add/{source}/{target}",
* "drupal:content-translation-edit" = "/admin/commerce/shipping-methods/manage/{commerce_shipping_method}/translations/edit/{language}",
* "drupal:content-translation-delete" = "/admin/commerce/shipping-methods/manage/{commerce_shipping_method}/translations/delete/{language}",
* }
* )
*/
class ShippingMethod extends ContentEntityBase implements ShippingMethodInterface {
/**
* {@inheritdoc}
*/
public function toUrl($rel = 'canonical', array $options = []) {
if ($rel == 'canonical') {
$rel = 'edit-form';
}
return parent::toUrl($rel, $options);
}
/**
* {@inheritdoc}
*/
......@@ -266,6 +285,7 @@ class ShippingMethod extends ContentEntityBase implements ShippingMethodInterfac
->setLabel(t('Plugin'))
->setCardinality(1)
->setRequired(TRUE)
->setTranslatable(TRUE)
->setDisplayOptions('form', [
'type' => 'commerce_plugin_radios',
'weight' => 1,
......
......@@ -5,6 +5,7 @@ namespace Drupal\commerce_shipping;
use Drupal\commerce_shipping\Entity\ShipmentInterface;
use Drupal\commerce_shipping\Event\ShippingEvents;
use Drupal\commerce_shipping\Event\ShippingRatesEvent;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
......@@ -18,6 +19,13 @@ class ShipmentManager implements ShipmentManagerInterface {
*/
protected $entityTypeManager;
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* The event dispatcher.
*
......@@ -37,13 +45,16 @@ class ShipmentManager implements ShipmentManagerInterface {
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity repository.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Psr\Log\LoggerInterface $logger
* The logger.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, LoggerInterface $logger) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, EventDispatcherInterface $event_dispatcher, LoggerInterface $logger) {
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
$this->eventDispatcher = $event_dispatcher;
$this->logger = $logger;
}
......@@ -57,6 +68,8 @@ class ShipmentManager implements ShipmentManagerInterface {
$shipping_method_storage = $this->entityTypeManager->getStorage('commerce_shipping_method');
$shipping_methods = $shipping_method_storage->loadMultipleForShipment($shipment);
foreach ($shipping_methods as $shipping_method) {
/** @var \Drupal\commerce_shipping\Entity\ShippingMethodInterface $shipping_method */
$shipping_method = $this->entityRepository->getTranslationFromContext($shipping_method);
$shipping_method_plugin = $shipping_method->getPlugin();
try {
$rates = $shipping_method_plugin->calculateRates($shipment);
......
......@@ -10,6 +10,7 @@ use Drupal\commerce_promotion\Entity\Promotion;
use Drupal\commerce_shipping\Entity\Shipment;
use Drupal\commerce_shipping\Entity\ShippingMethod;
use Drupal\commerce_shipping\ShipmentItem;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\physical\Weight;
/**
......@@ -53,6 +54,8 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
*/
public static $modules = [
'commerce_promotion',
'language',
'content_translation',
];
/**
......@@ -64,6 +67,9 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
$this->installEntitySchema('commerce_promotion');
$this->installEntitySchema('commerce_promotion_coupon');
ConfigurableLanguage::createFromLangcode('fr')->save();
$this->container->get('content_translation.manager')->setEnabled('commerce_shipping_method', 'commerce_shipping_method', TRUE);
$this->shipmentManager = $this->container->get('commerce_shipping.shipment_manager');
$order_item = OrderItem::create([
'type' => 'test',
......@@ -88,7 +94,7 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
'plugin' => [
'target_plugin_id' => 'flat_rate',
'target_plugin_configuration' => [
'rate_label' => 'Flat rate',
'rate_label' => 'Standard shipping',
'rate_amount' => [
'number' => '5',
'currency_code' => 'USD',
......@@ -107,7 +113,7 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
'plugin' => [
'target_plugin_id' => 'flat_rate',
'target_plugin_configuration' => [
'rate_label' => 'Flat rate',
'rate_label' => 'Overnight shipping',
'rate_amount' => [
'number' => '20',
'currency_code' => 'USD',
......@@ -117,6 +123,19 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
'status' => TRUE,
'weight' => 0,
]);
$another_shipping_method->addTranslation('fr', [
'name' => 'Another shipping method (FR)',
'plugin' => [
'target_plugin_id' => 'flat_rate',
'target_plugin_configuration' => [
'rate_label' => 'Le overnight shipping',
'rate_amount' => [
'number' => '22',
'currency_code' => 'USD',
],
],
],
]);
$another_shipping_method->save();
$this->shippingMethods[] = $another_shipping_method;
......@@ -158,6 +177,9 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
* @covers ::calculateRates
*/
public function testCalculateRates() {
// Use the FR translation where available (e.g. $another_shipping_method).
$this->container->get('language.default')->set(ConfigurableLanguage::createFromLangcode('fr'));
$rates = $this->shipmentManager->calculateRates($this->shipment);
$this->assertCount(2, $rates);
$first_rate = reset($rates);
......@@ -166,12 +188,14 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
$this->assertArrayHasKey($first_rate->getId(), $rates);
$this->assertEquals('2', $first_rate->getShippingMethodId());
$this->assertEquals('default', $first_rate->getService()->getId());
$this->assertEquals(new Price('20.00', 'USD'), $first_rate->getOriginalAmount());
$this->assertEquals(new Price('20.00', 'USD'), $first_rate->getAmount());
$this->assertEquals('Le overnight shipping', $first_rate->getService()->getLabel());
$this->assertEquals(new Price('22.00', 'USD'), $first_rate->getOriginalAmount());
$this->assertEquals(new Price('22.00', 'USD'), $first_rate->getAmount());
$this->assertArrayHasKey($second_rate->getId(), $rates);
$this->assertEquals('1', $second_rate->getShippingMethodId());
$this->assertEquals('default', $second_rate->getService()->getId());
$this->assertEquals('Standard shipping', $second_rate->getService()->getLabel());
$this->assertEquals(new Price('5.00', 'USD'), $second_rate->getOriginalAmount());
$this->assertEquals(new Price('5.00', 'USD'), $second_rate->getAmount());
......@@ -185,12 +209,14 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
$this->assertArrayHasKey($first_rate->getId(), $rates);
$this->assertEquals('2', $first_rate->getShippingMethodId());
$this->assertEquals('default', $first_rate->getService()->getId());
$this->assertEquals(new Price('20.00', 'USD'), $first_rate->getOriginalAmount());
$this->assertEquals(new Price('40.00', 'USD'), $first_rate->getAmount());
$this->assertEquals('Le overnight shipping', $first_rate->getService()->getLabel());
$this->assertEquals(new Price('22.00', 'USD'), $first_rate->getOriginalAmount());
$this->assertEquals(new Price('44.00', 'USD'), $first_rate->getAmount());
$this->assertArrayHasKey($second_rate->getId(), $rates);
$this->assertEquals('1', $second_rate->getShippingMethodId());
$this->assertEquals('default', $second_rate->getService()->getId());
$this->assertEquals('Standard shipping', $second_rate->getService()->getLabel());
$this->assertEquals(new Price('5.00', 'USD'), $second_rate->getOriginalAmount());
$this->assertEquals(new Price('10.00', 'USD'), $second_rate->getAmount());
}
......@@ -262,6 +288,7 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
$this->assertArrayHasKey($first_rate->getId(), $rates);
$this->assertEquals('2', $first_rate->getShippingMethodId());
$this->assertEquals('default', $first_rate->getService()->getId());
$this->assertEquals('Overnight shipping', $first_rate->getService()->getLabel());
$this->assertEquals(new Price('20.00', 'USD'), $first_rate->getOriginalAmount());
$this->assertEquals(new Price('10.00', 'USD'), $first_rate->getAmount());
......@@ -269,6 +296,7 @@ class ShipmentManagerTest extends ShippingKernelTestBase {
$this->assertArrayHasKey($second_rate->getId(), $rates);
$this->assertEquals('1', $second_rate->getShippingMethodId());
$this->assertEquals('default', $second_rate->getService()->getId());
$this->assertEquals('Standard shipping', $second_rate->getService()->getLabel());
$this->assertEquals(new Price('5.00', 'USD'), $second_rate->getOriginalAmount());
$this->assertEquals(new Price('4.00', 'USD'), $second_rate->getAmount());
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment