I'm busy developing a custom shipping module and want to have an auto-complete/select list of towns but the available data for the list is from a webservice (the reason for this is so that the shipping company can update their details without me having to update on my side the whole time).

How do I interact/modify/manipulate Address Field fields? I tried to modify field.tpl.php to hide a country but it does not affect the address section in checkout, only the products section of the cart (and somehow I don't think this is the way to go anyway). I also looked at addressfield.api.php but that looks very limited. Should I just use normal fields one by one (street, town, zip code etc) and try and modify them. I'm not sure where to even start researching what to do to accomplish this so any pointers in some direction will be appreciated.

  • This currently reads like a job spec, you haven't actually asked a specific question. Please bear in mind that this is a Q+A site and as such you need to ask a narrowly scoped, clear question in a manner that will be useful to future visitors. Asking how to make a contrib module "more dynamic with my custom module" doesn't make much sense, you'll need to identify what you want to change and let us know where your attempts to do so have failed so far – Clive Nov 29 '14 at 9:48
  • You're right, I actually thought about that after posting. My apologies. Will think about it and narrow it down and edit the question. – Christo Nov 29 '14 at 12:07
  • Updated the question - I hope this is more accetpable – Christo Nov 29 '14 at 19:17
  • I didn't think to edit the Postal Address field in Customer Profile/Billing Information in the UI and I see that is where I enable the address field example module & plugin (couldn't figure it out). I'm pretty sure this is where I need to fiddle so will provide feedback if I get anywhere. – Christo Nov 29 '14 at 21:21
  • Sorry, to clarify, "this" meaning the example module and plugins not the UI – Christo Nov 29 '14 at 21:50

More than likely, the best approach you can take here would be to add the AddressField reference to wherever it needs to be, and then add in logic to poll and collect address data inside a form validation call inside a module, using a hook_form_FORM_ID_alter() 1 call, when new Entity is created or edited.

This guide provides additional information on using validation calls in Forms: https://api.drupal.org/api/examples/form_example!form_example_tutorial.inc/7

  • I didn't actually try this method so cannot say if it would've worked for me but I can definitely say that it pointed me in the right direction. thanks – Christo Dec 1 '14 at 20:02

I managed to do this by adding a plugin to Address Field. I added the following to my .module file - this plugin has to be enabled in the UI by going to admin -> store -> customer profiles -> profile types - billing info and edit the field Postal Address

function commerce_shipping_mds_ctools_plugin_directory($module, $plugin) {
 if ($module == 'addressfield') {
return 'plugins/' . $plugin;

and then create an .inc in the plugins folder with the following code

$plugin = array(
  'title' => t('Commerce shipping MDS integration'),
  'format callback' => 'mds_addressfield_format_address_generate',
  'type' => 'address',
  'weight' => -80,

function mds_addressfield_format_address_generate(&$format, $address, $context = array()) {

  *  Use MDS API to get towns and populate select list

  $format['locality_block']['locality'] = array(
    '#title' => t('City'),  
    '#options' => commerce_shipping_mds_getTowns(), 
    '#required' => TRUE, 
    '#prefix' => ' ', 
    '#attributes' => array('class' => array('locality')),
    '#ajax' => array(
      'event' => 'change',
      'wrapper' => 'suburbs-div',
      'callback' => 'commerce_shipping_mds_ajax_getSuburbs_callback',
      'method' => 'replace',

if (isset($address['locality'])) {
      $mds_suburbs = commerce_shipping_mds_getSuburbs($address['locality']);
      $format['locality_block']['dependent_locality'] = array(
        '#title' => t('Suburb'),  
        '#options' => $mds_suburbs, 
        '#required' => TRUE, 
        '#prefix' => '<div id="suburbs-div">', 
        '#suffix' => '</div>',
        '#type' => 'select',
        //'#attributes' => array('class' => array('locality')),

*  Add the Ajax

function commerce_shipping_mds_ajax_getSuburbs_callback($form, $form_state) {
    return $form['locality_block']['dependent_locality'];

It is working fine on the 1st drop-down list but the AJAX for the second drop down list (suburbs) is not working properly yet . Will post another question for that and update with the link in case someone else has the same issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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