Needs work
Project:
Address
Version:
2.1.x-dev
Component:
Code
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
24 Dec 2025 at 07:17 UTC
Updated:
5 Nov 2026 at 07:55 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
ajayyadav commentedComment #3
ajayyadav commentedAdded patch for this.
Comment #16
anybody1. Please provide clear steps to reproduce
2.
#markupis risky, use#plain_text3. I'm unsure about this issue and the proposed solution in general ;)
4. So this should have a test to show the error
Comment #17
s_kulyk commentedHere is the updated patch. The issue is also valid for the administrative area.
Comment #18
cosmicdreams commentedI'm still having issues with some countries territories:
Areas I'm having trouble with:
I think I see the fix needed I'll provide an updated patch.
Comment #19
cosmicdreams commentedAdding a follow-up commit to MR !82 (branch 3564725-invalidargumentexception-code) that closes a regression and addresses the review feedback from #15.
Regression with the current MR / patch -17: AddressPlainFormatter::viewElement() exposes administrativeArea, locality and dependentLocality to the template as {code, name}. The format-driven loop in getValues() only produces that shape for subdivisions the country's AddressFormat lists in getUsedSubdivisionFields(). For countries that omit one — e.g. Singapore and Germany omit administrativeArea — the corresponding $values entry stays a raw string from $address->get*(). Any subsequent $values['x']['code'] access (as in patch -17, or in a downstream consumer) throws
TypeError: Cannot access offset of type string on string. We're hitting this in production today on a calendar page containing SG/DE/SA/AE/PR event addresses.
Steps to reproduce:
1. Create an Address field on an entity, formatted with the Plain formatter.
2. Save an entity with country_code = SG (or DE/SA/AE/PR — any country whose AddressFormat::getUsedSubdivisionFields() does not include administrativeArea).
3. View the entity. PHP errors with the TypeError above.
Fix: after the format-driven loop in getValues(), normalize the three subdivision-eligible fields unconditionally to the {code, name} array shape (only when not already an array, so the loop's enrichment is preserved). The shape is then guaranteed regardless of the country's format.
Also addresses review feedback from @anybody (#15): replaced all '#markup' with '#plain_text' in the formatter so address subcomponents are escaped, not rendered as markup.
Tests: I'd like to add Kernel coverage in AddressPlainFormatterTest::renderDataProvider() for SG (no admin area), DE (no admin area), and US (with admin area, code CA).
Comment #20
cosmicdreams commentedThe fix I pushed in commit b2157b7 has Kernel tests covering the subdivision-shape contract. testSubdivisionShape asserts #administrative_area, #locality, and #dependent_locality are always {code, name} arrays — fails on the previous codepath for SG/DE/AE/US-dependent-locality (raw string), passes with the getValues() normalization. Also adds SG and DE end-to-end render cases. The data provider is now static per PHPUnit 10+ requirements.
I'm using red/green testing here that includes both a failing test (that proves the existence of the bug) and a passing test (that proves this fixes the bug)
Comment #21
cosmicdreams commentedMoving to needs review so that someone can review the modified MR.
Comment #22
cosmicdreams commentedWe might want to open a follow up to handle the 2 code quality (deprecations) that the MR highlights:
See: https://git.drupalcode.org/project/address/-/merge_requests/82
Major - Call to deprecated function views_field_default_views_data(): in drupal:11.2.0 and is removed
from drupal:12.0.0. Use \Drupal::service('views.field_data_provider')
->defaultFieldImplementation($field_storage); instead.
in address.views.inc:18
Major - Class Drupal\address\Element\Address extends deprecated class
Drupal\Core\Render\Element\FormElement: in drupal:10.3.0 and is removed from drupal:12.0.0. use
\Drupal\Core\Render\Element\FormElementBase instead.
in src/Element/Address.php:51
Comment #23
csakiistvanComment #24
csakiistvanEnvironment
3564725-invalidargumentexception-code, target2.0.x)Prerequisites
addressenabled.Root cause
The plain formatter's value builder only converts the subdivision fields listed in the country's address format (
getUsedSubdivisionFields()) into the{code, name}structure the template and downstream consumers expect. For countries that omit a subdivision (e.g. Singapore/Germany omit the administrative area), that field stays a raw string. The default template tolerates it, but any consumer that accesses the value as an array (or renders it) fails — surfacing as "'code' is an invalid render array key. Value should be an array but got a string." or "Cannot access offset of type string on string".Steps
{code, name}structure unconditionally, and wrap the code/name values as plain-text render arrays.ddev drush crExpected results
{code, name}arrays for every country, regardless of whether the country's format uses that subdivision.Actual results
Before the fix (address 2.0.4), a Singapore or Germany address rendered with the Plain formatter exposed
#administrative_area(and, for other countries,#locality) as a raw string instead of the expected{code, name}array; accessing['code']on it threwTypeError: Cannot access offset of type string on string, and rendering it as a render array threw "'code' is an invalid render array key". After applying MR !82, the administrative area, locality and dependent locality are always{code, name}arrays for SG, DE, AE and US alike; the downstream['code']access succeeds and the addresses render correctly ("Singapore 238859 Singapore", "Berlin 10115 Germany", etc.).Testing produced with the assistance of an LLM.
Comment #25
cosmicdreams commentedah sounds like you independently verified my PR was good. Any blockers from merging?
Comment #29
dwwI'm getting ready to tag 2.0.5 and trying to get (hopefully) non-disruptive bug fixes in. Reviewed this. All seems reasonable to me. Rebased it and got the pipeline happy. Test-only is indeed failing, and passes with the fixes. There are a few nits I'd make, but it's not worth the churn to "fix" them, so I decided to merge to 2.0.x, cherry pick to 2.1.x, and push.
Thanks everyone!
-Derek
Comment #31
dwwArgh, now I'm having some regret. I have a custom field formatter in a project that just broke because of this. Anyone directly manipulating address render arrays got an unexpected change. 😢 I'm tempted to revert and cut a 2.0.6 whoops release.
Comment #33
dwwYeah, decided to revert this from 2.0.x and cut a 2.0.6 whoops release. 😢
I'll leave this fixed in the 2.1.x branch. We'll have to add a big release note about this for 2.1.0.
This also really could use a better issue summary. 😅
Comment #35
dwwActually, NW for the better summary. And while we're at it, a change record would be great. I see we haven't been using those in this project. 😅 This is a case that would benefit from it, with some clear before/after code examples, etc. And since this module is so heavily used, I suspect our user base will really appreciate if we flag disruptive changes, especially anything that might impact downstream code.
Comment #36
csakiistvan@dww
❌ Regression on 2.1.x — rendered the Plain formatter on 2.1.x before and after 40bd757e: after the fix, empty subdivisions print empty
<br>lines, because{% if dependent_locality.code %}inaddress-plain.html.twigis now always true (the value is a non-empty render array).Germany, only
locality,postal_codeandaddress_line1set:Country only: before
Germany; after two empty<br>lines aboveGermany.The original exception only fired for me with a template printing a whole value (
{{ country }}), not with the default template; the default template prints only.code/.name.