Steps to reproduce

when all options in the address field are empty and display the field value as plain text then it will occur this error.

Issue fork address-3564725

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

ajayyadav created an issue. See original summary.

ajayyadav’s picture

ajayyadav’s picture

StatusFileSize
new1.99 KB

Added patch for this.

ajayyadav changed the visibility of the branch 3564725-invalidargumentexception-code-is to hidden.

ajayyadav changed the visibility of the branch 3564725-invalidargumentexception-code-is to active.

ajayyadav changed the visibility of the branch 3564725-invalidargumentexception-code-is to hidden.

ajayyadav changed the visibility of the branch 3564725- to hidden.

ajayyadav changed the visibility of the branch 3564725- to active.

ajayyadav changed the visibility of the branch 3564725-invalidargumentexception-code-is to active.

ajayyadav changed the visibility of the branch 3564725- to hidden.

ajayyadav changed the visibility of the branch 3564725- to active.

ajayyadav changed the visibility of the branch 3564725- to hidden.

anybody’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

1. Please provide clear steps to reproduce
2. #markup is risky, use #plain_text
3. I'm unsure about this issue and the proposed solution in general ;)
4. So this should have a test to show the error

s_kulyk’s picture

StatusFileSize
new2.88 KB

Here is the updated patch. The issue is also valid for the administrative area.

cosmicdreams’s picture

I'm still having issues with some countries territories:

Areas I'm having trouble with:

  1. United Arab Emirates - AE
  2. Puerto Rico - PR
  3. Singapore - SG

I think I see the fix needed I'll provide an updated patch.

cosmicdreams’s picture

Adding 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).

cosmicdreams’s picture

The 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)

cosmicdreams’s picture

Status: Needs work » Needs review

Moving to needs review so that someone can review the modified MR.

cosmicdreams’s picture

We 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

csakiistvan’s picture

Assigned: Unassigned » csakiistvan
csakiistvan’s picture

Assigned: csakiistvan » Unassigned
Status: Needs review » Reviewed & tested by the community

Environment

  • Drupal: 11.4.1
  • PHP: 8.5.5
  • Database: MariaDB 10.11.16
  • DDEV: v1.25.2
  • address: 2.0.4 (fix from MR !82, branch 3564725-invalidargumentexception-code, target 2.0.x)
  • Browser: Chrome

Prerequisites

  • address enabled.
  • A content type (e.g. Article) with an Address field displayed with the Plain formatter.
  • Content with addresses in countries whose address format omits the administrative area, e.g. Singapore (SG) or Germany (DE).

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

  1. Apply the fix from MR !82: normalize the administrative area, locality and dependent locality to the {code, name} structure unconditionally, and wrap the code/name values as plain-text render arrays.
  2. Rebuild caches: ddev drush cr
  3. Create/view an entity with a Singapore or Germany address rendered with the Plain formatter.
  4. Confirm the address renders without the "invalid render array key" / "Cannot access offset" error, and that the subdivision variables are consistently structured.

Expected results

  • The Plain formatter exposes the administrative area, locality and dependent locality as {code, name} arrays for every country, regardless of whether the country's format uses that subdivision.
  • Rendering the address, and accessing the subdivision values as arrays downstream, no longer throws.

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 threw TypeError: 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.

cosmicdreams’s picture

ah sounds like you independently verified my PR was good. Any blockers from merging?

dww made their first commit to this issue’s fork.

  • dww committed 40bd757e on 2.0.x
    fix: #3564725 InvalidArgumentException: "code" is an invalid render...

  • dww committed 6adc1087 on 2.1.x
    fix: #3564725 InvalidArgumentException: "code" is an invalid render...
dww’s picture

Version: 2.0.4 » 2.0.x-dev
Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs tests

I'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

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

dww’s picture

Status: Fixed » Needs review

Argh, 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.

  • dww committed 67ad9928 on 2.0.x
    revert: #3564725 InvalidArgumentException: "code" is an invalid render...
dww’s picture

Version: 2.0.x-dev » 2.1.x-dev
Status: Needs review » Fixed
Issue tags: +Needs release note, +Needs issue summary update

Yeah, 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. 😅

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

dww’s picture

Status: Fixed » Needs work
Issue tags: +Needs change record

Actually, 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.

csakiistvan’s picture

@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 %} in address-plain.html.twig is now always true (the value is a non-empty render array).

Germany, only locality, postal_code and address_line1 set:

Before:                After:
X 1 <br>               X 1 <br>
Berlin 10115 <br>      <br>
Germany                Berlin 10115 <br>
                       Germany

Country only: before Germany; after two empty <br> lines above Germany.

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.