3

What I'm trying to achieve:

  1. User can see block only on his own "user/[uid]/my-page" - he cant see that block on other users my-page.

  2. Users who belong to administrator role can see user block on user own page.

Right now my code shows block for user himself and also to administrator role users BUT for administrator role users it shows block on every page - not only in "my-page".

How to modify code to show block only in "my-page" also for administrator role users?

Here is the code:

<?php
global $user;
if (in_array('administrator', ($user->roles))) {
return TRUE;
}
if (arg(0) == 'user' && $user->uid == arg(1) && arg(2) == "my-page" ) {
return TRUE;
}   
else {
return FALSE;
}
?>

I used this code as a base: https://drupal.org/node/134433

  • In my experience Block visibility with PHP script does not work as expected always. Try to create block in your module with hook_block_info() and hook_block_view() and put your logic into content generation part of hook_block_view() – xurshid29 Nov 19 '13 at 12:36
  • why you are adding if (in_array('administrator', ($user->roles))) { return TRUE; } ? – arpitr Nov 19 '13 at 12:38
  • What kind of block is it. This is important because there may be better ways to hid & show the block. For example a views block can achieve this through use of filtering. – rooby Mar 15 '14 at 2:39
2

This is almost a replica of your code with a small change. I hope it helps.

  1. If you want to allow the admin to see the block on every other user profile page

    global $user;
    if (arg(0) == 'user' && ($user->uid == arg(1) || in_array('administrator', ($user->roles))) && arg(2) == "my-isn10") {
      return TRUE;
    }
    else {
      return FALSE;
    }
    
  2. If admin or normal user do not have a difference both can see only on their profile page.

    global $user;
    if (arg(0) == 'user' && $user->uid == arg(1) && arg(2) == "my-page" ) {
      return TRUE;
    }   
    else {
      return FALSE;
    }
    
  • I am not sure why code is not picking up the code style – arpitr Nov 19 '13 at 12:51

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.