First of all, sorry for my bad english.

I'm using Drupal 7

Here is my problem :

I have a views in wich I would like to display a list of custom content type only for the users that I selected with a custom field "entity ID" in this custom content type. This users share the same role, but they can't see the node of others users (of the same role or another role as well).

I taught the simplest method would be to add some PHP code thanks to the PHP views module, but if you have a better solution I'll love to hear that.

So this is what I made so far:

I added a global PHP in the filters criteria section of the view with this code:

global $user;
$current_user = $user->uid;
$destiny_user = $data->field_field_uttilisateur[0][raw][entity]->uid;

if ($current_user == $destiny_user) {
   return false;
} else {
   return true;

Basicaly I'm comparing the current user ID with the ID I filled in the content type. If this "function" returns true, then the current row is not displayed.

I don't have to worry to know if the user is authentified because I already do that in Page settings (Access)

But it seems like $destiny_user is always empty. I don't get it because when I add an global php field in the fields section all seems ok (I can see the ID of the user I whant to display the row)

I'm using this method $data->field_field_uttilisateur[0][raw][entity]->uid; because the $rows->field_uttilisateur; is not working. (It display the nid Oo) After some research I found this solution that seems to work... at least more than the other one.

I'm having a really bad time with this, do you have some ideas how I can get this to work?

Thank you in advance !

1 Answer 1


This can be achieved using contextual filters in views. Here are the steps:

  1. In the views edit page click on Advanced section.

  2. Click on the Add button corresponding to "Contextual Filters"

  3. Search for your custom field in your custom content type and add it.

  4. In the configure contextual filter which appears make the following changes. Under "When the filter value is not in the url" section, select the option "Provide default value". In the type select list, select the option "User ID from logged in user". enter image description here

  5. Save the changes.

  • Yes, contextual filters will work but he seems to want to hide the node outside the view also, He said "This users share the same role, but they can't see the node of others users". So I think rather than using contexual filter, he should use hook_node_access() to deny the access.
    – msmani
    Commented Jun 18, 2014 at 12:14
  • 1
    From what I understand the requirement is to filter the content in the view based on the user id. I also agree with you that he might need to control access outside the views for which he needs to implement hook_node_access. From the reference here drupal.org/node/1266388 Views does not respect node access. So contextual filters are still required for taking care of the view in this case.
    – Bhuvana
    Commented Jun 18, 2014 at 12:42
  • Yes, I agree and I have removed my answer.
    – msmani
    Commented Jun 18, 2014 at 12:58
  • Ok this is working perfectly thank you Bhuvana, and wolverine. In deed, I'll try to implement hook_node_access. But for the moment I have another little question concerning views, can I exclude of the filter context a value ? If, for example in my custom field I set no user (for the "news") section which is the same content type... Maybe I have to do another content type ?
    – Russo
    Commented Jun 18, 2014 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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