0

How do you change the default fields in the addressfield Module for a given country?

For Germany you have Default addressfield of Germany, while for USA you get enter image description here, including a nice drop down menu with all 51 states.

I want to change the Germany default to have

  • only one address field (only Address 1)
  • a field with a drop down menu, showing the states of Germany.

I read on drupal.stackexchange that it should be possible with hook_form_alter or with ctools module, but within 4 hours of browsing and trying, I could not figure out how?

I'm using drupal for 5 months, so I would appreciate a detailed answer, but also any short link, that helps me to solve that issue, is highly appreciated.

  • Did you figure out how to change the default fields? – Jay Haase Jun 17 '14 at 23:22
2

Address Field module uses a plugin system for building the address fields. You should create an address field plugin in your custom module.

hook_form_alter() wouldn't help much in this case.

To create a plugin for Address Field, you should first implement hook_ctools_plugin_directory() in your module to tell you have an addressfield plugin and where they live:

function YOURMODULE_ctools_plugin_directory($module, $plugin) {
  if ($module == 'addressfield' && $plugin == 'format') {
    return 'plugins/format';
  }
}

Then, create the folders plugins/format in your module. The folder names are up to you. Under this folder create an .inc file, and give it a relating name, like german-states.inc.

In this file, you should define a $plugin array, which holds the information about your plugin, including the callback function name. Then define a function which can add new form elements, edit existing form elements etc.

$plugin = array(
  'title' => t('German States'),
  'format callback' => 'YOURMODULE_format_german_states_generate',
  'type' => 'name',
  'weight' => 0,
);

function YOURMODULE_format_german_states_generate(&$format, $address) {
  // Add new form element for German states
  if ($address['country'] == 'DE') {
    $format['locality_block']['administrative_area'] = array(
      '#title' => t('State'),
      '#required' => TRUE, // Set this FALSE if you don't want it to be required.
      '#attributes' => array(
        'class' => array('state'),
        'x-autocompletetype' => 'region',
        'autocomplete' => 'region',
      ),
      '#options' => array(
        '' => t('--'),
        'BW' => t('Baden-Württemberg'),
        'BY' => t('Bavaria'),
        'BE' => t('Berlin'),
        'BB' => t('Brandenburg'),
        'HB' => t('Bremen'),
        'HH' => t('Hamburg'),
        'HE' => t('Hesse'),
        'NI' => t('Lower Saxony'),
        'MV' => t('Mecklenburg-Vorpommern'),
        'NRW' => t('North Rhine-Westphalia'),
        'RP' => t('Rhineland-Palantine'),
        'SL' => t('Saarland'),
        'SN' => t('Saxony'),
        'ST' => t('Saxony-Anhalt'),
        'SH' => t('Schleswig-Holstein'),
        'TH' => t('Thuringia'),
      ),
    );
  }
}
  • Can you provide more information on how to do this? – Jay Haase Jun 17 '14 at 23:25
  • 1
    @JayHaase I've updated my answer with an example code and some explanations. – Елин Й. Jun 18 '14 at 7:03
0

I just make a commit to a sandbox on drupal https://www.drupal.org/sandbox/mordonez/1508242

This module works fine for me (France), but should work too in your case. Locality textfield is converted into a select list, dynamically populated (using geonames) after a postal code input.

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.