5

I want to import data from a external source and so am writing a module that creates content by populating the fields of node bundles in code. I followed this guide which is great.

So I am working on adding lists of properties (flats and houses) that belong to estates. I am using an address field to store the address and found this comment on the address field module page that tells me that the address field is an array and so I can access each element and set it in turn.

But what are the address field element and how do I get a list of them? Can they be anything I want? Does the create content form create the array when the form is submitted and therefore the form labels are just a guide or do I have to match them in my code? For example, a UK address has two textboxes for address (address1 and address2) that do not match any of the element names in the module documentation.

3
  • No need to crosspost to SO; this is a good question for Drupal Answers. It could use a little editing, though, if anyone is interested.
    – mpdonadio
    Commented Dec 28, 2013 at 15:41
  • Ah yes, I see I got in trouble for doing that, I thought it was a standard thing to do to reach more people. Learned my lesson! Commented Dec 28, 2013 at 19:00
  • No problem, just post to one site. 99% of all Drupal questions are a better fit here, unless it is a pure programming question that is only tangentially related to Drupal.
    – mpdonadio
    Commented Dec 28, 2013 at 20:12

1 Answer 1

7

But what are the address field element and how do I get a list of them?

You can get a list of the available field columns from addressfield_field_schema():

  • country
  • administrative_area
  • sub_administrative_area
  • locality
  • dependent_locality
  • postal_code
  • thoroughfare
  • premise
  • sub_premise
  • organisation_name
  • name_line
  • first_name
  • last_name
  • data

Can they be anything I want?

Not by default, but as the addressfield module uses ctools plugins, it's easy to extend to add your own if necessary (see the Address Field Phone module for inspiration).

Does the create content form create the array when the form is submitted and therefore the form labels are just a guide

Exactly right, the addressfield module provides labels and fields based on the currently selected country. They can be whatever suits the locale, but if you look at the names of the form elements, or inspect the field array on form submissions, you'll see they always match the keys from the field schema hook above. There's an excellent answer to a question on SO which goes some way to explaining the reasons for this implementation.

For a UK address specifically, you would populate it roughly like this:

$node->field_address[LANGUAGE_NONE][0] = array(
  'organisation_name' => 'Company',
  'first_name' => 'Joe',
  'last_name' => 'Bloggs',
  'country' => 'GB',
  'thoroughfare' => 'Street address 1',
  'premise' => 'Street Address 2',
  'locality' => 'Town/City',
  'administrative_area' => 'County',
  'postal_code' => 'SW1 1AA',
);
3
  • Perfect answer, thank you very much. Makes sense to me now. Could this be put somewhere on the module's API page? I am sure it would help a lot of us newbies no end. Is the technique of looking at the _schema() function in an .install file a good one to use for understanding custom fields and entities? Commented Dec 28, 2013 at 19:46
  • 1
    Yeah I always go for the field schema hook when I want to know what a field's expecting. All fields have to define their columns that way so it's reliable
    – Clive
    Commented Dec 29, 2013 at 5:09
  • Having poked around drupal for a week or so now, I also find that the dpm() function from the development module is excellent for seeing the structure of the form fields. I add dpm($form) to my hook_form_alter() implementation and get to see the whole array. Commented Jan 3, 2014 at 21:42

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.