0

I'm currently displaying a block on my D7 site' homepage.

Now I've a requirement to display that block only if the logged in user has a specific value in their profile.

I know it can be done using php evaluation by navigating to the bottom of the block and then click (Pages > Pages on which this PHP code returns TRUE ) but have no experience in writing php scripts.

global $user;

If($user->your_profile_field_value == 'requiredvalue') 
{ 
// display the block 
// display the block only on 'homepage' } 
Else { // don't display the block }

I'm new to php can someone please help me fill in the blanks in above code snippet.

thanks

  • Writting code is a good choice. Though views (drupal.org/project/views) could provide an alternative if you need, considering a contextual filter + relationship with user profile fields in your view. – Tritof Dec 27 '17 at 12:13
0

you can create custom module and implement this code

 /**Implementation of hook_preprocess_HOOK().  */

 function mymodule_preprocess_page(&$variables)
 {   
  global $user;
  if($user->your_profile_field_value == 'requiredvalue')  
  {
     if (drupal_is_front_page()) {
         return TRUE;
        // for front page only condition  
     }
    // your code
  }
 }

you can also add this in your current enable theme by just replace

mymodule to your themename

  • 1
    "have no experience in writing php scripts." There's no way he'll get from here to what he needs. – Niall Murphy Dec 27 '17 at 12:31
0

I would use the PHP visibility settings to display the block if user has requiredvalue. Once you've created the block that you want to display (or not display) add the following code to the "Page specific visibility settings" in the block configuration menu:

 <?php 
    global $user;

    If($user->your_profile_field_value == 'requiredvalue' && drupal_is_front_page()) {
    return TRUE;
    }

    return FALSE;

    ?>
  • thanks for your quick reply. How do I then only display this block only on home/front page as once I select the visibility option I'm no longer able to list my pages by selecting option 'Only the listed pages' in block settings – ozyy Dec 27 '17 at 13:14
  • try passing && drupal_is_front_page() check if you can achieve – Mahesh V Dec 27 '17 at 13:31
  • To make block visible on front page add following snippet to blocks 'Page specific visibility settings' and make sure using as PHP <?php if (drupal_is_front_page()) { return TRUE; } ?> or to hide it the reverse <?php if (drupal_is_front_page()) { return FALSE; } ?> – Mahesh V Dec 27 '17 at 13:36
  • Thanks for the quick reply @Mahesh v but no luck so far. I'm thinking if it is because the user field I'm tying to access is a custom field. please look at my code below<?php global $user; If($user->field_current_employer == 'TWSXV' && drupal_is_front_page()) { return TRUE; } return FALSE; ?> – ozyy Dec 27 '17 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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