Is there a way to rearrange the fields within the widget with a specific hook? Should I use hook_form_alter()?

To be more specific, I want to put the country field below the zip-code field when using the Address Field module.

2 Answers 2


I have two solutions for it:

  1. hook_field_widget_form_alter()
  2. Ctools plugin

Solution 1. hook_field_widget_form_alter().

 * Implements hook_field_widget_form_alter().
function MYMODULE_field_widget_form_alter(&$element, $form_state, $context) {
  if(!empty($element['#addressfield'])) {
    $element['country']['#weight'] = 100;

Use $element and $context arrays and implement own IF statement. You can find all the information about the field and user here.

Solution 2. Ctools plugin

It's possible with ctools plugins. Review addressfield/plugins/format/ or https://drupal.org/project/addressfield_vn as an example.

Just add your custom plugin with addressfield alter in 5 steps:

Step 1: Create MYMODULE/plugins/format/ folders structure.

Step 2: Add next code to your MYMODULE.module file:

 * Implements hook_ctools_plugin_directory().
function MYMODULE_ctools_plugin_directory($module, $plugin) {
  if ($module == 'addressfield') {
    return 'plugins/' . $plugin;

 * Implements hook_ctools_plugin_type().
function MYMODULE_ctools_plugin_type() {
  $plugins['format'] = array(
    'load themes' => TRUE,
  return $plugins;

Step 3: Add file with alter code. E.g.: MYMODULE/plugins/format/address-alter.inc:

 * @file
 * Move country field to the very end.

$plugin = array(
  'title' => t('Alter addressfield'),
  'format callback' => 'MYMODULE_format_address_alter_country',
  'type' => 'address',
  'weight' => -80,

 * Format callback.
 * @see CALLBACK_addressfield_format_callback()
function MYMODULE_format_address_alter_country(&$format, $address, $context = array()) {
  // Move country field to the very end.
  $format['country']['#weight'] = 100;

Step 4: drush cc all

Step 5: Enable your plugin for addressfield in customer profile fields settings. E.g. at /admin/commerce/customer-profiles/types/billing/fields/commerce_customer_address

In the current example all the alter code will be inside the MYMODULE_format_address_alter_country() function. Just play with weights of your fields and you'll get the form rearranged.


It makes sense to keep the country field in the first place, as the other fields change depending on the selected country.

  • Yeah I know, unfortunately a client wants us to move the field :(
    – Novazero
    Dec 4, 2012 at 0:32
  • 1
    hook_form_alter will help you on this.
    – niksmac
    Dec 4, 2012 at 4:37
  • Guess Ill try today and see if I can manipulate the widget form. Though I got a feeling hook_form_alter won't work.
    – Novazero
    Dec 4, 2012 at 15:45
  • Yeah hook_form_alter worked fine I just had to locate the field in the array object which was deep.
    – Novazero
    Dec 5, 2012 at 17:14
  • 1
    @milkovsky Thats what I ended up using awhile back.
    – Novazero
    Feb 21, 2014 at 16:39

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.