Long story short, I have an auto-complete text field with a callback that will use AJAX to dynamically fill out several fields. I have no problem updating 'textfield' or 'select' type fields. However, I have failed to update the 'address' field. The core issue seems to be that I cannot generate the markup. Here is a highly simplified attempt to generate the markup for the address field:
use Drupal\Core\Render\RendererInterface;
use CommerceGuys\Addressing\AddressFormat\AddressField;
use CommerceGuys\Addressing\AddressFormat\FieldOverride;
// Load the renderer service.
$renderer = \Drupal::service('renderer');
// Preloaded address values.
$address_values = [
'country_code' => 'AU',
'address_line1' => '123 Example St',
'locality' => 'Melbourne',
'administrative_area' => 'VIC',
'postal_code' => '3000',
];
// Define the address field render array.
$address_field = [
'#type' => 'address',
'#title' => t('Example Address Field'),
'#default_value' => $address_values,
'#tree' => TRUE,
'#description' => t('Enter the address details.'),
'#field_overrides' => [
AddressField::ORGANIZATION => FieldOverride::HIDDEN,
AddressField::ADDRESS_LINE1 => FieldOverride::OPTIONAL,
AddressField::ADDRESS_LINE2 => FieldOverride::HIDDEN,
AddressField::FAMILY_NAME => FieldOverride::HIDDEN,
AddressField::GIVEN_NAME => FieldOverride::HIDDEN,
],
'#available_countries' => ['AU', 'NZ', 'GB', 'CA', 'US', 'HK'],
'#required' => TRUE,
];
// Render the address field directly.
$rendered_output = $renderer->renderPlain($address_field);
// Print the rendered HTML of the address field.
print '[' . $rendered_output . ']';
However, all it produces is an empty div:
<div></div>
Any suggestions?