适用于多种内容类型的挂钩烫发
Drupal 6.x
我有这个模块来管理四种不同的内容类型。就此而言,如何定义同一模块中每个内容的权限?这可能吗?我不知道如何定义每个内容类型的权限,因为 hook_perm 必须用模块名称命名,并且它没有任何参数(如 hook_access $node)来返回基于内容类型的权限。这就是我想做的 -
function mymodule_perm()
{
if(content1)
return array(
'create content1 node',
'edit content1 nodes',
'delete content1 nodes',
);
if(content2)
return array(
'create content2 node',
'edit content2 nodes',
'delete content2 nodes',
);
if(content3)
return array(
'create content3 node',
'edit content3 nodes',
'delete content3 nodes',
);
.......
}
任何帮助将不胜感激。
Drupal 6.x
I have this module that manages four different content types. For that matter, how do I define permission for each content within the same module? Is that even possible? I can't figure out how to define permission for each content type cuz hook_perm has to be named with module name and it doesn't have any argument(like hook_access $node) to return permission base on content type. Here's how I'd like to do -
function mymodule_perm()
{
if(content1)
return array(
'create content1 node',
'edit content1 nodes',
'delete content1 nodes',
);
if(content2)
return array(
'create content2 node',
'edit content2 nodes',
'delete content2 nodes',
);
if(content3)
return array(
'create content3 node',
'edit content3 nodes',
'delete content3 nodes',
);
.......
}
Any help would be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您不需要自己创建内容类型的权限,因为节点模块在
node_perm()
。对于您在hook_node_info()
中声明的每种内容类型,节点模块将自动创建一组固定的权限,如下所示:除此之外,您可以在模块
hook_perm()
实现中声明任意数量的附加权限(只要它们是唯一的)并使用那些在你的代码中,如你所愿。这里重要的是,权限本身并没有太多作用 - 它只是一个将显示在权限页面上的名称,允许将其归因于角色。它们只有通过
user_access()
调用。因此,例如,如果您想自己为每种内容类型创建特殊的新权限,您只需在
hook_perm()
中一次性声明它们(因此您不需要任何参数- 只需为您想要创建的每个权限返回一个字符串)。Normally you do not need to create permissions for content types yourself, as the node module does this for you in
node_perm()
. For every content type you declare inhook_node_info()
, the node module will automatically create a fixed set of permissions as follows:Besides that, you can declare any number of additional permissions in your modules
hook_perm()
implementation (as long as they are unique) and use those in your code as you wish.The important thing here is that a permission does not do much by itself - it is just a name that will show up on the permissions page, allowing it to be attributed to roles. They only become 'meaningful' by code that uses them via
user_access()
calls.So if, for example, you wanted to create a special, new permission for each of your content types yourself, you would just declare them in
hook_perm()
all at once (so you do not need any argument - just return one string per permission you'd like to create).一般来说,实现多个内容类型的模块将从
hook_perm()
返回它定义的所有权限;没有办法知道 Drupal 正在请求实施的权限的内容类型。Drupal总是向模块询问所有已实现权限的列表,这甚至与节点无关;例如,有些模块仅实现其设置页面的权限。
Generally speaking, a module implementing more than one content type will return all the permissions it defines from
hook_perm()
; there isn't a way to know for which content type Drupal is asking the implemented permissions.Drupal always asks to the modules the list of all the implemented permissions, which could not even be related to nodes; there are some modules that implement only permissions for their setting pages, for example.