0

I use the following snippet to restrict who can see a block inside admin pages. Can you help me add the restriction of a path /cart* for non admin users into this snippet?

if (user_access('access administration pages')) {
  return FALSE;
} else {
  return TRUE;
}
1

I am assuming the logic is:

Display this block only on pages with a path beginning with cart/*,
And only when the user has `access administration pages` permission

If so....

if (arg(0)=='cart' && arg(1) && user_access('access administration pages')) {
  return TRUE;
} else {
  return FALSE;
}

If you want it displayed on path cart all by itself as well, just remove the && arg(1) from the above and then it will work on cart in addtion to cart/foo and cart/foo/bar etc.

If, however, you want it only on cart/oneargumentonly, you can add in && !arg(2).

ADDITION/CORRECTION:

if (user_access('access administration pages') || (arg(0)=='cart' && arg(1))) {
  return FALSE;
} else {
  return TRUE;
}

while return FALSE if the user can access administration pages regardless of the path OR the path is cart/* regardless of any permissions.

So, if my logic is right now, if the user can access administration pages the if is "short circuited" and returns FALSE. Otherwise, if the user doesn't have this permission, it continues on and if the path is cart/* it returns FALSE. Otherwise, since we already know the user doesn't have the access administration pages permission it returns TRUE for any other path.

FINAL?

if (user_access('access administration pages') || arg(0)=='cart') {
  return FALSE;
} else {
  return TRUE;
}
  • No, it should be visible for anybody anywhere except for all paths starting with cart and not visible for admin users at all anywhere. – loparr Jul 31 '12 at 19:54
  • ah....ok....brb :) – Jimajamma Jul 31 '12 at 19:58
  • Hi, thank you for this code. It works ok but I need to hide the block on cart page as well. Now it is hidden for all pages with cart in path. can you tell why this code doesn;t work? if (user_access('access administration pages')) || (stristr(current_path(),"/cart") { return FALSE; } else { return TRUE; } – loparr Jul 31 '12 at 22:44
  • your code in your comment doesn't work on path cart because it (the path) doesn't begin with a /, it's just plain old cart. If you take the && arg(1) out of my code it will work on cart, cart/foo, cart/foo/bar etc – Jimajamma Jul 31 '12 at 22:50
  • Final solution doe's the trick. Thank you. I need to learn some basics of PHP. – loparr Aug 1 '12 at 1:38
0

This is to hide the block in cart/* for non administrator roles.

if(stristr(current_path(),"cart/") && user_access('access administration pages')) {
  return TRUE;
}
else {
  return FALSE;
}
  • Hi,I was thinking about something like this if (user_access('access administration pages')) { return FALSE; } elseif (stristr(current_path(),"/cart") {return false;} else { return TRUE; } But it is not working – loparr Jul 31 '12 at 19:47
  • Hi,I was thinking about something like this – loparr Jul 31 '12 at 19:47
  • almost, this just checks if cart/ is in the path and thus would match something like teacart/ anywhere :) – Jimajamma Jul 31 '12 at 19:47

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.