Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

How to remove the Fieldset from a Drupal Address Field

Parent Feed: 

INTRODUCTION

The Drupal Address Field Module is a great tool that we use often. There are, however, many times when the default output causes some issues for us. Be default, Address Field places all of its individual field components inside of a Feldset wrapper. This is usually a nice feature but there are times when you may want to remove this Fieldset wrapper for aesthetics. Or, perhaps, you'd like to place additional fields within the Address Field's Fieldset. We'll show you how to do both.

REMOVING FIELDSETS FROM ALL ADDRESS FIELDS

As usual, Drupal provides a handy Hook Function that allows us to override the Address Field's output to remove its Fieldset wrapper.

We can use hook_field_widget_WIDGET_TYPE_form_alter to alter widget forms for a specific widget provided by another module. To remove the Fieldset wrapper from all Address Field output we simply use this hook in our own custom module to change the element type from 'fieldset' to 'container'.

NOTE:

The Address Field's 'Widget Type' name is 'addressfield_standard'. This can be discovered by examining the module's code or using the dpm() function from the Dev Module to examine the $form output returned to a hook_form_alter() function.

Create a function similar to the following in your custom module to remove the Fieldsets from all address fields.

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function MY_CUSTOM_MODULE_field_widget_addressfield_standard_form_alter(&$element, &$form_state, $context) { 
   
$element['#type'] = 'container'
}
?>

REMOVING FIELDSETS FROM SPECIFIC ADDRESS FIELDS

If you'd like to remove the Fieldsets from specific Address Field output instead of all of them then we can simply use the $context variable that is provided to our hook to only act upon certain conditions. In the example below we're checking the $context array for a specific bundle to act upon.

function MY_CUSTOM_MODULE_field_widget_addressfield_standard_form_alter(&$element, &$form_state, $context) { 
  if (
$context['instance']['bundle'] == 'student_registration'){
   
$element['#type'] = 'container'
  } 
}
?>

CUSTOM FIELDSETS

We often have a situation that requires an Address Field to have additional fields within its Fieldset, for example, email or phone number fields. To achieve this we use the method above to remove Address Field's default Fieldset wrapper and then simply add our own using Drupal's Field Group Module.

  1. Remove Address Field's Fieldset out from all or specific output using method above
  2. Create custom Drupal fields within your entity. For example, Phone Number or Email fields
  3. Create a custom Fieldset in your entity using the Field Group module's provided field groups
  4. Place the Address Field and your custom fields within the field group you created

Your output should now be similar to the 'after' image in the screenshot below.

Drupal Address Field Remove Fieldset</p>
</body></html>

Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web