I have a view with some fields and one of them is the title field. Now i need the feature that when an anonymous user views the view, the title is not linked to it´s content. When the user logs in, then the title should link to its content. How is it possible to realize that? I have more or less no php skills.
1 Answer
You can implement template_preprocess_views_view_field function in your module and rewrite output for this field for anonymous users.
/**
* Implements hook_preprocess_views_view_field().
*/
function mymodule_preprocess_views_view_field(&$vars) {
switch ($vars['view']->name) {
case 'my_view_name':
if ($vars['field']->options['id'] == 'my_field_name') {
if (user_is_anonymous()) {
$vars['output'] = strip_tags($vars['output']);
}
}
break;
}
}
-
i used your code and put it in the template.php file - is that possible too? changed "mymodule" to the name of my theme, also "my_view_name" to the name of my view and "my_field_name" to "views-field-title" but it seems it doesn´t work for Drupal7– JOKERCommented Aug 4, 2015 at 12:04
-
-
Didn´t work either, you wrote "options['id']" but the name is a class, does this matter? And to use the code in template.php is ok?– JOKERCommented Aug 4, 2015 at 12:31
-
It's okay to add this code in your theme. It should work. Can you debug if you use correct '$vars['view']->name' and '$vars['field']->options['id']'. You can use print_r or var_dump functions for these variables.– AndrewCommented Aug 4, 2015 at 12:50
-
working now!!! thanks a lot! The view name was correct but the field name wasn´t, i corrected it and now it works perfectly.– JOKERCommented Aug 4, 2015 at 13:15