如何创建自定义访问功能以在 Drupal 中编辑节点类型?
我有一个节点类型,只能由用户在超出其角色权限的某些情况下编辑。我正在自定义模块中执行此操作。
我想删除甚至查看编辑选项卡的功能,而不仅仅是向表单添加验证功能,以便在提交表单后提醒用户。
我需要添加某种访问功能。有人知道该怎么做吗?
提前致谢。
--更新--
我现在有两种可行的方法。
1)使用hook_nodeapi:
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch($op) {
case 'prepare':
if(!mymodule_access_function($node)) {
$_REQUEST['destination'] = 'my_access_denied_page';
// rest of function
2)我可以使用hook_menu_alter将访问回调函数插入到菜单项中。
就我的目的而言,2 更有意义。我想我会拼出 (1) 的代码,因为这是本页给出的答案并且它有效。
I have a node type that should only be edited by users under certain circumstances that go beyond the permissions their role has. I am doing this in a custom module.
I would like to remove the ability to even see the edit tab, and not just add a validation function to the form that will alert the user after the form is submitted.
I need to add some sort of access function. Anyone know how to do this?
Thanks in advance.
--Update--
I now have 2 ways that should work.
1) Using hook_nodeapi:
function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch($op) {
case 'prepare':
if(!mymodule_access_function($node)) {
$_REQUEST['destination'] = 'my_access_denied_page';
// rest of function
2) I can insert a access callback function into the menu item using hook_menu_alter.
For my purposes, 2 makes more sense. I thought I would spell out the code for (1) though since that was the answer given on this page and it works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于选项卡可见性,您可以在任何地方更改主题输出,从模块挂钩到主题模板或 CSS 补丁。根据数据可见性和性能问题的要求,某些解决方案比其他解决方案更好。我们需要有关您需要何种处理的更多详细信息。
要访问,hook_nodeapi() ,$op 是'prepare',此时针对 $node 运行您的自定义代码,并决定您想要执行的操作(例如如果不存在要求,则重定向到另一个表单,或者重定向到访问被拒绝的页面)。
编辑:重定向通常使用 $_REQUEST['destination'] = 'destination/alias' (不中断执行) 完成,有时 drupal_goto('destination/ alias') (中断执行) 是合适的,但通常不起作用。请在您的项目中跟踪重定向,因为在多个逻辑条件下,您可能最终会出现不需要的且难以调试的行为。
For the tabs visibility you can alter the themed output anywhere from a module hook to a theme template or css patch. Depending on the requirements for data visibility and performance issues some solutions are better than others. We need more details on what kind of processing you need.
For access, hook_nodeapi(), $op is 'prepare', run your custom code against $node at this point, and decide what you want to do (like redirect to another form if a requirement is not present, or to an access denied page).
Edit: Redirecting is usually done with $_REQUEST['destination'] = 'destination/alias' (does not break execution), sometimes drupal_goto('destination/alias') (breaks execution) is suitable but often it doesn't work. Please keep redirects tracked on your project, as with multiple logic conditions you may end up with unwanted and hard to debug behavior.
每个内容类型在
admin/user/permissions
中都有默认的权限设置,用于创建、编辑、删除节点。您可以分配给匿名或经过身份验证的用户。如果您想分配给组,请创建另一个角色并分配权限,如上所述。Every content type has default permission settings in
admin/user/permissions
for creating,editing ,deleting node . You may assign to anonymous or authenticated users. If you want assign to group then create another role and assign permission as mentioned above.