I just released commerceguys/addressing v2.0.0-beta1 and it has a number of improvements relevant to this module. I suggest branching 2.0.x of the module and then switching to it.
Steps to to update the module:
1) Drop PHP7 compatibility, since the library itself requires PHP8.
2) Switch composer.json and ludwig.json to commerceguys/addressing v2.0.0.
3) Update all classes extending the library ones to ensure typehint compatibility.
4) Add a getAddressLine3() to the address field type, and just return null for now.
5) Implement an AddressFormatEvent and strip "%addressLine3\n" from each format string.
6) Update GreatBritainEventSubscriber. We can keep the same subdivision keys (ISO codes are not required) but we need to remove the iso_code example cause there's no such key anymore.
7) Add postLoad() logic to the field type to use the SubdivisionUpdater (in order to update previous data).
Possible followups:
- Do #2482969: Add address line 3 for real.
- Set the label of the additionalName field to "Patronymic" when one of the patronymic countries is selected.
| Comment | File | Size | Author |
|---|---|---|---|
| #45 | 3354976-44-addressing-v2.interdiff.patch | 4.54 KB | bojanz |
| #45 | 3354976-44-addressing-v2.patch | 21.16 KB | bojanz |
| #43 | 3354976-43-addressing-v2.patch | 19.05 KB | bojanz |
| #33 | address-n3354976-33.patch | 19.52 KB | damienmckenna |
| #33 | address-n3354976-33.interdiff.txt | 2.69 KB | damienmckenna |
Comments
Comment #2
bojanz commentedInitial boilerplate.
The GreatBritainSubscriber is no longer wrong, but we might want to rewrite it to contain the full ISO list, keyed by ISO code, since that would be a better example.
Comment #3
devad commentedThis is a full composer report while installing commerceguys/addressing v2.0.0-beta1:
- Installing doctrine/deprecations (v1.0.0)
- Installing doctrine/collections (2.1.2)
- Installing commerceguys/addressing (v2.0.0-beta1)
The ludwig.json file should be updated like this:
All libraries are PSR-4, so no additional integration is needed.
Comment #4
devad commentedComment #5
jsacksick commentedSame patch with the signature change to the getLocale() method, let's seee if there are less test failures (haven't ran the tests locally :)).
Comment #6
jsacksick commentedWe always return a string so we can change the return type to just : string.
Comment #7
bojanz commentedAdded @devad's ludwig.json changes from #3.
Finished the address_line3 logic (the final few places require the field column to actually be created).
Comment #8
dwwOh, whoops, I see... we're now requiring PHP 8, so the PHP 7* tests are doomed.
Yeah, definitely would want a 2.0.x branch for this. Wonder if there's anything else that would be good to land in 8.x-1.x before we create that and have to start maintaining 2 branches...
Comment #9
dwwp.s. Sort of out of scope here, but it's always bothered me that this module depends so heavily on an external library that:
Any interest / possibility of moving this out of the "commerceguys" namespace into a new location that's more welcoming and that the rest of us could be added to help co-maintain?
Thanks!
-Derek
Comment #10
bojanz commented@dww
I've granted you access to the library. Every address maintainer should have access to the underlying library.
As for the name, that ship has sailed. While GitHub allows us to rename an organization/repository, Packagist doesn't. At this point, 9 years in, the library is too popular and widely used for us to break every single composer.json file.
Comment #11
devad commentedRe: #8
This Ludwig integration update patch would be nice to commit to 8.x-1.x branch before 1.x new release: #3306611: Ludwig integration update
Comment #12
damienmckennaFor anyone who needs it, here are the composer.json changes I used to add this to my project:
Then running
composer update commerceguys/addressing drupal/addressdownloaded commerceguys/addressing v2.0.0-beta1 and applied the patch.Comment #13
damienmckennaUsing the address field and changing the country from one to another results in the following error in the AJAX request:
I suspect this is the AJAX error the tests are hitting.
Comment #14
damienmckennaComment #15
damienmckennaFor anyone who's interested, because of API changes in commerceguys/addressing v2, module 8.x-1.x is no longer compatible with the library, so you can't currently shoehorn the two together to make them work, it gives this error when you clear the caches:
Comment #16
bojanz commented@DamienMcKenna
The whole point of this issue is to fix those incompatibilities, the issue summary mentions the typehints.
There's no use in mentioning that an unfinished patch doesn't work.
Comment #17
damienmckennaThe country selector AJAX request fails with this error:
This is because in AddressFormat::__construct() $this->administrativeAreaType is only defined if the address definition has "administrative_area_type" defined.
Comment #18
damienmckennaI suspect some of the bugs are in the library, e.g. this change:
.. results in the following error:
Comment #19
bojanz commentedIndeed, there was a library bug in the AddressFormat class.
Tagged https://github.com/commerceguys/addressing/releases/tag/v2.0.0-beta2
Here's an updated patch, and it covers #18.
Still incomplete, of course (missing postLoad(), other typehint fixes).
Comment #20
bojanz commentedNow with the missing file.
Comment #21
damienmckennaFixing the addViolation() method return type allows the AJAX request to load the per-country fields to work.
Comment #22
damienmckennaFWIW I manually tested the following:
* Added an Address field to a content type.
* Customized the field widget settings.
* Created a node with that field - the address field's country selector showed correctly.
* Selected a country - the fields showed correctly.
* Selected a different country (Ireland) - the new fields showed correctly.
* Entered invalid data into the address fields and clicked "save" - the data was correctly validated and an error message was shown.
* Fixed the invalid data and clicked "Save" - the node saved correctly.
* Examined the data in Devel, the data was stored correctly.
Great work!
Comment #23
damienmckennaWorking on the test coverage.
Comment #24
damienmckennaThis fixes tests/src/FunctionalJavascript/AddressDefaultWidgetTest.php locally.
Comment #25
damienmckennaThis fixes tests/src/Kernel/Formatter/AddressDefaultFormatterTest.php locally.
Comment #26
damienmckennaThe last problem stems from the fact that the Views administrativearea filter requires a default value, even though it's optional; I loaded the view into Drupal, edited the filter, but when I try to save it I get an error that says "The predefined country must be set for this filter to work" even though the field is not required.
Comment #27
damienmckennaInconsequential tweaks that just tidy the code a little.
Comment #28
damienmckenna(from the last patch)
Comment #29
damienmckennaWhat's weird is that with 8.x-1.x/v1.4.2 the address-test/views/filter-administrative-area page has the administrative area field:

Looking at the code I see the following:
* The address_test_filter_administrative_area view sets the "country_static_code" item of the "field_address_test_administrative_area" exposed filter to "AR".
* In AddressTestEventSubscriber::getAvailableCountries() it includes AU, BR. CA, GB and US, but not "AR".
Does this mean that the country_static_code value is wrong and it should actually be one of the other values? Or is there a bug in the 8.x-1.x/v1.4.x codebases that allows this field to store an invalid value?
Comment #30
damienmckennaChanging country_static_code to "BR" seems to allow this to work.
Comment #31
damienmckenna(I was incorrect in my assessment that the tests worked for me locally, I had ran the wrong test)
Comment #32
damienmckennaThe bug editing the included address_test_filter_administrative_area view with the patch also happens with the 8.x-1.x codebase. This can be reproduced as follows:
* Manually enable the address_test module.
* Edit the view.
* Open the "Content: Address:administrative_area (exposed: fixed country: AR)" filter.
* Try clicking the "Apply" button.
* The form will reload with the following error message: The predefined country must be set for this filter to work.
That said, I tracked the test failure down to the fact that in v1.4.2 the country Costa Rica did not have subdivisions but in v2 it does.
Comment #33
damienmckennaSo in the end all that was needed to fix the Views tests were some updates to match changes in the v2 library.
Comment #34
damienmckennaAnd the tests are green!
Comment #35
bojanz commentedThank you, Damien!
We are now down to one todo:
I'll circle back to this issue by the end of the week.
Comment #36
damienmckennaIn our case we've been writing custom update scripts, as I found the site had legacy address data from before it was migrated to Drupal 7 about eight years ago.
Comment #37
damienmckennaSelecting some of the countries leads to further bugs, so I need to expand the test coverage and see if I can fix the bug.
Comment #38
damienmckennaSelecting some countries results in the following error:
Selecting others results in the following:
Furthermore, selecting some countries doesn't update the country fields, but doesn't log an error. For example, select United States, wait for the fields to update, then select Singapore, it still shows the fields for United States but doesn't show an error.
Comment #39
damienmckennaNeed to work on something else, will look into this later.
FWIW I was starting by expanding the test coverage to include a few additional countries:
Comment #40
bojanz commented@DamienMcKenna
Is there a chance that you somehow got an older version of the library, and not v2.0.0-beta2?
Because beta2 clearly initializes these properties to null, so the error from #38 should not be possible:
I tried to write a unit test retrieving an address format for a country without an administrative area ("RS") and it passed successfully.
Comment #41
damienmckennaApologies, yes, I missed that the composer file was loading beta1 and hadn't updated to beta2. With beta2 it's working great.
So now just back to needing the postLoad() logic.
Comment #42
damienmckennaFor anyone who needs it, here are the updated composer.json lines:
Comment #43
bojanz commentedOpened the 2.0.x branch. Here's a reroll of the patch.
Comment #44
bojanz commentedComment #45
bojanz commentedAdded the BC logic.
Let's do one last test run before committing.
Comment #47
bojanz commentedLet's party.
@DamienMcKenna
Big thank you for your help driving this home.
Comment #48
damienmckennaFantastic news! And you are very welcome, I'm glad I was able to organize time to work on it.
Comment #49
devad commentedGreat. Thanks for all the work and commit.
I have noticed one detail in Ludwig integration. As suggested in comment #3 doctrine/collections library update to v2.1.2 would be nice if possible:
Do we need to open a follow-up ticket for this?
Comment #50
bojanz commented@devad
2.1.2 requires PHP 8.1, our minimum is 8.0.