简体   繁体   English

如果用户没有特定权限,如何限制显示Drupal块?

[英]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'); 通过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. 对于块管理,请使用PHP代码以提高可见性。

Nikit is right, a code example would be: Nikit是正确的,代码示例为:

<?php

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

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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