Skip to Navigation

How to Create a Custom Login and Welcome Block in Drupal 6

login with no welcome login with welcome text

Every now and then I have the task of creating a Drupal block that displays Register and Login links for users who are not logged in and also displays Welcome Text and Logout links for users who are logged in. This can easily be done using two separate Drupal blocks and good ole PHP. However, if like me, you have curious clients who aren't knowledgeable in PHP but love editing every block on their website, a different solution may be more suitable.

Using a Custom Module to Create a Login/Wecome Block

If you don't already know how to create a module in Drupal 6, you can check out the tutorial on the Drupal site.

  1. In your custom_module_name.module file, you will need to implement hook_block to create your custom block. Below is the function I used for my module (f1_login_welcome_block):
    /*Implements hook_block to make custom login/welcome block for users */
    function f1_login_welcome_block_block($op = 'list', $delta = 0, $edit = array()) {
        if ($op == 'list') {
            $blocks[0] = array('info' => t('F1 Login/Welcome Block'));
                return $blocks;
            }
            else if ($op == 'view') {
                switch($delta) {
                    case 0:
                    $block = array(
                        'content' => f1_login_welcome_block_display_block()
                    );
                    break;
                }
            return $block;
        }
    }
    Your function should look just like this, except you should replace all places that say 'f1_login_welcome_block' with the name of your custom module. As you'll notice, this function simply adds my block to the Block listing with the description "F1 Login/Welcome Block" and it displays the block using a custom function f1_login_welcome_block_display_block.
     
  2. Next, you should add your function to display the content for your block. In the function I created (f1_login_welcome_block_display_block), I used a conditional statement to show a welcome block if the $user->uid value is not empty and display a login block if it is empty. Below is the function I used:
    function f1_login_welcome_block_display_block() {
        //load user
        global $user;

        //if user is logged in, display welcome block
        if (false == empty($user->uid)) {
            $username = t(truncate_utf8($user->name,20,false,true));
            $user_link = l($username, 'user',
                array(
                    'attributes' => array(
                         'class' => 'wb-user-link'
                    )
                )
            );
            $logout_link = l(t('Logout'),'logout',
                array(
                   'attributes' => array(
                        'class' => 'wb-logout-link'
                    )
                )
            );
           //set the html for welcome block
            $block_text = '<div class="welcome-block">Welcome, '.$user_link.
                   '!&nbsp;&nbsp;&nbsp;&nbsp;'.$logout_link.'</div>';
        }
        //if uswer is not logged in, display login block
        else {
            $register_link = l(t('Join'),'user/register',
                array(
                    'attributes' => array(
                        'class' => 'lb-register-link'
                    )
                )
            );
            $login_link = l(t('Login'),'user/',
                array(
                    'attributes' => array(
                        'class' => 'lb-register-link'
                    ),
                    'query' => drupal_get_destination()
                )
            );
            //set the html of login block
            $block_text = '<div class="login-block">'.$register_link.
                        ' '.$login_link.'</div >';
        }

        return $block_text;
    }
    As you can see, I used the drupal_get_destination() function to redirect users back to their current page after they login on the login page. You can omit that line if you wish for users to be redirected to their account pages after login.
     
  3. Enable your module and add it to a block region!

That's it, you now have your own custom login/welcome block in your own module.