1

This last bug is the only thing between be and launching this site. Please help!

I have a field called publish photo? that is a checkbox in the user's profile. For users with this box unchecked, I would like to hide the user picture for users that are not logged in. I would like it to be hidden everywhere, in a display suite view and in the views view. I had the following in my template.php, which I could have sworn was working, back before I added on display suite, but it seems not to be working now.

function MYMODULE_preprocess_user_picture(&$variables) {
    if ((!isset($variables['account']->field_user_publish_photo) || $variables['account']->field_user_publish_photo[LANGUAGE_NONE][0]['value'] == '0') && $variables['logged_in'] != 1) { // hide user photo when Publish photo is turned off
        $variables['user_picture'] = '';
    }
}

What is the proper way to do this?

2
  • Have you tried unset($variables['user_picture']); instead of using $variables['user_picture'] = ''; – Pallavi Sugandhi Oct 24 '16 at 10:33
  • Yes, I did try that. In the end, I ended up using a theme_preprocess field to blank out the display suite version and implementing hook_views_pre_render for the views version. It certainly isn't a clean solution, I'd really like a better one, but if none is forthcoming, I'll write up what I did, just in case it helps someone else. Thanks! – UltraBob Oct 24 '16 at 10:36
0

template_preprocess_user_picture seems to be fair enough approach to me, the hook should be able to unset profile picture from any instance including views etc.

Only thing I doubt could go wrong in your code is conditional unsetting of images. To make sure could you comment out the condition

function MYMODULE_preprocess_user_picture(&$variables) {
        $variables['user_picture'] = '';
}

If that unset your field than problem can be narrowed to make the condition working.

Probable problem with your condition
Can you check if you are getting fields in $variables['account'] object$variables['account'], if not you might need to object the user account and check the field conditions

$uid = $variables['account']->uid;
$account = user_load($uid);
function MYMODULE_preprocess_user_picture(&$variables) {
  if ((!isset($account->field_user_publish_photo) || $account->field_user_publish_photo[LANGUAGE_NONE][0]['value'] == '0') && $variables['logged_in'] != 1) { // hide user photo when Publish photo is turned off
      $variables['user_picture'] = '';
  }
}
1
  • I tested this without the conditional in there, and it seems not to affect my display suite user profile display (although the hook is called there) or my staff list view where the hook is not even called. – UltraBob Oct 24 '16 at 6:26

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.