1

How do I the user ID of the account profile that an administrator user is currently viewing/editin? I can't use \Drupal::currentUser() as it always returns the administrator details.

I tried \Drupal::routeMatch()->getParameter('node') and \Drupal::request()->attributes->get('node'), but it returns this error:

Call to a member function id() on null...(file path)

  • 1
    Welcome to Drupal Answers! Notice that getParameter('node') would eventually give you the node being viewed, not the user for which you are seeing the user page (e.g. example.com/user/10). – kiamlaluno Feb 12 '16 at 4:29
3

Assuming that you're viewing a page at a path like /user/42, then you can use the RouteMatch class. Your example is close, but you need to change the parameter passed to the getParameter() method. The parameter in this case is named 'user' not 'node'. You can determine the parameter name from the route definition. Not all routes have parameters, but when they do, they are specific to the route in question.

In this case the route (entity.user.canonical) is defined by \Drupal\user\Entity\UserRouteProvider::getRoutes. And the 'user' parameter is the UID of the user entity in question. Given that, you can use RouteMatch to get the ID in one of two ways.

Load the $user object specified by the user parameter. Which is probably more common. I'm guessing if you're trying to get the UID it's so you can load the user object. With this method parameters are processed by the parameter conversion system, which does operations such as converting entity ID parameters to fully-loaded entities

$user = \Drupal::routeMatch()->getParameter('user');
$uid = $user->id();

Or access the parameter value directly:

$uid = \Drupal::routeMatch()->getRawParameter('user');

This change record provides a bit more information https://www.drupal.org/node/2295317

0

If you are talking about in the admin section, as in /user/42 - the user ID is the last fragment of the URL - take a look at drupal_parse_url to break your URL into components.

Once you know what the user id is (42 in the case above), you can use user_load function to load the profile of user 42

  • The question is for Drupal 8. (See the used tags.) – kiamlaluno Feb 12 '16 at 5:10
  • Oops. sorry. my bad. – JP Dippenaar Feb 12 '16 at 11:38
0

Replying to @eojthebrave (but I do not yet have enough reputation to comment directly, hence this new comment).

Good answer, helpful, thank you. Exactly what I was looking for.

Just for info to share with others, I discovered that \Drupal::routeMatch()->getParameter('user') does not always return a user object. When viewing the main 'view' tab on the user profile it does return an object:

$path => /user/1346
$route_name => entity.user.canonical
$user => object
$uid => 1346

But if on a contrib-supplied tab, for example provided by a view, you get:

$path => /user/1346/scheduled
$route_name => view.scheduler_scheduled_content.user_page
$user => numeric
$uid => 1346

In these cases you want to avoid $uid = $user->id(); which will cause an error. The good news is that in both cases \Drupal::routeMatch()->getRawParameter('user') returns the uid as an integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.