6

I am working on multi-user website with 3 roles in Drupal 7 website. I'm using profile2 module for custom fields for different users, also profile2 path for profile/user registration.

Problem came up with Location module because I need 3 different location cck (different fields from location cck are allowed) fields from every user/profile role. I got stuck with this more than 2 months. It would be nice that I can use Profile2 and Location cck but it's not possible because Views module does not extract different Location fields for my Profile2 CCK fields in different profiles (for example I need only phone or city field from location in some view).

The only way to achieve this (so far) is to make 3 location cck fields in user account fields and with field permission module grant privileges for specific roles.

But! I need to expose that location fields on profile2 registration paths with all other fields from specific profile and they need to be required.

Is there a way to hide location cck fields on the registration form based on user type (buyer, seller, etc)?

1 Answer 1

1

to hide and show fields on user registration form while using Profile2 path - try the following in hook_form_alter in template.php.

if ($form_id == 'user_register_form') { 

// get current path  
$request_path = request_path();  

// check if current path corresponds to your registration form for a particular role  
if (strpos($request_path,'buyer') !== false) {    

// removes the field that should be hidden
$form['field_1']['und'][0]['value']['#access'] = FALSE;

// or   
unset($form['field_1']['und'][0]['value']);

// set the state of the field to required  
 $form['field_2']['und'][0]['value']['#required'] = TRUE;

}  
}

simple warning - you may find that Location module fields that utilize ajax (same goes for Address Field module) have a bug that breaks the user reg. form produced by Profile2 Path if it is submitted with errors and exposes fields hidden with method described above. Look for insight in this issue (https://www.drupal.org/node/2558335).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.