0

I want to display the block only when the current user visits own page. So in block visibility I use the following PHP:

global $user;
if ($user->uid == arg(1)) {;
return TRUE;
} else {;
return FALSE;
};
endif;

But that block returns on every page.What am I doing wrong?

  • By own page you mean user profile page? – Somya Gupta Oct 19 '13 at 7:59
  • yes own profile page which is normally located on domain.com/en/user/# , although I have set it to display as domain.com/en/username – drupalina Oct 19 '13 at 8:02
1

If you are using the php code in the block visibility option than I guess the following should work:

<?php
global $user;
$user_id = $user->uid;
if (('user' === arg(0)) && ($user_id === arg(1))) {
  return TRUE;
} else {
  return FALSE;
}
?>

If your site is multi-lingual than may be you have to change the if condition as: if (('user' === arg(1)) && ($user_id === arg(2)))

  • If user on edit profile this section is also displaying. – Rajesh Vishwakarma May 11 '15 at 12:49
0

The provided code is insufficient, as it does not check whether the user is editing their account (user/uid/edit), or simply viewing it (user/uid or user/uid/view).

<?php
$uid = $GLOBALS['user']->uid;
if ('user' == arg(0) && $uid == arg(1) && ('view' == arg(2) || NULL == arg(2))) {
  return TRUE;
}
return FALSE;
?>

The above code ensures that your block will ONLY ever be visible when the user account is being viewed (at /user/uid or /user/uid/view) by the user the account belongs to.

Personally, I would simply use Views to create a page, which uses a contextual filter that uses the currently logged-in user to display specific information about them.

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.