Subdivision event subscriber doesn't receive a list of subdivisions already set for a country

Created on 4 February 2021, almost 4 years ago
Updated 7 January 2024, 11 months ago

Problem/Motivation

Two union territories in India recently merged to become a single one called Dadra and Nagar Haveli and Daman and Diu. My team wants to use a subdivision event subscriber to remove the old union territories and add the new one. However, we don't want to (nor do we think it is good practice) to add in the subscriber a full list of all India subdivisions, as that list is already provided by the address module. Ideally, the event subscriber would receive the existing subdivision definitions for a country, and the user would be able to modify that list if they want.

Proposed resolution

Currently, the event subscriber receives a SubdivisionsEvent object that contains a parent (the country code) and an empty list of definitions (which are used to define the subdivisions). This SubdivisionsEvent object should have the list of definitions already populated with the values set by the address module. That way if the user wants to add and modify only a few subdivisions, they can simply modify the existing list, rather than redefine the full subdivision list.

I'm providing a patch, with the proposed solution.

API changes

The user will be able to call '$event.getDefinitions()' inside a SubdivisionsEventSubscriber to get the list of subdivisions defined by the
address module. See example below:

<?php
/**
  /**
 * Adds the subdivisions that are missing on commerceguys/addressing.
 */
class SubdivisionsEventSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AddressEvents::ADDRESS_FORMAT][] = ['onAddressFormat'];
    $events[AddressEvents::SUBDIVISIONS][] = ['onSubdivisions'];
    return $events;
  }

  /**
   * Alters the address format.
   *
   * @param \Drupal\address\Event\AddressFormatEvent $event
   *   The address format event.
   */
  public function onAddressFormat(AddressFormatEvent $event) {
    $definition = $event->getDefinition();
    }
  }

  /**
   * Provides the subdivisions.
   *
   * @param \Drupal\address\Event\SubdivisionsEvent $event
   *   The subdivisions event.
   */
  public function onSubdivisions(SubdivisionsEvent $event) {
    $parents = $event->getParents();
    $current_definitions = $event->getDefinitions(); #provides the subdivision list included in the address module 
 }
}
?>
Feature request
Status

Fixed

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024