3

I have a site using Profile2 with address_field Module; and I would like to prevent users from filling out the follwing fields: Address 1 Address 2 ZIP Code Country

The goal is is only to be able to fill only CITY AND STATE; (e.g. Orlando, FLORIDA)

Thanks.

J.M.

3 Answers 3

2

in a hook_form_alter() add:

   hide($form['field_address']['und'][0]['street_block']);

you can use dpm($form['field_address']['und'][0]) to get the name of the city, and country fields.

You could also use CSS to hide the fields but I ran into problems where browsers prefilled those fields. It's better to hide it with a hook.

1
  • you solution is good (y)
    – Yuseferi
    Commented Apr 10, 2014 at 9:52
1

Refer to the address-hide-country.inc file in the address module. It has given a good example of how to hide the country field.

Also there is an example module for how to create an address field plugin to create custom plugin to act on the address field.

1

You can hide the country by checking the "Hide the country when only one is available" box in the field settings for your address field. Then you can add the following code to custom module to hide the Address1, Address2, and ZIP (where "MYMODULE" is the name of your custom module, "MYPROFILETYPE" is the machine name of the profile type you are altering, and MYFIELD is the name of your address field):

/**
 * Implementation of hook_form_profile2_edit_PROFILE_NAME_form_alter()
 */
function MYMODULE_form_profile2_edit_MYPROFILETYPE_form_alter(&$form, &$form_state){
  //dsm($form['profile_MYPROFILETYPE']['field_MYFIELD']['und'][0]);
  hide($form['profile_MYPROFILETYPE']['field_MYFIELD']['und'][0]['street_block']['thoroughfare']);
  hide($form['profile_MYPROFILETYPE']['field_MYFIELD']['und'][0]['street_block']['premise']);
  hide($form['profile_MYPROFILETYPE']['field_MYFIELD']['und'][0]['locality_block']['postal_code']);
}

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.