Drupal7:创建没有模块的水平登录栏

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Drupal7:创建没有模块的水平登录栏相关的知识,希望对你有一定的参考价值。

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.
  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. */

以上是关于Drupal7:创建没有模块的水平登录栏的主要内容,如果未能解决你的问题,请参考以下文章

drupal7 boost模块为登录用户提供缓存

JavaFX自定义窗口标题栏

如何在登录表单上的drupal 7中设置占位符

使用循环片段依赖关系模块化单活动Android应用程序

Drupal 7 View 列出登录用户创建的内容

drupal7 - 使用 ajax(jquery) 加载节点