【问题标题】:How to set permission in custom drupal module in Drupal7如何在 Drupal7 的自定义 drupal 模块中设置权限
【发布时间】:2014-05-02 11:11:43
【问题描述】:

我创建了一个自定义 Drupal7 表单模块,但我只想向经过身份验证的用户显示该表单。我想同样创建模块,它可以在people->permission 部分中有复选框选项,我可以在其中为所有类型的用户设置此模块的权限。这是menu_hook

function form_example_menu() {
  $items = array();
  $items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Example Form', //page title
    'description' => 'A form to mess around with.',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_example_form'), //put the name of the form here
    'access arguments' => array('access administration pages'),
    'access callback' => TRUE
  );
  return $items;
}

我是 drupal 的新手,所以任何有关这方面的帮助都会很明显。如果有人可以写下 hook_permission 而不是给出示例,那将是一个很大的帮助。

【问题讨论】:

  • 这个应该被移到 Drupal stackexchange

标签: drupal drupal-7 drupal-modules


【解决方案1】:

这里是hook_permission的实现

/**
 * Implements hook_permission().
 */
function form_example_permission() {
  return array(
    'administer your module' => array(
      'title' => t('Administer permission for your module'),
      'description' => t('Some description that would appear on the permission page..'),
    ),
  );
}

并且你必须在hook_menu的实现中将返回数组的键(administer your module)给access arguments 因此,您的 hook_menu 实现将变为:

function form_example_menu() {
  $items = array();
  $items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
    'title' => 'Example Form', //page title
    'description' => 'A form to mess around with.',
    'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed.  for a form, use drupal_get_form
    'page arguments' => array('form_example_form'), //put the name of the form here
    'access arguments' => array('administer your module'),
  );
  return $items;
}

请注意,在您更改 hook_menu 中的任何内容后,您必须刷新缓存。您可以通过admin/config/development/performace/ 进行操作

【讨论】:

  • 你的代码工作正常和舞会权限部分我只检查了验证用户的权限,但表单的链接仍然显示给匿名用户
  • @user3170182 您必须从 'access callback' 实现中完全删除 'access callback' 键,因为这会授予所有人访问权限 (TRUE)。见编辑。记得再次刷新缓存。
【解决方案2】:

试试这个。添加后清除您的缓存并转到 people->permission 并从那里您可以为 users 设置权限。

function form_example_permission() {
  return array(
    'administer my module' => array(
      'title' => t('Administer my module'),
      'description' => t('Perform administration tasks for my module.'),
    ),
  );
}

【讨论】:

  • 但链接显示在匿名用户中,尽管我只向经过身份验证的用户授予权限
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
  • 1970-01-01
相关资源
最近更新 更多