Can't save data for Address field I am trying to save data programmatically to an address field from the Address module.

I am not successful.

First, when I dsm() a node with the address field filled out, it looks like this:

        [field_addr] =&gt; Array
            (
                [x-default] =&gt; Array
                    (
                        [0] =&gt; Array
                            (
                                [langcode] =&gt; 
                                [country_code] =&gt; US
                                [administrative_area] =&gt; CA
                                [locality] =&gt; San Diego
                                [dependent_locality] =&gt; 
                                [postal_code] =&gt; 91945
                                [sorting_code] =&gt; 
                                [address_line1] =&gt; 123 main st
                                [address_line2] =&gt; apt 2
                                [organization] =&gt; 
                                [given_name] =&gt; 
                                [additional_name] =&gt; 
                                [family_name] =&gt; 
                            )

                    )

            )


However, no matter how I try to save it to my node, it doesn't work.

I create an array that looks like this:

Array
(
    [x-default] =&gt; Array
        (
            [0] =&gt; Array
                (
                    [address_line1] =&gt; Australian Club
                    [address_line2] =&gt; Royal Lane
                    [locality] =&gt; Sydney
                    [administrative_area] =&gt; NSW
                    [country] =&gt; Australia
                )

        )

)


Let's say with the name, $address_save

And then do the following:

$node-&gt;set('field_addr', $address_save);


No luck. My node saves with other fields, but this one doesn't. I've also tried it without the x-default key around it as well as without a delta, but neither of those work either.

How can I save values to an Address field inside of a node?
The problem here was due to Drupal failing silently (or obfuscating the error - maybe because I was running this in cron) but it was due to the address not having all of the required fields set to the correct values for the country in question - mainly using the actual country name instead of the country code.

Once the country code was swapped in it worked. I switched the country around to the country code like this:

function MY_MODULE_get_country_code($country) {
  $countries = MY_MODULE_country_list();
  $flipped_countries = array_flip($countries);
  $country_code = $flipped_countries[$country];
  return $country_code;
}

function MY_MODULE_country_list() {
  $countries = \Drupal\Core\Locale\CountryManager::getStandardList();
  foreach ($countries as $key =&gt; $value) {
    $countries[$key] = $value-&gt;__toString();
  }
  return $countries;
}


It worked correctly.