What is the best way to implement this?

A link appears in the header that says Log In. It leads to the "/?q=user" page.

Once logged in, the user finds 2 links in the same position that read My Account and Log Out, linked to "/?q=user" and "/?q=logout" respectively.

Comments

g10tto’s picture

bump

g10tto’s picture

I notice that when you configure a block's settings, you can set it to appear only when a certain statement is true.

I would think that would be the easiest method, but I don't know what code to place that would show that a user is logged in.

tms8707056’s picture

This method could work but I found some limitations. I attempted this method with the custom links module. The problem that I ran into was that the links did not match my theme when they were displayed. Therefore, I modified my theme (code below) to display the links appropriately. It would be much easier if there were link based permissions built into core.

jody lynn’s picture

There are menu link permissions built into core. Just make a new menu, and add menu items for user, user/login, and logout. Each will be displayed to users who can access them. If that doesn't get you all the way there then add menu per role module to restrict access manually. --Zivtech--

--Zivtech--

tms8707056’s picture

What I was meaning was a selectable role for each menu item that is part of the core menu system (i.e. when you add a menu item you can set permissions/roles solely for that item). The menu per role module may meet this need. However, I am not familiar with the module.

tms8707056’s picture

My method is posted below. It contains custom menus for authenticated vs. non-authenticated users. It also contains a login redirect that drops the user on the same page where they clicked the login link. I don't know if this is the best way to implement this or not, but it works for me.

The example below renders a different menu for admin, anonymous, and authenticated users. The menu links are constructed as variables to make the code easier to read and to make it easier to manipulate the order of the menu items if desired. The code will have to be customized thoroughly to work on another website.

This code is in my template.php file for my theme. It is located inside of the function phptemplate_preprocess_page(&$vars).

// User Navigation

    global $user;
    $destination = drupal_get_destination();

// Set Menu Links As Variables
              $launch_player_link='<li class="first"><A HREF="javascript:popup(\'/drupal6/XSPF/player.php\')">LAUNCH PLAYER</A></li>';
              $register_link='<li class="menu-340">'.l('REGISTER','user/register').'</li>';
              $login_link='<li class="menu-454 last">'.l('LOGIN', 'user/login', array('query' => $destination)).'</li>';
              $create_link='<li class="menu-305 first">'.l('CREATE','node/add').'</li>';
              $admin_link='<li class="menu-304">'.l('ADMIN','admin').'</li>';
              $logout_link='<li class="menu-290 last">'.l('LOGOUT','logout').'</li>';
              $my_account_link='<li>'.l('MY ACCOUNT','user/'.$user->uid).'</li>';


// Build Menus Based on User

    $output = '<ul class="links secondary-links">';

    if ($user->uid == 0) {
               $output .= $launch_player_link.$register_link.$login_link;
        }

        elseif ((in_array('Admin', array_values($user->roles))) || ($user->uid == 1)) {
               $output .= $create_link.$my_account_link.$admin_link.$logout_link;
        }

        else {
               $output .= $launch_player_link.$my_account_link.$logout_link;
        }

    $output .= '</ul>';

    $vars['user_navigation'] = $output;

// End User Navigation

I then use the code below in my Page.tpl.php file where I want the menu to appear.

<?php print $user_navigation ?>

If somebody knows a way to clean up my code I would appreciate suggestions. I simply hacked this together to get it working on my site. I haven't found any issues yet. I think it would be nice to develop a module with some kind of admin interface for menu items, but I don't know how to do that... yet.