views:

500

answers:

3

I would like to show / hide certain fields in my Drupal view accordingly to the user role.

Provided I can only have this view to work with, how can I achieve this programmatically or there's some settings that I am not aware of in Drupal.

P/S: I am aware of the access settings under basic settings in View but that would restrict access to the whole view, not field level.

A: 

You can create two identical Displays (within the same view) and override the field settings and access settings in each of them. For example, in the first display show the fields you only want a certain role to see, and set the access control setting to that role. In the second display, remove the unwanted fields and set the access control to the corresponding role.

Start by creating the most restrictive display first and then the least restrictive one.

Jukebox
A: 

If your the fields you want to exclude are 1) created with CCK and 2) should be hidden from users of that role everywhere on the site (not just in this particular view) then you can just set the permissions on the fields so that users are particular roles can't view them. If the current user doesn't have permissions to view a field that is part of a View, the field won't be shown to the user.

Chaulky
A: 

I liked this answer, but in my case the field is dependent on the argument and I would need to create a new display for each argument (which isn't practical).

I installed the Views Custom Field module and used this code for the field:

<?php
if(user_access("some permission string here"))
{
  print "Your field value here";
}
?>