Score:-1

How to alter list of available countries provided commerce/guys

om flag

I need to alter the list of countries that are displayed in a webform. That uses a widget that is powered by the Address module which is powered by the commerceguys/addressing library.

According to the docs for Drupal/Commerce, that can be done with an event subscriber.

You can alter the list of available countries for a select list by subscribing to the AddressEvents::AVAILABLE_COUNTRIES

In fact, in the Address module, there is example code. This shouldn't be so hard, right?

Question is; when does the event fire? I'm on a page that has generated dropdown menu with a list of countries clearly produced by the Address module. But, the event never fires (see output below from the webprofiler)

enter image description here

I've pretty much copied the code in the module used as a test. But no luck getting the event to happen. So;

  1. Anyone have any experience and hopefully success customizing the list of available countries
  2. Any suggestions on determining when an event should fire

Side note to #2. Looking at the trait, I can see where it "should fire" but what page / endpoint would trigger that?

cn flag
Please follow the general guideline of one question per post. In general, if a module includes example code, then you can expect it to work; if it's not working, you should show your code when posting here, and describe what you changed from the sample. My initial guess would be that you named the file wrong, so it's not being picked up, but without posting code, that's impossible to say.
cn flag
No offense intended. I made my comment based on the fact that you ended your post with a numbered list of points that could invite an open discussion-- for example, asking "How can I determine when an event should fire?" is a different question than "How to alter available countries on webform?"
Score:0
om flag

After reviewing this with module maintainers, the answer to the question is that it can't be resolved in the approach I was taking

Webform is special, it uses the address element directly so the event (which is field type specific) is not fired. In that case you will need to override the country repository

Score:0
om flag

Here is the answer. Service Decorators

What needs to happen is to augment/modify the behavior of one method in the address.country_repository service. Specifically \Drupal::service('address.country_repository')->getList();

Service Decorators from Symfony does exactly that- allows the base class to persist, but provides the ability to add to or modify the behavior of methods within the class

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.