Drupal 7: Addressfield - Change the order of the inner fields

User login

A request that often comes up is to change the order of the Addressfield fields.
In this example, we are creating a custom module in order to position the Country field at the bottom.
It is a very simple function, and the other elements can be positioned in a similar fashion.

<?php
/**
* Implements hook_field_widget_form_alter().
*/
function MYMODULENAME_field_widget_form_alter(&$element, $form_state, $context) { 
  if(!empty($element['#addressfield'])) {   
    $element['country']['#weight'] = 100;
  }
}

MYMODULENAME needs to be replaced with the real name of the custom module.

Tags: 
Drupal 7, Drupal Commerce, Drupal Quick Tips, Drupal theming