I have a view showing the nodes of a content type, and I want to show the user ID of the currently logged-in user as a field in view, but I can't find how to add the user ID of the currently logged-in user. I know that I can achieve this with the Views PHP module but I want to know if there is another way.

I have Views 3.11.

There is no relation between the content being shown, and the currently logged-in user; I just want to show the user ID of the user who is currently seeing the view.


You can't without some custom module providing such a data field (such as views php as you've seen).

Unless the uid has some relations to the content type you're displaying (e.g. a user reference field, the node uid, etc.), there's no way to retrieve the current UID. Views is designed to pull in data from a base table (e.g. nodes, users, taxonomy, etc.) and display the fields directly attached to it or related to it through a relationship.

| improve this answer | |
  • thanks Shawn, I think that the sareed comment drupal.stackexchange.com/users/30335/sareed is a way too. But the uid of the logged user I think that is a global var, I can't understand why this is not possible – Adrian Cid Almaguer Sep 11 '15 at 2:14
  • It is possible, just not with Views alone. Maybe there is a module that creates view fields from contextual filter values, I'm not sure. – Shawn Conn Sep 11 '15 at 20:50
  • This is a good idea, I will search about this – Adrian Cid Almaguer Sep 12 '15 at 0:11
  • 1
    There is an alternative to Views PHP. All you have to do is override the view template file, which allows you to use php there. I can provide a detailed answer if you are still interested. – No Sssweat Dec 6 '15 at 13:46
  • @NoSssweat I know that way, but if you want add other answer for other users go ahead. – Adrian Cid Almaguer Dec 6 '15 at 15:52

You can do this with the help of relationships in views by choosing content:author. This will give you uid get_uid.

Other way of achieving this is by using views php filter. Views php field with code should do the job:

  global $user; 
  print $user->uid; 
| improve this answer | |
  • both the options from your answer don't give solution related to question. 1) In your first option uid will be visible only if logged in user has created some content. If user has not created any content then it's uid will not be shown in views. 2) In your 2nd option you suggest views php but in question this is clearly mentioned that OP wants an other functionality except of views php. – WaQaR Ali Sep 11 '15 at 7:14
  • I know the way with views_php actually this is the code that I have. But if I can't use views_php is better for me – Adrian Cid Almaguer Sep 11 '15 at 11:56

The uid of the current user as a field in view would be the same for each row of your view results, so I am presuming some other outcome than merely displaying this field.

My guess would be to use it as a rewrite rule to create a link that has the user id in it (I'm struggling to come up with another scenario - perhaps specify?)

For that use-case, I would use custom php field - it's the simplest. It does sound like you are looking for a contextual filter, rather. Set the default value to be the uid of the logged in user.

| improve this answer | |
  • I need this value to make a comparison with views_conditional – Adrian Cid Almaguer Sep 11 '15 at 11:59

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.