You are here

function welcome_username_block_view in Welcome Username 7

Implements hook_block_view().

File

./welcome_username.module, line 23
Show a simple login form to anonymous users and a 'Welcome username'|logout to authenticated users.

Code

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;
  }
}