SteD
SteD

Reputation: 14027

Drupal show / hide fields in view

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.

Upvotes: 3

Views: 11953

Answers (5)

Seb datarazor
Seb datarazor

Reputation: 21

Click advanced, theeming, find the field and make a _.tpl.php file for it, then in the file you will see:

print $output;

Change this to:

if (user_access('administer nodes')) {
    print $output;
}

Or whatever the permission is you are checking against.

Upvotes: 2

Rishi
Rishi

Reputation: 41

I think you have to try module Field Permissions

Upvotes: 1

Johnathan Elmore
Johnathan Elmore

Reputation: 2244

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";
}
?>

Upvotes: 2

Chaulky
Chaulky

Reputation: 699

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.

Upvotes: 0

Jukebox
Jukebox

Reputation: 1603

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.

Upvotes: 6

Related Questions