1

Drupal 7

Is it possible to write an if statement in the page.tpl.php file that first gets the user role and then compares it to a string and it either shows the logo or doesn't. Something like...

//some how get user role

global $user;
$user_fields = user_load($user->uid);

$current_role = $user_fields->role[];

if ($current_role == "public_user"){
>?
<?php
 print $front_page; 
?>" title="

<?php
 print t('Home'); 
?>
" rel="nofollow">

?>
}else{
//don't show.
}
1

2 Answers 2

4

I'd suggest checking against a permission, rather than an entire role.

In your custom module, add:

function mymodule_permission() {
    return array(
        'view site logo' => array(
            'title' => t('View Site Logo'), 
            'description' => t('Allows a user to view the site logo'),
        ),
    );
}

And in your theme's template.php page preprocessor, add:

if(!user_access("view site logo")) {
    $variables['logo'] = "";
}

This way, $logo will not be processed in page.tpl.php for a user without the permission.

3
  • Thanks +1 stupid question... the custom module, is that something I create and upload via the module form?
    – Denoteone
    Commented Sep 21, 2011 at 5:07
  • 1
    Customizations can be made at many levels in Drupal. Typically users start at the theme layer with custom code/over rides in template.php of their site theme. For more advanced customizations a user can create a custom module from scratch and install it on their site. In "Jukebox" example above, the mymodule_permission function would be in your custom module and named after the module rather than "mymodule_permission" it could be "any_name_you_choose_permission".
    – DeeZone
    Commented Sep 21, 2011 at 7:39
  • Take a look at this: Create your first simple Drupal 7 module -> reinholdweber.com/2010/06/28/…
    – DeeZone
    Commented Sep 21, 2011 at 7:43
1

Get the ROLE ID from the listing of roles in the admin section of site at http://YOURSITE.COM/node#overlay=admin/people/permissions/roles

global $user;

if (array_key_exists('ROLE ID', $user->roles)) {
  print $front_page; 
}
elseif (array_key_exists('SOME OTHER ROLE ID', $user->roles)) {
  print t('Home');
} 
else {
  //don't show.
}
2
  • Will I be able to add the code above to my page.tpl.php file?
    – Denoteone
    Commented Sep 21, 2011 at 4:42
  • 1
    Yes - not the cleanest solution but it would work. Don't forget to surround the code with PHP open and close tags <?php ?> to jump in and out of HTML.
    – DeeZone
    Commented Sep 21, 2011 at 7:41

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.