简体   繁体   中英

How to restrict a Drupal block from being displayed if a user does not have a particular permission?

I know that I can use the Show block for specific roles setting to manually configure whether a block is displayed to users.

I have a module that defines custom blocks. Rather than relying on the administrator to restrict the block visibility based on roles , can my module limit its blocks from being displayed unless a user has a particular permission ?

Check access by user_access('Some access name');
For your module just return empty value, and block will not appear for that user.
For block admining, use php code for visibility.

Nikit is right, a code example would be:

<?php

$block = array();
if (user_access('my custom permission')) {
  $block['content'] = t('Here is a message');
}
return $block;

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM