Hiding the login block

crotown's picture

I'm wondering what tricks people know for hiding the login block but keeping it accessible for administrators (so they don't have to type ?q=user/login). Anyone like to share? Thanks in advance.

Login to post comments

Boy to my knowledge there is

Rick Hood's picture
Rick Hood - Thu, 2009-07-02 12:36

Boy to my knowledge there is no way to do that.

You have to be logged in as something in order for Drupal to know what to show you or what not to show you (e.g. the login block) different from what anonymous users see.

One thing: the url is just /?q=user and if you enable Clean URLs it's just /user

admin/settings/clean-urls


Good point

crotown's picture
crotown - Thu, 2009-07-02 14:23

Good point that the URL is just "user" and that with clean URL's you just need to add "/user" in the browser. A point to ponder: user/login works, but user/foo does not.


You can hide a link to /user

pdcarto's picture
pdcarto - Thu, 2009-07-02 13:20

I suppose it might be possible to write a module that saves a cookie to identify the user as someone who has logged in as the administrator, and use that to decide whether to display the login block. There might be a chicken and egg problem with that though...

One thing I've seen is to put a link to /user somewhere on the page that is colored the same as the background. It will be invisible, but the cursor will change when you hover over it (or you can even disable that, with css). You'll know where to click. It's basically the same as having to know to type "/user" in the address field, or to create a bookmark to "/user".

If you don't want to be so (transparently) secretive, just put a (unhidden) link to /user in a tasteful spot on the home page (or in a block that only displays with the home page)


hiding links

crotown's picture
crotown - Thu, 2009-07-02 14:27

Thanks Pat,
Your idea of hiding (or not hiding) is a little like a trick I remember hearing. And bookmarking /user for admins is practical. I'm thinking that the trick I vaguely remember hearing involves making the login block float (with a module?) and then hiding a link to it. Ring any bells?


Javascript can do that

pdcarto's picture
pdcarto - Thu, 2009-07-02 16:25

You can show/hide the block with some fairly simple javascript and css. Three things are needed:

  1. Use css to hide the div that the block is inside of initially Find the id of the login block using Inspect Element in Firefox (for example), and add "#⟨the id⟩ { display:none; }" to your css file.
  2. Create a javascript function that toggles the display attribute of that div (or which takes the id of the div as an argument (e.g. "toggleDisplay(id)")
  3. Add 'onclick="toggleDisplay(⟨the id of the login block div⟩)" ' to a link

Here's a javascript function that I've used (not in Drupal though):

<script language="javascript">
    function toggleDisplay(n) {
    if (document.getElementById) {
     var e = document.getElementById(n);
     var eStyle = e.style.display;
     eStyle = (eStyle == 'block') ? 'none' : 'block';
     e.style.display = eStyle;
    }
    else if (document.all) {
     var e = document.all[n];
     var eStyle = e.style.display;
     eStyle = (eStyle == 'block') ? 'none' : 'block';
     e.style.display = eStyle;
    }
   }
</script>

And then for your link, something like

<a href="javascript:void(0)" onclick="toggleDisplay(--the id--)">show login</a>


If you just want to hide the login block

skowyra's picture
skowyra - Fri, 2009-07-03 03:18

One thing you can do is to create a primary link with the path simply 'user' (sans quotes).


Another idea: Since /user is

J. Cohen's picture
J. Cohen - Sat, 2009-07-04 17:15

Another idea:
Since /user is hard to remember unless you use Drupal a lot, you could make a redirect with .htaccess from /login to /user. Easier for clients to remember. They can drag it to their bookmarks toolbar to make it even easier.

Redirect 301 /login http://example.com/user


This got me to thinking

skowyra's picture
skowyra - Mon, 2009-07-06 02:02

So I started playing around with a jQuery solution. You can see it in action on www.anthonymedia.com. I left the link to login and logout visible just under the primary links.

Here's what I did.

First, I created a file called login.js that contained this code:

Drupal.behaviors.loginPopup = function(context){
 
$('body.not-logged-in a.popLogin', context).click(loginPopup);
$('#login-close', context).click(loginClose)
  
function loginPopup(){
  $('#popup').show('slow');
  $('a.popLogin', context).click(loginClose)
return false;
}

function loginClose(){
  $('a.popLogin').unbind('click', loginClose);
  $('#popup').hide('slow');
}
};

Next, I added a region in the theme's .info file called login_popup along with a call to load the login.js file

regions[login_popup] = Login Popup

scripts[] = login.js

Then, I made sure the page.tpl.php and page-front.tpl.php body tag included the $body_classes

<body class="<?php print $body_classes; ?>">

Right under the body tag on the pages, added

<div id="popup"><?php print $login_popup; ?></div>

In order to display the login/logout link, this was added beneath the primary links,

<div id="login">
  <!-- login link start -->
  <?php print $pop_login; ?>
  <!-- login link end -->
</div>

Now, in the template.php file (ifeeldirty2col being the theme name) this was added to create the login/logout link that appears when you print the $pop_login variable

function ifeeldirty2col_preprocess_page(&$variables) {
  if ($variables['logged_in']){
    $variables['pop_login'] = '<a href="'.url('logout').'" class="popLogin">Log out</a>';
  }else{
    $variables['pop_login'] = l(t('Login'), 'user/login' , array('attributes' => array('class' =>'popLogin')));
  }
}

Style it a bit

#login-close {
  float:right;
  padding:0 10px 0 0;
}
#popup {
  position:absolute;
  top:60px;
  left:775px;
  width:300px;
  z-index:100;
  background-color:#FFFde8;
  display:none;
}

In my case, I added a block-user-0.tpl.php file so I could add a CLOSE link to the login block. The code in the block:

<div id="login-close"><?php print '<a href=# class="loginCloseBlock">close</a>'; ?></div>
<?php print $block->content ?>

Almost there. Now, flush the cache. If you don't have the admin-menu module installed, another way to do this via admin/settings/performance and press the Clear cached data button.

Finally, assign the User Login block to the Login Popup region you created in the .info file.

If you want me to go over this at the next user group meeting, I'd be happy to do that.

Jim


Seems like a good topic for the next meetup

crotown's picture
crotown - Mon, 2009-07-06 11:10

It's of interest to a few of us at least. Thanks all!


Hi jskowyra, Thanks for the

deirdrelee's picture
deirdrelee - Tue, 2009-09-01 09:17

Hi jskowyra,

Thanks for the great solution. This is exactly what I was looking for: to have a nice neat login on my site. However there is an issue with Internet Explorer. The popup login appears and disappears straight away, as opposed to staying open in Firefox. I've checked that this also occurs on your site www.anthonymedia.com. I'm using IE7.0.5730.11 and Firefox 3.0.13.

Cheers,
Deirdre


Yup, problem fixed

skowyra's picture
skowyra - Tue, 2009-09-01 13:17

Thanks for catching that. The line in the jquery function

function loginPopup()
$('a.popLogin', context).click(loginClose)

when removed, fixes it.


I limited block display for

userid's picture
userid - Thu, 2009-10-15 09:39

I limited block display for the login block to "show only on selected pages" - and entered "user" in the form. Presto!