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:

  1. Make sure you have enabled translations for Shipping method entity
  2. Add a sample shipping method and provide translations in at least 2 languages
  3. Make a sample order and advance to checkout/{order}/review tab
  4. Change the language in the UI
  5. 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

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mbovan created an issue. See original summary.

mbovan’s picture

Issue summary: View changes
Status: Active » Needs review
Issue tags: +Needs tests
FileSize
778 bytes

Updated 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.

bojanz’s picture

Thanks, mbovan!

Shouldn't we use the same code as in ShipmentManager:

$shipping_method = $this->entityRepository->getTranslationFromContext($shipping_method);

The getTranslationFromContext() method accepts a $langcode as a second parameter.

Of course, needs $this->entityRepository to be injected, and some manual testing at least.

Berdir’s picture

Status: Needs review » Needs work

What @bojanz said, was going to comment the same.

mbovan’s picture

Status: Needs work » Needs review
Issue tags: -Needs tests
FileSize
3.23 KB
6.4 KB
6.53 KB

Addressed #3 and added tests.

The last submitted patch, 5: 3115613-5-TEST-ONLY.patch, failed testing. View results

Berdir’s picture

Status: Needs review » Reviewed & tested by the community
  1. +++ b/src/Plugin/Field/FieldFormatter/ShippingMethodFormatter.php
    @@ -19,7 +22,55 @@ use Drupal\Core\Field\FormatterBase;
      * )
      */
    -class ShippingMethodFormatter extends FormatterBase {
    +class ShippingMethodFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
    +
    +  /**
    +   * The entity repository.
    +   *
    +   * @var \Drupal\Core\Entity\EntityRepositoryInterface
    +   */
    +  protected $entityRepository;
    +
    

    This _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.

  2. +++ b/tests/src/Kernel/Formatter/ShippingMethodFormatterTest.php
    @@ -16,6 +17,32 @@ use Drupal\Tests\commerce_shipping\Kernel\ShippingKernelTestBase;
    +    $this->container->get('content_translation.manager')->setEnabled('commerce_shipping_method', 'commerce_shipping_method', TRUE);
    
    @@ -53,6 +93,20 @@ class ShippingMethodFormatterTest extends ShippingKernelTestBase {
     
    +    $default_language = $this->container->get('language_manager')->getDefaultLanguage();
    +    $this->container->get('language.default')->set($this->translationLanguage);
    

    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.

mbovan’s picture

#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). :)

  • bojanz committed ab91a59 on 8.x-2.x authored by mbovan
    Issue #3115613 by mbovan, Berdir: Shipping method formatter does not...
bojanz’s picture

Status: Reviewed & tested by the community » Fixed

Thank you both!

I prefer $this->container in tests so I've kept the code as-is.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.