Reputation: 814
I have a rather basic question but I can not wrap my mind around how to solve it "nice". I have a node type that contains calendar info, I use views to get the month-view rendered nice and all is good so far. Next thing was to make the calendar view also contain nodes of type "group_xyz" but only display these events when a user is logged in I managed to do that with filtering using a couple of lines PHP to get user id and node type, but already here it didnt feel like a real drupalish solution...
To my problem, we would like to limit the display of calendar posts depending on the user role and a taxonomy term used in the node.
So the node has a taxonomy term reference "visibility_to" lets say one of (All, Internal, Administrators) set. The user can be either guest, logged in user or administrator.
Now I would like to find a nice way to filter like this: Display the item IF node-type is calendar AND ( (visibility_to == "All") OR ( (visibility_to == "Internal") AND (role=="administrator" OR role == "logged_in") OR (visisbility_to == "Administrators" AND role =="administrator"))
I think you get the point... I think I could manage to hard code this in PHP but the day when we add new roles, new taxonomy terms I would rather have all this configured using the admin interface...
Any help and/or suggestions is appreciated. Also posted in drupal forums at: http://drupal.org/node/1879238
Upvotes: 2
Views: 3447
Reputation: 2015
OK, here's how I'd do it in a 'Drupalish' way:
Finally, add a contextual filter that will compare the role in the 'visibility' term to the current user's role. Only after you've added the above relationship, will the field 'Visibility role' pop-up in the list of available contextual filters. Select that as your contextual filter. Choose 'Provide default value' and 'PHP code' as your options. Type in the following php code to compare the roles associated with the node's term with the role of the current user:
global $user;
$current_roles = "";
foreach ($user->roles as $key => $val) {
$current_roles .= $key;
if ($val != end($user->roles)){ // If not last item, add a '+' which treats these as an or
$current_roles .= '+';
}
}
return($current_roles);
* Finally, make sure you expand the 'More' option under the contextual filter and select to allow multiple values for this filter.
Try it out and let us know if this works for you!! I tested it out and it seems to work for me.
Upvotes: 4