Block Visibility Conditions in Drupal


While dealing with “Block” in Drupal, you have many choice regarding its display. Like, in simple words where to display,for which conditions,for which users,for which roles.etc..  So, in this post you will learn about such useful conditions.We called it as “Block Visibility Conditions”.

General Way of providing block permissions


If your block was created from a view (if not then you can create one) ,then the access settings can be set in the view edit mode:


Now you can simply select the permissions to access the block.Users having the selected permission will be able to access this display.

Click on the little 'cog-wheel' to the right of the 'Access' row under the view's basic settings in order to select the restriction you desire: 



Attention :  Please see the comment below the 'Default Access Options' list as shown in the above image.

If you want to remove 'access all views' for the anonymous user,you can do it from  '/admin/user/permissions' and then set the permissions of your other views also. This gives more control over who sees what. Views by default have 'Access' set to 'Unrestricted' generally.

Display the block only to logged-in users.



<?php
global $user;
if ($user->uid) {
    return true;
} else {
    return false;
}
?>

Showing a block only to certain Users.



<?php
global $user;
if ($user->uid == 31) {
    return "This block is only visible for the user with the user-ID 31.";
} else {
    return;
}
?>

If you want to allow more than one User



<?php
global $user;
if ($user->uid == 11 || $user->uid == 21 || $user->uid == 31) {
    return "This block is only visible for the users having user-ID 11,21,31.";
} else {
    return;
}
?>

Show block on specific node type.


For this case, there are two codes you can use any of them.

Code :1

Here, just replace “Example” with your node type.


<?php
$nodeType = " Example";
if (arg(0) == 'node' AND is_numeric(arg(1)) AND arg(2) == FALSE) 
{
    $node = node_load(arg(1)); 
    if ( ($node->type == $nodeType) OR ($node->nid == 1) ) 
{
      return true;    
}
}
return FALSE;
?>

Code : 2


<?php  
// Only show if $return is true  
$return = FALSE;  
// Matching current node type with Example  
if ((arg(0) == 'node') AND is_numeric(arg(1)))  
{  
  $nid  = arg(1);  
  $node  = node_load($nid);   
  $type  = $node->type;   
    if($type == " Example")
     {  
      $return = TRUE;  
     }  
}  
return $return;  
?>  

Showing to specific roles only(Pre-Defined) in Drupal



<?php
global $user;
$output = '';
if (in_array('admin user',$user->roles)) {
  return true; 
}
else
{
return false;
}
?>

Showing block only when the Admin is logged in



<?php
global $user;
if (($user->uid) == 1) {
return true;
} else {
return false;
}
?>

Showing for a certain content type and node ID:



<?php
// Only show block from types array and nodes array
$match = FALSE;
// Which node types
$types = array('page' => 1);
// Which nodes (by nid)
$nodes = array(1, 2);
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid  =  arg(1); 
  $node  =  node_load(array('nid' => $nid));
  $type  =  $node->type;
  if (isset($types[$type]) && in_array($nid, $nodes)) {
        $match = TRUE;
  }
}
return $match;
?>

Show block only on User profile



<?php
  // dont show on edit pages
  $args = arg();
    if (count($args) > 2) {
      return FALSE;
    }
    // Visible only on  yoursite/user/uid where uid is the user id
  if ($args[0] == 'user') {
    return TRUE;
  }
?>

Display block for all conditions  like node/(anything) but not for node/25 and node/28 (specific)



<?php
/* you can define a list of pages to include one per line */
$include = "node/*";

/* you can define a list of pages to exclude one per line */
$exclude = "node/25
node/28";


/* ----------- DON'T  MAKE ANY CHANGES IN THE BELOW CODE ----------- */
$match = FALSE;
$path = drupal_get_path_alias($_GET['q']);
$regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($include, '/')) .')$/';
if (preg_match($regexp, $path)) {
  $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($exclude, '/')) .')$/';
  if (!preg_match($regexp, $path)) {
    $match = TRUE;
  }
}
return $match;
?>

Showing block for certain  node ID’s



<?php
$match = FALSE;
//Matching  which nodes (using nid)
$nodes = array(9, 28);
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load(array('nid' => $nid));
    if ( in_array($nid, $nodes)) {
        $match = TRUE;
  }
}
return $match;
?>


Comments

Popular posts from this blog

How to show only month and year fields in android Date-picker?

How to construct a B+ tree with example

Conflict Serializability in database