dtt5830659 2013-01-14 04:45
浏览 30
已采纳

仅当个人资料所有者不属于角色X时才显示内容(在Drupal 7中)

I have a Drupal 7 site, and I would like to print out inside the profile page some field content, according to the role of the profile user (not the logged in user). I need to show a specific field only if the profile owner does not have role autor:

<?php
  if (!in_array('autor', array_values($user->roles))) {
print drupal_render(field_view_field('profile2', $profile['main'], 'field_nombrecompleto1', 'value'));
  }
  else {print "yada yada";}

Note: The profile field in question, is a rofile2 field and not a core profile one.

What´s wrong with that code? Because it will print out the field in question in any case, regardless of the role of the profile owner.

I´ve tried this other code, and in this case the field won´t print out in any case:

<?php
if (is_array($user->roles) &&
    in_array('authenticated user', $user->roles) &&
    !in_array('autor', $user->roles)) {
    print drupal_render(field_view_field('profile2', $profile['main'], 'field_nombrecompleto1', 'value'));
}
?>
  • 写回答

1条回答 默认 最新

  • dongliantong3229 2013-01-21 21:44
    关注

    Here is how this is accomplished in Drupal 7 when modifying the form for editing your profile:

    function MYMODULE_form_profile2_edit_main_form_alter(&$form, $form_state) {
      global $user;
      $roles = $user->roles;
      if(!in_array('authenticated user', $roles)) {
        $form['profile_main']['MY_FIELD_ID']['#access'] = FALSE;
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错