1

What is the simplest code that checks if user is admin and then prints a block to that user in node.tpl.php? I have seen different examples, but they are all either complicated and deal with other problems at the same time or they include editing template.php, which I want to avoid. I have tried with this, but it doesn't work:

<?php if ($is_admin->true): ?>
<?php print views_embed_view('myview','block'); ?>
<?php endif; ?>

Is there such a simple way to achieve this?

  • Tried this module: Block Access link – abhiklpm Feb 13 '15 at 19:53
4

You can do something like this:

global $user;
if (in_array('admin', $user->roles)) {  //if not "admin", maybe "administrator"?

}

Or you can use the built-in block access control, under your block's configuration you'll see "Visibility Settings" and in there is a way to restrict by user role.

Edit: what the hell, it's friday and I'm bored.

Take this in small steps before you conclude it's "not working".

Step 1)

global $user;
if (in_array('admin', $user->roles)) {  //if not "admin", maybe "administrator"?
    echo "<hr>admin user!<hr>";
}

If that works, then start making it more complex. This is the best way to learn how to do new things, especially with PHP since you get to see your stuff immediately!

  • Block settings are easy, but I have to use tpl.php in this case. Where exactly do I specify here that it should work for admin only? – take2 Feb 13 '15 at 18:30
  • just put in the name of the role you want, so in this case 'admin' (I think it's shortened, might be "administrator") – user5482 Feb 13 '15 at 18:32
  • Please keep in mind that I'm really not good with php. :) I have edited the question, is that it? – take2 Feb 13 '15 at 18:37
  • look at my edit :) in_array($needle, $haystack) – user5482 Feb 13 '15 at 18:52
  • OK, tried it, but it didn't output anything. You can see the whole code at the bottom of the question. I have tried both admin and administrator. – take2 Feb 13 '15 at 18:58
0

try this:

<?php global $user; ?>
<?php if ($user->roles[5]): ?>
<?php print views_embed_view('myview','block'); ?>
<?php endif; ?>

just make sure of the role number from admin/people/permissions/roles hover the link on your Administrator edit role you'll see the number of the role at the end of the link

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.