1

I am presenting a form to receive an address. I want to take the data and add it to a node that has an address field (from the address field module).

Can someone point me in the right direction on how to do that?

3 Answers 3

1

I provided a code example using entity_metadata_wrapper() in this issue. As soon as I finish cleaning up the issue queue, I'll make a regular documentation page out of that. You can follow this issue to see when the documentation page gets published.

1

Here's a PHP snippet that can be used to set an address field on a node.

PHP

$lang = $node->language;
$node->field_address[$lang][0] = array(
  "country" => "US",
  "thoroughfare" => "123 Fake Street",
  "premise" => "",
  "locality" => "Beverly Hills",
  "administrative_area" => "CA",
  "postal_code" => "90210"
);

And for reference, here's the JSON that can be used with the Services module.

JSON via Services Module

{
  "nid":123,
  "field_address":{"und":[{
    "country":"US",
    "thoroughfare":"123 Fake Street",
    "premise":"",
    "locality":"Beverly Hills",
    "administrative_area":"CA",
    "postal_code":"90210"
  }]}
}

This example works for the US only, see addressfield_get_address_format() for the components that are required for your desired country(ies).

1

In drupal 8 the data insert will work only if 'country' is changed to 'country_code' and 'thoroughfare' is changed to 'address_line1'. Hence the code will be as follows

$lang = $node->language;
$node->field_address[$lang][0] = array(
  "country_code" => "US",
  "address_line1" => "123 Fake Street",
  "locality" => "Beverly Hills",
  "administrative_area" => "CA",
  "postal_code" => "90210"
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.