继续Drupal分享精神 QQ群:107748121 站长QQ:2275288328 skype: hellodrupal【drupal交流+drupal 建站+theme制作】
登录 注册

覆写用户登录区块user_block,drupal theme template.php 里设置

默认主题,在后台区块里面可以看到一个用户登录的区块,但是有时想改掉它的表现形式,按照自己的设计来设计。 这时就需要修改user_block。这个用户登录的block,注意这是区块和user 登录注册那个不一样。这个用户登录区块是在user模块里面定义的。可以先看看代码: 文件位于 user.module 分析源文件: url($_GET['q'], array('query' => drupal_get_destination())), '#id' => 'user-login-form', '#validate' => user_login_default_validators(),//用到 user_login_default_validators验证函数 '#submit' => array('user_login_submit'), ); $form['name'] = array('#type' => 'textfield', //定义用户名表单 '#title' => t('Username'), '#maxlength' => USERNAME_MAX_LENGTH, '#size' => 15, '#required' => TRUE, ); $form['pass'] = array('#type' => 'password',//定义密码表单 '#title' => t('Password'), '#maxlength' => 60, '#size' => 15, '#required' => TRUE, ); $form['submit'] = array('#type' => 'submit', '#value' => t('Log in'), ); $items = array();//返回items 数组 主要是两个衔接 if (variable_get('user_register', 1)) { $items[] = l(t('Create new account'), 'user/register', array('attributes' => array('title' => t('Create a new user account.'))));// 创建新帐号衔接 } $items[] = l(t('Request new password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));//创建找回密码衔接 $form['links'] = array('#value' => theme('item_list', $items)); return $form; } ?> 定义一个user 登录区块 uid && !(arg(0) == 'user' && !is_numeric(arg(1)))) {//判断是否登录,而且是否是注册页面,第一个参数不是user 并且 第二个参数不是数字 $block['subject'] = t('User login'); $block['content'] = drupal_get_form('user_login_block');// 区块内容就是返回一个form,调用上面的form 数组,drupal_get_form 这个函数强大,很好用。 } return $block; ?> 理解上面的源文件,开始做覆写的工作,主要参考以下代码: 下面就考虑如何在drupal theme template.php里面覆写这个区块了。 第一步:建立template.php文件 在所启用的主题下面新建立一个template.php 的文件夹,然后把下面的代码拷贝进去,把yourthemename 改为自己所用的主题名称。 不要拷贝"?>" function yourthemename_theme() { return array( 'user_login_block' => array( 'template' => 'user_login', 'arguments' => array('form' => NULL), ), ); } function yourthemename_preprocess_user_login_block(&$variables) { //去掉两个衔接。 $variables['form']['name']['#title']='请填写你的大名'; $variables['form']['pass']['#title']='请出示的暗号'; $variables['form']['submit']['#value']='快快登录'; $variables['form']['links']['#value']=''; } //拷贝到这里,后面问号就不要拷贝了?> 第二步:建立user_login.tpl.php文件 放在主题里面。 把下面代码拷贝进去即可: 直接在 user_login.tpl.php里面
效果如下图:

评论

这样输出登陆框会有form

这样输出登陆框会有form id样式,影响设想的页面布局,该怎么办呢?

form id样式 是什么意思?

form id样式 是什么意思? 它不是隐藏的变量吗?这个登录框的区块变量,任意组合。定义CSS。

我另一个站点使用的 修改代码,右边效果, 体检网 http://jstjw.com/
template.php 代码

<?php
function  yourthemename_theme() {
  return array(
   
'user_login_block' => array(
     
'template' => 'user_login',
     
'arguments' => array('form' => NULL),
    ),
  );
}

function
yourthemename_preprocess_user_login_block(&$variables) {

//注释代码,你需要就可以去掉
//  $variables['form']['name']['#value'] ='请输入用户名';
//  $variables['form']['pass']['#value'] ='请输入密码';、
//  $variables['form']['name']['#attributes']['OnClick'] = 'this.value=""';
//  $variables['form']['pass']['#attributes']['OnClick'] = 'this.value=""';
// $variables['form']['name']['#title']='用户名';
// $variables['form']['action']['#title']='';
//$variables['form']['submit']['#value']='登录';
//$variables['form']['links']['#value']='';

 
$variables['form']['name']['#title']='';
 
$variables['form']['pass']['#title']='';

}
?>

然后直接在user_login.tpl.php ,样式自己调整。

<table border="0"  width="200px" cellpadding="0" cellspacing="0"  height="100px">
<tr><td >用户名:</td><td align="left"><?php print  drupal_render($variables['form']['name']);?></td></tr>
<tr><td>密码:</td><td align="left"><?php print  drupal_render($variables['form']['pass']);?></td></tr>
<tr><td align="right"><?php print  drupal_render($variables['form']['submit']);?><?php print  drupal_render($variables['form']['form_id']);?><?php print  drupal_render($variables['form']['form_build_id']);?></td><td align="center"><a href="/user/register">注册帐号</a></td></tr>
</table>

发表新评论

此内容将保密,不会被其他人看见。
  • 自动将网址与电子邮件地址转变为链接。
  • 自动断行和分段。
  • You can use BBCode tags in the text.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

更多关於格式化选项的信息

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.