I am using CCK, Views and Views Attach to create a parent <-- children relation.

On the parent page I have a view with Title, Body of any child.

I am trying to add a ‘Edit’ link beside the Title of any given children (easily done adding a Edit link field which “Provides a simple link to edit the node.”).

My proble is that I want to have this link visible ONLY for certain roles (editors and so).

How could this be achieved? Adding an if… then… clause to the .tpl.php file is the best approach…?

Thanks for any advice!

Comments

tomgf’s picture

I found an easy way to do it.

In template.php:

function mytheme_preprocess_views_view_fields(&$vars) {
	global $user;
	if (!in_array('editor',$user->roles)) {
		unset($vars['view']->field['edit_node']);
	}
}

The only problem is that this is working from the second value on… somehow the first value on the array is untouched!

webel’s picture

interested also in strategies for only showing field in view to selected roles.

> The only problem is that this is working from the second value on… somehow the first value on the array is untouched!
Did you figure out why and solve this ?

Webel IT Australia, "Elements of the Web", Scientific IT Consultancy,
For PHP-driven Drupal CMS web sites, Enterprise Java, graphical UML, UML Parsing Analysis, SysML, XML.

bregtemundo’s picture

I tried unset($vars['fields']['field_id']); instead and it seemed to work for the first value too.

giorgosk’s picture

in drupal7 preprocess function is called but it does not unset the fields

function THEMENAME_preprocess_views_view(&$vars) {
  global $user;
  if(!in_array('authenticated user',$user->roles)){
    unset($vars["view"]->field["field_to_hide"]);
  }
}

anyone knows why ?

and $vars['fields'] as mentioned above is not passed to the preprocess

EDIT
creating a module and hooking into views_pre_render works nice though

function MODULENAME_views_pre_render(&$view) {
  global $user;
  if (!in_array('authenticated user',$user->roles)) {
    unset($view->field['field_to_hide']);
  }  
}

------
GiorgosK
Web Development

mndonx’s picture

Hmm - I'd edit the view and take the template suggestion for that field (under Theme:Information) and try adding the logic there. The tpl will end up being something like: views-view-field--edit_node.tpl.php. You can copy and paste the default code, then edit it with your if statement around it.

I'm not sure if that will solve the problem or not -- just a different way of doing the same thing, I think.

Amanda

webel’s picture

Webel IT Australia, "Elements of the Web", Scientific IT Consultancy,
For PHP-driven Drupal CMS web sites, Enterprise Java, graphical UML, UML Parsing Analysis, SysML, XML.

bolshii’s picture

I found the 'user role field' module. Unfortunately for 7 only.

How do I show a specific views field only for admins in Drupal 8?