What I really want is that I have a block and I want it to be shown only for authenticated users.
Can anyone help me?
Note: I want to do it programmatically without an extra module.
What I really want is that I have a block and I want it to be shown only for authenticated users.
Can anyone help me?
Note: I want to do it programmatically without an extra module.
global $user;
check $user->roles
and unset
your block:
function modulename_block_list_alter(&$blocks){
global $user;
$roles = $user->roles;
if(!in_array('authenticated user', $roles)){
unset($blocks[6]);
}
// 6 is actually a block id witch is
//an array key from $blocks
}
I have just tested it with simpletest and it works. You need to check the user roles also.
there is the option of assigning a role in the block settings under the Visibility settings
.
if($user == admin) { // body content }
the body content only show if user admin.
Commented
Oct 1, 2015 at 12:40
If you looking it through grammatically so follow this.
function hook_block_view($delta = '') {
global $user;
$block = array();
switch ($delta) {
case 'demo_block1':
if (in_array('Admin', array_values($user->roles))) {
$block['content'] = t('Hello Admin');
}
break;
case 'demo_block2':
if ($user->uid) {
$block['content'] = t('Hello Authenticated User');
}
break;
}
return $block;
}