1
votes

I've created a custom event content type in Drupal 7 with user fields of teacher and student. I would like to create a view in which the content is displayed only for the users who are either teachers or students in that content - a custom calendar.

So far, I've tried to create a filter on Content: Teacher (field_teacher), using user:uid and user. I know it's a syntax problem, but I need the value to be currentlyLoggedinUser

4

4 Answers

1
votes

To get the currently logged in user uid, follow this code snippet.

global $user;
$uid = $user->uid;

Hope this works... Muhammad.

0
votes

Try adding a Relationship To the field Content: Teacher (field_teacher) first and then check the filter options again to see if the one you need is now available.

0
votes

Why don't you just create two different displays and use the Access views option to filter based on the user's role?

Putting that suggestion aside: You shouldn't be using two fields to distinguish between a teacher and a student for your content type. Use a single field select list. Values would be 0 for student, 1 for teacher, as an example.

Then add that field as a Contextual Filter, select Provide default value and use PHP Code as the type. Use the following code to determine the role of the user and return the appropriate value:

global $user;

if(in_array('student', array_values($user->roles))) {
  return 0;
} else {
  return 1;
}

I would recommend my first suggestion though and make use of the access based on roles.

0
votes

It was actually a pretty simple solution - I created contextual filters on the two entity references (student and teacher), then in the section "If no default provided", I added "use currently logged in user".

I then had to write a custom module with a hook_query_alter to change the contextual filter's AND to an OR