如何在drupal中以编程方式访问模块?

时间:2011-10-14 13:34:30

标签: php drupal drupal-6 module drupal-modules

基于某些条件,我想提供对模块的访问权。

if(abc == abc) {
  //give access to module xyz.
}

1 个答案:

答案 0 :(得分:2)

没有像在Drupal中访问整个模块那样的概念,只有模块可以定义的页面。通常,这是通过实施hook_menu()来定义页面,然后提供access callbackaccess arguments来完成的。

第一个定义了一个函数,该函数将被调用以进行访问决策:

function mymodule_menu() {
  $items['some/path'] = array(
    'title' => 'Some Title',
    'page callback' => 'mymodule_callback',
    'access callback' => 'mymodule_some_path_access'
  );

  return $items;
}

function mymodule_some_path_access() {
  global $user;

  if ($user->foo == 'bar') {
    // Access allowed, return TRUE
    return TRUE;
  }

  // Access not allowed, return FALSE
  return FALSE;
}

第二个定义将传递给user_access函数的参数。这通常基于您的模块提供的权限:

function mymodule_menu() {
  $items['some/path'] = array(
    'title' => 'Some Title',
    'page callback' => 'mymodule_callback',
    'access arguments' => array('access mymodule')
  );

  return $items;
}

function mymodule_perm() {
  return array(
    'access mymodule'
  );
}

在第二个示例中,除非用户具有“访问mymodule”权限(如Drupal的权限管理区域中所定义),否则将拒绝用户访问。

希望有所帮助

相关问题