I want to hide the log in block that can be seen by anonymous users and replace it with a less conspicuous link on the navigation menu. Hiding the block was easy. The hard part is figuring out how to make the "log in" menu item disappear when a user is logged in?

Is there anybody out there who has done this and has found an easy solution?

Comments

nevets’s picture

You can make a small block that contains the following php code

<?php
  global $user;

  if ( !$user->uid  )
  {
    echo l(t('login'), 'user');
  }
?>

It takes the user to a page where they can login/register.
Once they login in the block disappears

Steve Dondley’s picture

nevet,

Thanks for the input but creating a whole block for one links is more than I want to do. I need it to be a part of the main menu so it blends in more with the rest of the site.

Sam Deane’s picture

I've been trying to do this too.

I tried making a module which adds some custom menu commands, but if the menu command's path is 'user/login' then it doesn't show up!

I get the impression that there is already a menu item with this path (probably the login tab on the login page), which is in conflict with my code.

Bit of a shame, as it's annoying having to have a custom block for such obviously useful functionality.

kalin_s’s picture

Someone could not log in, because he had removed the block. So he was given the link to the "login node" - "www. .... .com/user".
I think you can use it as a "normal" link even in the navigation menu if you have installed the menu.module, which makes it possible to customize the menu itself.
----------
Working on a bilingual mini-portal - www.archeologic.net

sepeck’s picture

There is a built in alias to /user as there is also a built in alias to /admin. These aliases are there for convience as well as funtionality.

More block info here: http://drupal.org/node/21869 and this is also a nice spot to start http://drupal.org/node/17272 on your experimentation. If you come up with anything different, consider adding a handbook page there.

As to the obviousness of this, well, it may certainly be obvious to you, but in the 18 months I have been using Drupal sites I have not needed this functionality. I remove the logon block on the vast majority of my sites. 'Obviousness' is a matter of perspective. Your perspective will be a nice new addition to the community and we all look forward to your contributions. It'll only make things better.

-sp
---------
Test site...always start with a test site.
Drupal Best Practices Guide

-Steven Peck
---------
Test site, always start with a test site.
Drupal Best Practices Guide

kalin_s’s picture

If you are not a PHP coder, you have to screen through all possible conbinations of the material (the modules). It's a challenge, but it is rewarding.
----------
Working on a bilingual mini-portal - www.archeologic.net

webmestre@www.coll-vallcarca.net’s picture

Thanks to the link, because I had lost the login block and I need to login.

Thanks a lot!!!

Sverre’s picture

I'm using Drupal 4.6. I don't claim to be an expert, but this is how I created a dynamic 'log in' menu item, within the navigation block...

First open user.module in an editor (I use Dreamweaver) (user.module is in the 'modules' folder).

Around about line 696 you'll find a comment: '//Your personal page' below which is the if statement which appears to control the 'my account' menu item.

I copied this complete if statement, pasted it immediately below, then made adjustments to suit. (Note the addition of an exclamation mark before $user->uid to achieve the opposite of the 'my account' menu item.)

This is what I ended up with, and gave me a working, fully dynamic 'log in' menu item:

    //Edited by Sverre: Add dynamic log in menu item
    if (!$user->uid) {
      $items[] = array('path' => 'user', 'title' => t('Log In / Register'),
        'callback' => 'user_page', 'access' => TRUE,
        'type' => MENU_DYNAMIC_ITEM);
    }
heine’s picture

This is a fairly old thread. Adapting core modules is your prerogative, but can give trouble when updating (you need to apply the change again).

After a chat on #drupal-support with JoeyDay I wrote a tiny module to accomplish this:

/* $Id: loginmenu.module 7 2006-01-11 08:07:40Z Heine $
 *
 * @author Heine Deelstra
 */  
function loginmenu_help($section) {
  $output = '';
  switch ($section) {
    case 'admin/modules#description':
      $output .= t('Generates a login link for anonymous users');
      break;
    case 'admin/help#loginmenu':
      $output .= '<p>'. t('By design the login link will be at the top of the menu.'). '</p>';
      break;
    case 'admin/help/loginmenu':
      $output .= t('Generates a login link in the navigation menu for anon only.');
      break;
  }
  return $output;
}
/* Implements hook_menu
 * Returns a menu hook with access set to NOT(userid)
 * Since anonymous has uid 0, this will be true for anon
 */
function loginmenu_menu($may_cache) {
  global $user;
  $items = array(); 
  if(!$may_cache){
    $items[] = array(
      'path'      =>  'login',
      'title'     =>  t('Login'),
      'access'    =>  !($user->uid),
      'callback'  =>  'drupal_goto',
      'callback arguments' => array('user'),
      'weight' => -10 /* modify to set it no longer on top */
    );  
  }
  return $items;
}

Save as loginmenu.module in modules/loginmenu, omit the last ?> tag.
--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

Sverre’s picture

Many thanks Heine, and to anybody else who comes this way I withdraw my hacking at a core module in my previous comment and recommend using Heine's loginmenu.module.

I'm an ASP / VB / .NET person really, but I've come to like Drupal and am learning php as needed. After 10 minutes searching for a solution I tend to have a bash myself and see what happens!

Within the $items array in the loginmenu_menu function, is there an additional option to add a title attribute to the generated link? (i.e. <a href="login" title="THIS">Login</a>)

Sverre’s picture

I've just worked this out for myself! Simply add an additional line below the one starting 'title' as follows:

'description' => t('Login'),
heine’s picture

You're fast!

A downloadable 4.7 version of loginmenu.module has two more settings: the text of the link and the weight in the menu. The page is in Dutch.

(I haven't tested whether it has an effect on caching)

--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

rwohleb’s picture

Here is a revised version that makes the menu item movable, like Logout is. Tested in 4.7.

/* $Id$
*
* @author Heine Deelstra
*/ 
function loginmenu_help($section) {
  $output = '';
  switch ($section) {
    case 'admin/modules#description':
      $output .= t('Generates a login link for anonymous users');
      break;
    case 'admin/help#loginmenu':
      $output .= '<p>'. t('By design the login link will be at the top of the menu.'). '</p>';
      break;
    case 'admin/help/loginmenu':
      $output .= t('Generates a login link in the navigation menu for anon only.');
      break;
  }
  return $output;
}
/* Implements hook_menu
* Returns a menu hook with access set to NOT(userid)
* Since anonymous has uid 0, this will be true for anon
*/
function loginmenu_menu($may_cache) {
  global $user;
  $items = array();
  if($may_cache){
    $items[] = array(
      'path'      =>  'login',
      'title'    =>  t('Login'),
      'description' => t('Login'),
      'access'    =>  !($user->uid),
      'callback'  =>  'drupal_goto',
      'callback arguments' => array('user'),
      'type' => MENU_DYNAMIC_ITEM | MENU_NORMAL_ITEM,
    ); 
  }
  return $items;
}

Save as loginmenu.module in modules/loginmenu, omit the last ?> tag.

darren oh’s picture

I have attached a downloadable copy of this file to issue 74706.

rwohleb’s picture

I've created a project to house the module referenced.

http://drupal.org/project/loginmenu

jack large’s picture

Hi,

Tried your module and it works fine but only for the default Navigation menu. I need the same functionality but on a custom menu. When I've attempted to make such a menu item it never appears. Any suggestions?

Thanks,

John.

Eric Cosky’s picture

I wanted the same thing, pretty much, except for with the account menu. I modified the login menu to do this; you can grab it from here (its attached to the post): http://www.cosky.com/a_year_with_drupal

Not sure it's worth a whole project or if it should be packaged with other little menu things like login. Or unified somehow. Anyway, I hope someone else gets some use out of it.

Cheers,

- Eric Cosky
http://www.cosky.com