4

How to do I inject Drupal\user\Entity\User class as dependency? I could not find service name in service list. for example:

$user = User::load($this->currentUser->id()) 

for this code I want to call like this

$user = $this->user->load(($this->currentUser->id()) 

when injecting in Controller. How can I achieve this?

0

1 Answer 1

8

You don't have to inject services in the controller, because ControllerBase already provides the services needed. So this should work:

$user = $this->entityTypeManager()->getStorage('user')->load($this->currentUser()->id()); 

For other services not included in ControllerBase, see How do I use dependency injection on \Drupal::moduleHandler() to a Controller?

2
  • is it same with Blocks? Commented Oct 23, 2018 at 8:23
  • No, this only works in a controller. Commented Oct 23, 2018 at 8:53

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.