Objective: To override the default classes and divs that are spit out from the Form-API from my custom addressfield implementation in a Drupal Commerce checkout pane. I need to make the classes/divs predictable based on the number of seperate address forms their are. I dynamically create (x) addressfields based on the number of gift subscriptions (x) so that I can combine address information for subscriptions.

Note: The addressfield form is a patch from the addressfield module which creates a form element type. This looks like this:

          $form['address'] = array(
          '#type' => 'addressfield',
          '#required' => TRUE,
          '#title' => t('Recipient\'s Address'),
          '#context' => array('countries' => array('AT', 'AS', 'BE', 'BG', 'CA', 'CY', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'GU', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PR', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'GB', 'UM', 'US', 'VI'), ''),
          '#default_value' => array('country' => 'US'),
          '#description' => "Please enter the address you want to mail this gift to.",
          '#maxlength' => 60,

However the problem arises when you load the page with the form. Drupal is dynamically creating numbers in the classes/divs that appears to be random. I would like to specify that number so that it conincedes with the number of subscriptions. So when i test i select (2) gift subscriptions and get and .

As opposed to the current way it does things (screenshot)

enter image description here

  • Are you calling every address $form['address'] or do you give them all a unique key? Sep 18, 2012 at 23:37

2 Answers 2


See line 398 of plugins/address.inc ? $format['#wrapper_id'] = drupal_html_id('addressfield-wrapper'); That's the root of the seemingly arbitrary field identifier stuff. The code gets there from line 460 of addressfield.module $element += addressfield_generate($address, $settings['format_handlers'], $context);

Make a custom module, set the weight higher than that of addressfield so it runs after the addressfield module's hooks, and alter the form. There aren't hooks inviting you to alter those IDs unique to addressfield, and altering them in this case is no different than any other, so long as the module weight is adjusted.


If you are using 7, try hook_page_alter()

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.