Drupal 7 : Creating Horizontal Login Bar Without Module


/ Published in: PHP
Save to your folder(s)

Do you know that you can create a new form directly from theme template.php without the need of creating a module in Drupal 7?

Armed with that knowledge, we will build a new login form and transform it to become a horizontal login bar.

You might asked, why create a new login form while you can form alter the standard login block form (user_login_block) ? The reason for this is very simple, the standard login block form will be altered by other contribution module such as captcha module and it would not be possible to display the captcha in a tight spaced horizontal bar. Not to mentioned other standard module such as openid will also altered the module and adds its own html tag.

So by creating a new login form we will be able to create a clean and slim horizontal login bar without the worry about other contribution module messing the layout of our horizontal login bar.

To do this we need to add a few functions to the theme template.php.


Copy this code and paste it in your HTML
  1. /** Function 1 : create a $form array for our new login form
  2. */
  3. <?php
  4. function horizontal_login_block($form) {
  5. $form['#action'] = url($_GET['q'], array('query' => drupal_get_destination()));
  6. $form['#id'] = 'horizontal-login-block';
  7. $form['#validate'] = user_login_default_validators();
  8. $form['#submit'][] = 'user_login_submit';
  9. $form['#prefix'] = '
  10. ';
  11. $form['#suffix'] = '
  12. ';
  13. $form['name'] = array(
  14. '#type' => 'textfield',
  15. '#prefix' => '
  16. ',
  17. '#suffix' => '
  18. ',
  19. '#maxlength' => USERNAME_MAX_LENGTH,
  20. '#size' => 15,
  21. '#required' => TRUE,
  22. '#default_value' => 'Username',
  23. '#attributes' => array('onblur' => "if (this.value == '') {this.value = 'Username';}", 'onfocus' => "if (this.value == 'Username') {this.value = '';}" ),
  24. );
  25. $form['pass'] = array(
  26. '#type' => 'password',
  27. '#maxlength' => 60,
  28. '#size' => 15,
  29. '#required' => TRUE,
  30. '#prefix' => '
  31. ',
  32. '#suffix' => '
  33. ',
  34. );
  35. $form['actions'] = array('#type' => 'actions');
  36. $form['actions']['submit'] = array('#type' => 'submit', '#value' => '');
  37. return $form;
  38. }
  39. ?>
  40. /**
  41. This function will create a div wrapper for the login form and div wrapper for the login textfield and password textfield. Also the function will embed a javascript function to display "Username" in the username textfield which will be changed when the textfield is clicked. This is important because the function didn't have any title field to let user differentiate between username field and password field.
  42.  
  43. Next we will need to create the actual function for building the form array and output it as html tag
  44.  
  45. Function 2 : Determine if user is not logged in -> show the login form but if user is logged in -> display greeting message instead
  46. */
  47. <?php
  48. function login_bar() {
  49. global $user;
  50. if ($user->uid == 0) {
  51. $form = drupal_get_form('horizontal_login_block');
  52. return render($form);
  53. } else {
  54. // you can also integrate other module such as private message to show unread / read messages here
  55. return '
  56. ' . t('Welcome back ') . ucwords($user->name) . '
  57.  
  58. ';
  59. }
  60. }
  61. ?>
  62. /** This function will switch between anonymous user and logged in user. You will need to call this function in the area that you wish for the login bar to appears.
  63.  
  64. Last thing is the css, you will need to create a new css / use the theme css to add these simple css code :
  65. */
  66. <?php
  67. /** Login Bar **/
  68. .usericon, .passicon {
  69. float: left;
  70. width: 180px;
  71. padding-left: 36px; /** create space for small 24px x 24px icon **/
  72. height: 24px;
  73. }
  74. #loginbar {width: auto; float: right;}
  75. #loginbar .form-actions {display: none;}
  76. #loginbar p { color: #fff; font-size: 1.1em; font-weight: bold;}
  77. ?>
  78. /**
  79. If you notice, the functions still build the login submit button, without this the form will not submit thus no login process will ever happen. To tackle this, we just need to hide the login submit button via css
  80.  
  81. To see this code in action, you can visit : http://zenith.victheme.com
  82. */

URL: http://www.drupalstore.info/blog/drupal-7-creating-horizontal-login-bar-without-module

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.