We have "access callback" and "access argument" for defining the access to pages.
What is the way to restrict showing blocks? Can "access callback" also be defined in hook_block_info()
, or through any other way?
The array returned from the implementations of hook_block_info()
doesn't contain a property equivalent to the access callback for the menus. If you have a module that implements a block, then you can implement hook_block_view() using code that checks if the user has a specific permission. The following code is used from a Drupal module; as you see the block is "rendered" only when the user has the "access content" permission.
case 'recent':
if (user_access('access content')) {
$block['subject'] = t('Recent content');
if ($nodes = node_get_recent(variable_get('node_recent_block_count', 10))) {
$block['content'] = array(
'#theme' => 'node_recent_block',
'#nodes' => $nodes,
);
}
else {
$block['content'] = t('No content available.');
}
}
break;
You can use similar code, but checking a different permission, even a custom one your module uses for checking if the user can see the block.
You can also control access to other blocks by implementing hook_block_list_alter(), just remove the blocks which you don't want to be displayed on the current page from the array.
You can add a PHP snippet in the block configuration, and check whether the current user has enough permissions to see the block.