2017-08-23 10:12
浏览 155

如何在Drupal 8中获取当前用户对象?

I am proficient in Drupal 7 where I get the current user object from global $user, but how do I get it in Drupal 8 ?

图片转代码服务由CSDN问答提供 功能建议

我精通Drupal 7,从 global $ user 获取当前用户对象 ,但我怎么在Drupal 8中得到它?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongren4099 2017-08-23 10:15

    Please use following code to get the current user object in Drupal 8: $user = \Drupal::currentUser();

    打赏 评论
  • doujia1988 2017-08-24 06:40

    $user = \Drupal::currentUser();

    To get the current userID you can use :

    $user = User::load(\Drupal::currentUser()->id());

    打赏 评论
  • duancan5327 2019-07-14 10:08

    One liner way to get a full drupal user object for the current active user :

    $account = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());

    Why not just Drupal::currentUser() ? Because most of the time it won't be sufficient as you'll need a fully loaded user object while it's not :

    打赏 评论

相关推荐 更多相似问题