1

I'm trying to get the currently logged-in user's status (active or blocked) in template_preprocess_user. I tried the following with no luck.

function MYTHEME_preprocess_user(&$variables) {
  $user = $variables['user'];
  $variables['status'] = $user->status();
}

function MYTHEME_preprocess_user(&$variables) {
  $user = $variables['user'];
  $variables['status'] = $user->getStatus();
}
0

1 Answer 1

5

Assuming this is Drupal 8, try $user->isBlocked().

If this is Drupal 7, you probably just need to call user_load($uid) first in order to get a fully-loaded user object before checking the $user->status. Or try user_is_blocked($name).

2
  • Yes, Drupal 8, and $user->isBlocked() worked! For $variables['status'] = $user->isBlocked() In my user twig file, {{ status }} would display 1 for blocked user, and nothing for active user. So I was able to do {% if status == '1' %} and do different things depending on if the user is active or blocked Thanks very much!
    – kokamow
    Oct 30, 2020 at 19:09
  • If it worked, can you accept the answer? See stackoverflow.com/help/someone-answers
    – anonymous
    Nov 5, 2020 at 9:58

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.