检查node-type.tpl.php中的角色

时间:2009-11-12 18:41:23

标签: drupal

有没有办法在drupal主题文件中检查查看器的角色,以便编写条件语句?

...谢谢

3 个答案:

答案 0 :(得分:3)

当前用户始终可用作全局变量,所以只需执行:

// Make the user object available
global $user;
// Grab the user roles
$roles = $user->roles;

$ user->角色将是一个角色名称数组,由角色ID(rid)键入。


编辑:准确地说,全局用户对象在早期引导期间在阶段DRUPAL_BOOTSTRAP_SESSION中可用,但是从主题或模块中的自定义编码开始,您可以将该全局视为始终可用。< / p>

答案 1 :(得分:1)

这样做

 
  global $user;
  $num_roles = db_fetch_object(pager_query(db_rewrite_sql('SELECT rid from {role} ORDER BY rid Desc')))->rid;  // Find how many roles are there
  for($i=0; $i < $num_roles; $i++){
      if(strlen($user->roles[$i]) >0){
        echo $user->roles[$i];
        $i = $num_roles;
      }
  }
  

答案 2 :(得分:0)

Henrik Opel答案的附录: 如果你在tpl.php文件中使用它,那么首先在preprocess_node函数中创建一个变量:

<?php
function YOURTEMPLATE_preprocess_node(&$variables) {
  global $user;
  $variables['current_user_roles'] = $user->roles;
}
?>

现在您可以在tpl.php中打印您的角色:

<?php
if ($current_user_roles) {
?>
<ul class="roles">
<?php
  foreach ($current_user_roles as $role) {
    ?><li class="roles-item"><?php print $role; ?></li><?php
  }
?>
</ul>
相关问题