You are here

welcome_username.module in Welcome Username 7

Same filename and directory in other branches
  1. 8 welcome_username.module

Show a simple login form to anonymous users and a 'Welcome username'|logout to authenticated users.

File

welcome_username.module
View source
<?php

/**
 * @file
 * Show a simple login form to anonymous users and a 'Welcome username'|logout
 * to authenticated users.
 */

/**
 * Implements hook_block_info().
 */
function welcome_username_block_info() {
  $blocks = array();
  $blocks['welcome_username'] = array(
    'info' => t("Welcome Username Login/Logout"),
    'cache' => DRUPAL_NO_CACHE,
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function welcome_username_block_view($delta = "") {
  $block = array();
  switch ($delta) {
    case "welcome_username":

      // Don' show a title.
      $block['subject'] = t("");

      // For anonymous users load a simple login form.
      if (user_is_anonymous()) {
        $form = drupal_get_form("user_login");

        // Placeholders.
        $form['name']['#attributes']['placeholder'] = $form['name']['#description'];
        unset($form['name']['#description']);
        $form['pass']['#attributes']['placeholder'] = $form['pass']['#description'];
        unset($form['pass']['#description']);
        $block['content'] = drupal_render($form);
      }
      else {

        // Load user object.
        global $user;

        // Show realname if available.
        if (module_exists('realname')) {
          $name = realname_load($user);
        }
        if (!isset($name) || strlen($name) < 1) {
          $name = $user->name;
        }

        // Load string from variable table if set.
        $welcome_string = variable_get('welcome_username_welcome_string', 'Welcome');
        $logout_string = variable_get('welcome_username_logout_string', 'logout');

        // Create a link to the user profile page.
        $user_profile_link = l(t($welcome_string) . " " . $name, 'user/' . $user->uid);

        // Create a logout link.
        $user_logout_link = l(t($logout_string), "user/logout");

        // Create the message shown on the frontend.
        $logged_in_user_message = "";
        $logged_in_user_message .= $user_profile_link;
        $logged_in_user_message .= " | ";
        $logged_in_user_message .= $user_logout_link;
        $block['content'] = $logged_in_user_message;
      }

      // Return the rendered block;
      return $block;
  }
}