0

I use simple code to hide field from profile2 main profile based on another field inside the profile2 main profile. If field_testcheckbox is not checked, hide field_info.

function odbornik_fields_form_profile2_edit_main_form_alter(&$form, &$form_state){
global $user;
$profile = profile2_load_by_user($user->uid, 'main');
$checkbox = $profile->field_testcheckbox['und'][0]['value'];
  if ($checkbox == 0) {  
     $form['profile_main']['field_info'][LANGUAGE_NONE][0]['#type'] = 'hidden'; 
    };
}

This code works ok for all users browsing their profiles, except admin user browsing own or others profiles, where I get error: Notice: Trying to get property of non-object on line with this code.

$checkbox = $profile->field_testcheckbox['und'][0]['value'];

Admin has the main profile as well and the checkbox field is there too. What can be the problem? Thank you.

1 Answer 1

1

You are assuming there is a profile for all users and your admin user seems to not have one. Add a check if there is a profile for the current logged in user:

function odbornik_fields_form_profile2_edit_main_form_alter(&$form, &$form_state){
global $user;
$profile = profile2_load_by_user($user->uid, 'main');
if($profile) {
  $checkbox = $profile->field_testcheckbox['und'][0]['value'];
    if ($checkbox == 0) {  
       $form['profile_main']['field_info'][LANGUAGE_NONE][0]['#type'] = 'hidden'; 
     }
  }
}

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.