如何在Drupal7中设置自定义drupal模块的权限

时间:2014-05-02 11:11:44

标签: drupal drupal-7 drupal-modules

我已经创建了一个自定义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而不是举例,那么这将是一个很大的帮助。

2 个答案:

答案 0 :(得分:3)

以下是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..'),
    ),
  );
}

您必须在administer your module的实现中将返回的数组的键(access arguments)提供给hook_menu 因此,您的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/

执行此操作

答案 1 :(得分:0)

试试这个。添加后清除缓存并转到people->权限,然后您可以为此设置权限。

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