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
- Add an address field to a content type.
- Add a form alter hook and set
#disabled to true on the address element of the address field (see example code below).
- Visit the form and you should see the Country element is not disabled when it should be.
- 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.
Comments
Comment #3
tame4tex commentedI 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.
Comment #4
dieterholvoet commentedThe current MR fixes the issue for me! The disabled state is now propagated from the address element to its country element.
Comment #5
dwwThanks 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.
Comment #6
ludo.rHere's the patch from the current MR #73
Comment #8
dwwThanks for moving this forward! Left review comments on the MR.
Comment #9
tbkot commentedComment #10
benstallings commentedLooks good to me!
Comment #11
dwwThanks, 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.
Comment #12
alexortega_98 commentedUpdated patch with latest changes