Search code examples
drupaldrupal-taxonomydrupal-blocks

drupal 7 block restriction based on user term reference


Users are set up on my D7 site with various term references as profile fields.

I need to be able to show hide certain blocks based on these term references, is this possible?

I cant see a way to link a block with a term reference.


Solution

  • One way to achieve this is to enable php filter and set Show block on specific pages -> Pages on which this PHP code returns TRUE (experts only) in the block settings.

    then you can access the user profile field terms of the logged in user like this

     global $user;
     $user = user_load($user->uid); 
    
     $term1 = $user->field_term1[LANGUAGE_NONE][0]['value']; //the value should be the tid of the term
     $term2 = $user->field_term2[LANGUAGE_NONE][0]['value'];
    

    then you can return true accordingly to control the visibility of the block.

    for example

           return $term1 == 15 //this is the tid of the term
    

    The above code will show the block only to users with custom field term with tid = 15

    Hope I am not confusing you.