2

I've looked everywhere for an answer to my problem, but can't find one. My goal is to collapse/force a custom region on my website to disappear when a user logs in, and then have it reappear when the user logs out. Or, if it's not possible to collapse a region, to replace the region with an empty region that, through CSS, I have styled to display with zero height. Basically, I'm trying to make a custom region disappear upon user login, and then make it so the region reappears when the user logs out.

The reason for this is I'm putting the final touches on transforming the login block on my website so that instead of appearing in the sidebar, it appears as a horizontal login bar. I'm 90% of the way there. I created a custom region at the very top of the page to house the bar, and the bar kind of looks like the horizontal login bar you see on Facebook. But in reformatting the login block using CSS, I was only able to achieve the look I wanted by forcing the "Forgot your password?" and "Create a new account" links not to appear (otherwise, the links would reside between the password field and the "Log in" button, which looked weird).

But that left me with no login help links. To try to resolve this problem, I created a new region directly under the "username" and "password" fields to house the "Forgot your password?" and "Create a new account" links, which displays really nicely. But, of course, whenever someone logs in, those links remain on the screen, which is obviously weird.

I need to figure out a way to make the new region disappear when the user logs in, and to reappear when the user logs out. Also, I want the other regions in the header directly below the two new custom regions to move up once the region disappears so that there's not a gaping hole in the space where the region used to be. I'm pretty new to PHP, so what I've tried may be way wrong, but this is what I attempted, which didn't work -- it's the option of trying to swap out the region with the links with the empty region when a user logs in:

     <?php 
        if(isset($username) == false):
        print render($page['nameofregion1']);
        else: ?></div>
        <div id="region2style">
        <?php print render($page['nameofregion2']);
        endif;
     ?>

where the CSS for 'region2style' is set to "height: 0px" to give it zero height. My suspicion is that I'm misusing the variable $user here, among other errors, but I can't seem to figure out what variable holds username info in Drupal. I'm sure there's a more elegant way of doing this, and if someone could point me in the right direction, I'd really appreciate it. Thank you.

  • 1
    If you place your custom block in the region, have you tried configuring the block itself to be visible only to anonymous users? – Triskelion Apr 18 '13 at 21:00
  • That makes sense, Triskelion. I tackled the problem using PHP, but this would have been a good approach, too. Thanks for letting me know. – Drupguy Apr 19 '13 at 11:32
1

Context is a great plugin for remodelling sections of your website - paired with Delta and the Omega theme, you can overhaul your entire layout based on nearly any condition without editing any code by hand.

There's a decent intro tutorial for Context and Delta over here.

But if that doesn't fit the bill, you're on the right track with your .tpl.php files. Checking for user login is quick, just remember to global the variable first:

<?php
global $user;

if ( $user->uid ) {
  // Logged in user
}
else {
  // Not logged in
}
?>

... and by inserting the desired code between braces you could easily toggle blocks, regions, messages and nearly anything else accessible from the template.

This is my "PRINT ALL THE THINGS!!" snippet, try it out for the $user variable:

<?php global $user; echo '<pre>'.print_r($user, true).'</pre>'; ?>

Good luck!

  • Helpers such that user_is_logged_in() or user_is_anonymous() are useful as well. – arshadcn Apr 18 '13 at 23:03
  • Thank you, npc!!! It worked! I just had to add "==false" to the line "if ($user->uid)" (in other words, I made it "if ($user->uid == false)..." and it worked perfectly. Thank you very much! – Drupguy Apr 19 '13 at 11:30
  • Glad to help. Don't forget to upvote and select an answer! – emc Apr 19 '13 at 20:25
  • I'm a newbie to Drupal Answers, npc, so it won't let me upvote anything without a 15 reputation. But I chose your answer as the most helpful. Thanks!! – Drupguy Apr 19 '13 at 21:08
1

If the login block is the only content in that region, you can achieve this without any custom modules, template overrides, or custom code, just by means of Drupal's built-in role based access control.

The key is that Drupal will not display an empty region. By making the login block inaccessible to authenticated users, the login block will disappear as soon as the user logs in.

Assuming Drupal 7, you go to Structure -> Blocks, and click "configure" for the User login block. You then go to the Roles panel, and tick the box under "Show block for specific roles" to make the anonymous user the only role that is allowed to see this block.

(This method for works in Drupal 6 as well, but the navigation path is slightly different.)

  • That makes a lot of sense, Gisle. I went ahead and solved the problem using PHP, but your insight is very helpful and I'm sure I'll be able to apply it in the future. Thank you! – Drupguy Apr 19 '13 at 11: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.