Problem:
I want to use the address field in a custom form which can be fulfilled with the below code
$form['address'] = [ '#type' => 'address', '#required' => TRUE, ];
This would print the following fields
Now what if we want to hide some the above fields
SOLUTION:
We can use the following code to hide certain fields
$form['address'] = [ '#type' => 'address', '#required' => TRUE, '#field_overrides' => [ AddressField::ORGANIZATION => FieldOverride::HIDDEN, ], ];
You also can limit the countries by adding the code as below
$form['donor']['address'] = [ '#type' => 'address', '#required' => TRUE, '#field_overrides' => [ AddressField::ORGANIZATION => FieldOverride::HIDDEN, ], '#available_countries' => ['US', 'CA'], ];
If you use something like
'#available_countries' => ['US',],
The country filed will be hidden and US will be by default selected