Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
#2934142: Shipping method entity does not support translations has allowed Shipping methods to be translatable.
However, Drupal\commerce_shipping\Plugin\Field\FieldFormatter\ShippingMethodFormatter
does not respect that yet.
Steps to reproduce:
- Make sure you have enabled translations for Shipping method entity
- Add a sample shipping method and provide translations in at least 2 languages
- Make a sample order and advance to
checkout/{order}/review
tab - Change the language in the UI
- The "Shipping method" field value does not get updated
Proposed resolution
Use the given langcode
parameter to load the correct shipping method translation.
Remaining tasks
User interface changes
API changes
Data model changes
Release notes snippet
Comment | File | Size | Author |
---|---|---|---|
#5 | 3115613-5-2-interdiff.txt | 6.53 KB | mbovan |
#5 | 3115613-5.patch | 6.4 KB | mbovan |
| |||
#5 | 3115613-5-TEST-ONLY.patch | 3.23 KB | mbovan |
#2 | 3115613-2.patch | 778 bytes | mbovan |
|
Comments
Comment #2
mbovan CreditAttribution: mbovan at MD Systems GmbH commentedUpdated
Drupal\commerce_shipping\Plugin\Field\FieldFormatter\ShippingMethodFormatter::viewElements()
to look for correct translation if there is one.For tests, we can extend
Drupal\Tests\commerce_shipping\Kernel\Formatter\ShippingMethodFormatterTest
.Comment #3
bojanz CreditAttribution: bojanz at Centarro commentedThanks, mbovan!
Shouldn't we use the same code as in ShipmentManager:
The getTranslationFromContext() method accepts a $langcode as a second parameter.
Of course, needs $this->entityRepository to be injected, and some manual testing at least.
Comment #4
BerdirWhat @bojanz said, was going to comment the same.
Comment #5
mbovan CreditAttribution: mbovan at MD Systems GmbH commentedAddressed #3 and added tests.
Comment #7
BerdirThis _might_ break subclasses if they have their own constructor, but commerce_shipping is still in beta and there have been tons of changes recently, so I assume @bojanz is fine with that.
And FormatterBase only implements ContainerFactoryPluginInterface in 8.8, so we'd need to require that to do the trick with assigning stuff in create() + parent call.
I prefer using \Drupal::languageManager()/service() in tests, but not enough context in the diff to see how the tests is doing these calls currently, if there are any. Not a big deal and could be changed on commit.
Comment #8
mbovan CreditAttribution: mbovan at MD Systems GmbH commented#7.1 I didn't find any contrib usages yet http://grep.xnddx.ru/search?text=extends+ShippingMethodFormatter&filenam.... I guess not many custom modules extend this formatter either.
#7.2 Yes, I've seen more calls to the injected container (34) than to the global service (8). :)
Comment #10
bojanz CreditAttribution: bojanz at Centarro commentedThank you both!
I prefer $this->container in tests so I've kept the code as-is.