3

I have a View based on taxonomy vocabularies and terms. The view only contains 2 fields, taxonomy vocabulary and taxonomy term and the output is HTML list.

How can I hide one of the terms in one of the vocabs in the view result based on the role of the logged in user?

UPDATE: With the help from No Sssweat below, this is how the final snippet looks like:

function mytheme_preprocess_views_view_fields($vars){
  global $user;
  if ($vars['view']->name == "view name" && strip_tags($vars['fields']{rendered_entity}->raw) == tid && in_array('user role', array_values($user->roles))){
    $vars['fields']{rendered_entity}->content = NULL;
  }
}

... in case anyone else need something like this snippet.

2 Answers 2

2

Assuming you added Taxonomy term: Name as the field, which you want to hide.

Add this to your theme's template.php file.

function mytheme_preprocess_views_view_fields($vars){

  $global user;

  if ($vars['view']->name == "name_of_view" && strip_tags($vars['fields']['name']->content) == "Term Name" && in_array('role name', array_values($user->roles))){
    $vars['fields']['name']->content = NULL;
  }

}
  • change mytheme to your actual theme name.
  • change name_of_view to your view name.
  • change Term Name to the name of the term.
  • change Role Name to the specific role name.
2
  • Thanks a lot! Just one thing... I am using Taxonomy term: Rendered taxonomy term... I am trying to figure out how to change your snippet but Devel refuse to show me the variables when I use dsm()... Could you enlighten me?
    – TBJ
    Feb 16, 2017 at 13:20
  • Never mind my comment, I just figured it out. Se my updated question. Thanks again!
    – TBJ
    Feb 16, 2017 at 13:37
1

You can achieve this using Views PHP module as well.

This module allows the admin account (user 1) to add fields, filters and sorts to views which use PHP code. It also supports additional accounts when the core PHP Filter module is enabled.

While this module allows you to directly use PHP inside views which may be useful for quick and easy solutions, it is highly advisable to use regular handlers and plugins when available (or even to create one yourself). Take note that filtering and sorting a view using PHP always has a considerable perfomance impact.

Steps:

Add Global PHP filter in view.
Load global user.
Get User role.
Get the term id
After getting both values, Put in condition.
If condition satisfy return False else return TRUE.

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.