Problem/Motivation

Prior to version 2.0.3, you could set #disabled to TRUE on the parent address element in a form alter hook, and all child elements would be disabled. From 2.0.3 onwards the country_code element is no longer disabled.

The fix introduced in 2.0.3 for issue #3495694: Give support for '#disabled' property in address_country form element is the cause for this bug. The new #disabled declaration on the country_code element causes the declaration on the parent address element to be ignored.

Steps to reproduce

  1. Add an address field to a content type.
  2. Add a form alter hook and set #disabled to true on the address element of the address field (see example code below).
  3. Visit the form and you should see the Country element is not disabled when it should be.
  4. If you create a node with an address set, on the edit form you should see all the Address fields are disabled except for the Country field.
function address_test_form_node_article_form_alter(&$form, FormStateInterface $form_state): void {
  $form['field_address']['widget'][0]['address']['#disabled'] = TRUE;
}

function address_test_form_node_article_edit_form_alter(&$form, FormStateInterface $form_state): void {
  $form['field_address']['widget'][0]['address']['#disabled'] = TRUE;
}

Proposed resolution

  • Add testing to ensure setting #disabled to TRUE on the address element results in all child elements being disabled
  • Refactor \Drupal\address\Element\Address::processAddress() to set #disabled on the country_code element based on the parent address element.

Issue fork address-3538160

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

tame4tex created an issue. See original summary.

tame4tex’s picture

Assigned: tame4tex » Unassigned
Status: Active » Needs review

I have created a MR with the new test and fix.

There are failing PHPUnit tests on the MR but I believe they are unrelated to the changes made as they appear to be occurring on other MR.

dieterholvoet’s picture

Status: Needs review » Reviewed & tested by the community

The current MR fixes the issue for me! The disabled state is now propagated from the address element to its country element.

dww’s picture

Thanks for this bug report and proposed fixed!

Even though it's the older issue, I just marked #3499837: Warning: Undefined array key "#disabled" in Drupal\address\Element\Country::processCountry() (line 100 of src/Element/Country.php). as duplicate, since the MR over there only includes the fix, while this one also includes test coverage.

Haven't actually reviewed yet, so I'm not going to commit, but this seems promising on a very quick skim. The fix itself looks right. Not sure about the tests.

ludo.r’s picture

Here's the patch from the current MR #73

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

dww’s picture

Status: Reviewed & tested by the community » Needs work

Thanks for moving this forward! Left review comments on the MR.

tbkot’s picture

Status: Needs work » Needs review
benstallings’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me!

dww’s picture

Status: Reviewed & tested by the community » Needs review

Thanks, definitely looking much better!

Resolved some of the MR threads, but opened some others (mostly with nit-picky suggestions).

But also wondering if we can save more of the test coverage from the earlier versions of this so that we're testing everything about enabling/disabling address form elements, that all the sub-elements behave as expected, etc.

alexortega_98’s picture

Updated patch with latest changes