At BADCamp last week, I participated in the two lab sessions to upgrade a module to Drupal 8. The module I was working on was Mixpanel.
There are three places in the the D7 module which use global $user;
to get the current user. I ported that to \Drupal::currentUser()
in D8, which is documented as returning AccountInterface - but I actually need UserInterface so I can call ->getLastLoginTime()
and ->getCreatedTime()
.
Right now, my only option is copying the following magic incantation to three places in my module:
$user = \Drupal::currentUser();
if (!$user instanceof UserInterface) {
$user = user_load($user->id());
}
This is something of a developer experience WTF for me. It'd be great to have an API for this, maybe even a new method on AccountInterface
, so that instead, module developers can do:
$user = \Drupal::currentUser()->getFullUser();
What do you think?
Comments
Comment #1
dsnopekAdding tags.
Comment #7
jhedstromComment #10
colanComment #11
sharma.amitt16 CreditAttribution: sharma.amitt16 as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedComment #12
sharma.amitt16 CreditAttribution: sharma.amitt16 as a volunteer and at Srijan | A Material+ Company for Drupal India Association commentedComment #18
m.stentaDiscovered this today when trying to use
$this->getCurrentUser()
inside a controller class, and passing that intouser_pass_reset_url()
, which callsuser_pass_rehash()
, which requires aUserInterface
parameter.@dsnopek's "magic incantation" works... but definitely a "developer experience WTF". :-(