1

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 1

0

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;
   }
 }
5
  • 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
    – JOKER
    Commented Aug 4, 2015 at 12:04
  • Please try to change "views-field-title" to "title"
    – Andrew
    Commented Aug 4, 2015 at 12:21
  • 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?
    – JOKER
    Commented 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.
    – Andrew
    Commented 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.
    – JOKER
    Commented Aug 4, 2015 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.