As of Drupal 5.x you can specify what roles can see a block through the block admin UI, but using PHP still gives you extra flexibility.
If you are going to set visibility on a lot of blocks, see Block Page Visibility
Review of how the role visibility works in Drupal 5
- If no role check boxes are checked, block is visible to all roles.
- If "authenticated user" is checked, all logged in users will see the block, checking any of the other role boxes (other than anonymous user) is meaningless.
- Checking anonymous users means that logged-in users, unless they belong to another role that is also checked, will not see the block.
Basics of setting visability via PHP:
- Go to administer >> access control and ensure “administer blocks “ and “use PHP for block visibility” are checked for your role.
- Go to administer >> blocks and find the block you want to set the visibility for.
- Click “configure” next to that block.
- Under the section entitled, “Page specific visibility settings”, click the radio button for “Show if the following PHP code returns TRUE”
- In the text box provided, enter whichever of the following snippets meets your needs:
Sample Snippets
Show block only to logged in users:
(In Drupal 5 this is done by simply checking the "authenticated user" box.)
<?php
global $user;
if ($user->uid){
return TRUE;
} else {
return FALSE;
}
?>
Variation: “if (!$user->uid){“ will show block only to anonymous users.
Show block only to a particular role:
(In Drupal 5 this is done by simply checking the box of the desired role.)
<?php
global $user;
if (in_array('Approved Role',$user->roles)) {
return TRUE;
} else {
return FALSE;
}
?>
Variations: Replace "Approved Role" with whichever role you want to view it. Or use !in_array to hide it for that role.
Show block set of roles:
(Thanks Chris Herberte). (In Drupal 5 this is done by checking role checkboxes.)
global $user;
$allowed = array('moderator','administrator');
$valid=FALSE;
foreach($user->roles as $role){
if(in_array($role, $allowed)) {
$valid=TRUE;
}
}
return $valid;
Don't show block set of roles:
(This snippet is quite helpful in Drupal 5. If you want to show the block to authenticated users who have no other role but not to other authenticated users who do have a role, the checkboxes will be of no help.)
global $user;
// These are the roles who should not see this block
$notallowed = array('anonymous user','artist','GoZabo Administrator', 'SuperAdmin');
// Assume they can see it to start
$valid=TRUE;
// Go through each role the user is in and, if we hit a role that is not allowed to see it, set valid to false
foreach($user->roles as $role){
if(in_array($role, $notallowed)) {
$valid=FALSE;
}
}
// If no roles were hit that aren't allowed, this will still be true. Otherwise it will be false.
return $valid;
Show block only to a particular user:
<?php
global $user;
if ($user->uid == 1){
return TRUE;
} else {
return FALSE;
}
?>
Variations: Replace the “1” with any UID.
Show block only for specific content type:
<?php
$match = FALSE;
$types = array('story' => 1, 'page' => 1);
if (arg(0) == 'node' && is_numeric(arg(1))) {
$nid = arg(1);
$node = node_load(array('nid' => $nid));
$type = $node->type;
if (isset($types[$type])) {
$match = TRUE;
}
}
return $match;
?>
Variations: This example would show the block on all 'story' and 'page' type nodes. Just change line 2 - the $types array - to indicate which node types you want your block to appear on. Use the format 'nodetype' => 1 for each type you need. And yes, the array can hold single type only.
Combining PHP visibility control with specific page visibility:
When you choose the option to control visibility by PHP, you lose the ability to have Drupal automatically show/not show a block based on path. The following workaround is a bit complex and clunky, but it works.
- On the page administer >> blocks, hover over the configure link for the block you want to control in order to view the URL. Most of the URLs will end in “configure/sometext/somenumberortext”. Make a note of the “sometext” and the “somenumberortext”.
- Set the region for this block to "None". From now on let's call this block "Original Block."
- Go to administer >> blocks >> add block (tab) . In the block description field (what shows up on the admin/build/block page) write something like "Original Block Name Visible".
- Set the block body input type to PHP.
- What we do here, in the body (not in the visibility settings) is that we set the non page orientated visibility settings with php code. If the visability settings we set are TRUE -- then the code calls to output the actual content of our now disabled, original block. Pretty tricky huh! As an example, we are going to use a simple visability snippet. Start out with this shell in the block body text area:
- Paste in just the “if” part from one of the snippets above. We’ll use the first snippet as an example. You should have something like this:
- Add in the block calling code so it looks like this:
- Replace with the “SOMETEXT” and “SOMENUMBER” that you noted in step 1.
- The final result should look something like this:
- That takes care of showing the block only to logged in users (or whatever snippet you used). Next go to the “Page specific visibility settings” (you are still in this new block, not the original block) section and set one of the first two radio buttons as normal. Add the page specific setting in the usual place
- Enable and place this new block and check to see that the original one has its region set to "none". Make sure you don't delete the original one. That's where the content is coming from. Examine the block descriptions for both of them and describe them in a way that, hopefully, won't confuse you later. It's a good idea to add commenting to the php snippet explaining exactly what you've done.
<?php
global $user;
$output = '';
return $output;
?>
<?php
global $user;
$output = '';
if ($user->uid){
}
return $output;
?>
<?php
global $user;
$output = '';
if ($user->uid){
$block = SOMETEXT_block('view', SOMENUMBER);
$output = $block['content'];
}
return $output;
?>
<?php
global $user;
$output = '';
if ($user->uid){
$block = menu_block('view', 10);
$output = $block['content'];
}
return $output;
?>
Comments
I needed to show a block for
I needed to show a block for specific content type as well as specific url aliases.
I believe I read the arg(0) function doesn't work correctly for all content types so I combined this with $_SERVER['REQUEST_URI'] that works well:
Here is the combined snippet:
-----------------------------------------------------------------
Petafoo - We Love Animals
maybe better
The following code is just slightly modified v1nce's snippet.
Changes:
I use Drupal
request_uri()
function instead of$_SERVER['REQUEST_URI']
to get URL of current page andstrpos()
to show block when this URL contains specified word ("blog" in this case; you can change it on line 13).Visibility set to TRUE based on taxonomy term OR url
Here is another snippet cut and paste. This one combines the need to make a block visible based on a node's taxonomy term OR the presence of a string in part of the URL. I have a topic area of a site which includes nodes and views and I needed the block to show both the nodes (based on taxonomy term) and views (based on a portion of the path).
Note: I'm a relatively new coder. Not only do I like comments in code, but I also like verbose semantically oriented names for variables. I know a lot of coders like to use short variable names. Here the variable names are long and verbose and hopefully make the code easier to understand.
Shai Gluskin
content2zero.com
content2zero.com
See also: How to control
See also:
For Drupal 6
modified for D6:
Alter for Vocab ID
This works great for Terms. How would I alter it to work for and entire vocabulary? The initial one posted at the top isn't working for me in Version 6.
Also how do I alter it so that it works for multiple term ids. The one you posted only works for 1 term.
Do Not Show Block for Specific Node Type
Here is a variation of the code posted above. This time it prevents the block from being shown for specific node types, in this case story and page.
--
Scott M. Stolz
http://www.wistex.com/
specific content type if current user is author
Combining a couple of methods shown above and using the conditional statement
if ($user->uid == $node->uid)
, I created this code to only show a block when the node is a certain content type (page) AND the viewer is also the author of the node.Sincerely,
Andrew G
This code also works in panels
This will works in panels when you want to show a pane only to the author of the post.
Thanks
Block to appear on user/1 but not user/1/edit or user/register
user/1, user/2, user/3, etc. Block shows
user/1/edit. Block doesn't show because arg(2) exists
user/register. Block doesn't show because arg(1) isn't numeric.
visibility based on nodetype submission...?
i created a nodetype that is associated with a user's profile vis a vis the Content Profile module. i want to limit a block's visibility based on whether or not they have created that piece of content. so, for example, i have a nodetype called 'am_registration' which is associated to the user who created it. if they have already filled out this content type once, i do not want the block to show. because i only want this block to show up on one page, i have the code for it in the body section:
it works in terms of becoming invisible when the user is not logged in (if ($user->uid)) but the next embedded if statement doesn't seem to be doing a damn thing...
ideas?
--
anawillem
http://jellobrain.com
block visible for specific content type
if ($node = menu_get_object()) {
return($node->type=='agent' || $node->type=='exhibition')
}
--
keine zeit für spielkonsolen mein leben ist jump n run!
valderama.net
add missing semicolon and it works perfectly
Hi, thanks for the 6x snippet above, it's working great for me. The only snag I hit was that the "return" line is missing a semi-colon at the very end of the line. After adding the semicolon it started working.
Steve
Revista Y AHORA QUÉ | Selectividad - Universidades - Foro Blog Chat Estudiantes
Check visibility on site-wide contact page
Any clue about testing visibilty on site-wide contact page?
works just fine
I did exactly what you said. İt worked great. Guys you are just awsome :)
Combining two arguments
We need to figure out a way to combine these two php codes for block visibility, which so far we haven't been able to figure it out. Can anyone help? We need a block to show on the profile page for author (shown below), but not have that block on the edit page for that same user's profile.
This one shows a block on the profile page only if the profile belongs to the logged in user:
This one will show a block on the profile page, but not the profile subpages, like user/1/xxx:
We tried this and it doesn't work. It makes the block not show at all:
Fixed: Show block for node as author and NOT on edit pages
Here is the code that works:
Block visible only on the profile page not on user/*/edit and..
Block visible only on the profile page not on user/*/edit and user/*/register.
I feel really happy to help because even i needed this and was able to achieve this, Try this :
Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form
visible on nodes authored by logged in user
Trying to display one thing to the author of the node and something different to everyone else viewing it:
but I get the negative NOT message in all cases, even when the author of the node is the currently logged in user.
The possible catch is that I am trying to use this functionality for the "empty text" of a view.
This should be easy... is there something wrong with my php? Thanks
I have the same issue: I want
I have the same issue: I want to display a block (http://drupal.org/project/authorcontact) for every user, except for the author of the node.
I used this code:
But :
- the block is still displayed for every logged user, including the node's author
- the block is not displayed for anonymous users
Any solution?
just a guess: load node
I think you have to load the $node before you can access its uid.
So it should be something like
hope it helps, cossme
Thanks a lot Cossme, it works
Thanks a lot Cossme, it works perfectly!
screen size
Hi some1 can say me what is the code pour the block visibility by screen size ?
thank for you help
Block visible based on contents of a user profile field
If you had a profile field to store a favorite color, you could use this to display a block if the favorite color matches one of the items in the array.
Block visible on publishing of specific node type.
Any ideas? Looking to only show a block when a "Special Alert" node type is published and not show the block when the "Special Alert" node type is unpublished or deleted.
Any idea to show a block only
Any idea to show a block only when the page title containing some string?
Better
these all information is very well for visibility of block but i have an problem where anonymous user does not see block please help me..
D6
D6
Administer > Blocks > Configure > Show if the following PHP code returns TRUE
Add block for specific content type:
Show block only on user profile
That's awesome tinny.
That's awesome tinny. Thanks.
What about if one wanted it to appear only on one specific page like 'user' but not appear on 'user/2'
Hi, Here is the one you are
Hi, Here is the one you are looking for :
Hope this helps
Regards
Sagar
Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form
Cheers Sagar. In the end I
Cheers Sagar. In the end I got it working by simply changing the first arg to being greater than 1.
Taxonomy and Panels
Is there any code that will identify panel types? or custome pages. The easiest way would be for taxonomy but the codes I have tried just ignore the taxonomy altogether :(
TIA
Andrew
Excellent page
This is very helpful, thank you for posting it.
I was able to quickly display a block for a particular content type, and focus my time on other elements of the site.
Thanks for the post.
Display Block till a certain URL Depth
Hi all,
My URL hierarchy is like 1/2/3/4/5/6
I have a block which I want to show only for 1/2/3 and 1/2/3/*
but not for 1/2/3/*/* or 1/2/3/*/*/*.
I tried the following using the option
Show on only the listed pages
1/2/3
1/2/3/*
This does not work. It shows for all 4 types, 1/2/3, 1/2/3/*, 1/2/3/*/* and 1/2/3/*/*/*.
Show on only the listed pages
1/2/3
1/2/3/*/
This also does not work. This shows only for 1/2/3 only.
Can you please help me out?
Thanks,
Mrigank.
Solution at
Solution at http://drupal.org/node/1138908#comment-4394628
PHP Visibility for Home Page and Content Type
Can anyone tell me how to amend the code below so that the block also appears on
Hi, You need to add this
Hi,
You need to add this code under page specific visiblity settings and select "Show if the following PHP code returns TRUE (PHP-mode, experts only)" and hit save block.Hope this helps.
Regards
Sagar
Acquia certified Developer, Back end and Front specialist
Need help? Please use my contact form
Block visible to selected User Role but not to User 1
Hi
I am having a weird problem with Block Visibility. I enabled the Who's Online block for just one User Role. It shows correctly when someone in that role logs in but does not show when I am logged in as User 1.
Any ideas?
display block on preview page only
I was wondering if it was possible at all to display a block on the preview page only. I have tried using this:
if ($node->in_preview == TRUE)
However, it's not working.
Would there be a solution with the "form_state()" function? Since I am not a programmer, any advice would be greatly appreciated :)
Can someone please tell me
Can someone please tell me how to combine the snippet 'Don't show block set of roles' with 'Combining PHP visibility control with specific page visibility'?
I've done this, but nothing shows up: