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?

1
  • Tried this module: Block Access link Commented Feb 13, 2015 at 19:53

2 Answers 2

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!

9
  • 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? Commented Feb 13, 2015 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") Commented Feb 13, 2015 at 18:32
  • Please keep in mind that I'm really not good with php. :) I have edited the question, is that it? Commented Feb 13, 2015 at 18:37
  • look at my edit :) in_array($needle, $haystack) Commented Feb 13, 2015 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. Commented Feb 13, 2015 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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.