2

In my store I defined list of supported billing and shipping countries at store settings. This list is respected during checkout process - limited list of countries is shown when customer is entering the address. But when user is on "Address book" tab of user profile page and wants to add new address list is not limited, but all countries are allowed there.

How can I limit list of displayed countries when editing address at address book user profile tab?

I tried using hook_form_alter() but I don't see country drop-down there? Also tried hook_preprocess_input() and I see other fields, but not country drop-down.

theme_preprocess_field() didn't help either.

And of course, I didn't find that option in back-end.

2
  • That's really a bug, address book entry should respect those too. There is an issue about this.
    – No Sssweat
    Oct 24 '20 at 2:38
  • I fount that patch my self and tried it, but for some reason it didn't work for me.
    – MilanG
    Oct 26 '20 at 8:30
2

Found solution here: https://docs.drupalcommerce.org/commerce2/developer-guide/customers/addresses/countries-and-subdivisions

Created new module "country_limit" added event subsriber like explained there:

<?php

namespace Drupal\country_limit;

use Drupal\address\Event\AddressEvents;
use Drupal\address\Event\AvailableCountriesEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LimitCountriesEventSubscriber implements EventSubscriberInterface {

  public static function getSubscribedEvents() {
    $events[AddressEvents::AVAILABLE_COUNTRIES][] = ['onAvailableCountries'];
    return $events;
  }

  public function onAvailableCountries(AvailableCountriesEvent $event) {
    $countries = ['AT' => 'AT', 'DE' => 'DE', 'CH' => 'CH'];
    $event->setAvailableCountries($countries);
  }

}

and registered it like:

services:
  # Name of this service.
  my_config_events_subscriber:
    # Event subscriber class that will listen for the events.
    class: '\Drupal\country_limit\LimitCountriesEventSubscriber'
    # Tagged as an event_subscriber to register this subscriber with the event_dispatch service.
    tags:
      - { name: 'event_subscriber' }

Event subsriber class is directly inside "src" dir, since I don't have other functionalities in this module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.