Drupal登录表单自定义

时间:2013-11-26 12:01:29

标签: drupal drupal-7 drupal-views drupal-theming drupal-forms

您好我使用了此代码

<?php
    $elements = drupal_get_form("user_login");
    $form = drupal_render($elements);

    echo $form;
?>

获取我网站的默认Drupal登录表单,但我需要自定义HTML,我在模块/用户中找到了一些页面,但不了解如何自定义结构。

2 个答案:

答案 0 :(得分:2)

Drupal的用户登录表单由user_login使用Drupal Form APIuser.module功能构建。如果您需要自定义它,您应该使用模块中的hook_form_alter()

function YOUR_MODULE_NAME_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id=='user_login') {

     // YOUR CUSTOM CODE FOR THE FORM GOES HERE

  }
}

**编辑,在您的评论之后**

您不需要调用YOUR_MODULE_NAME_form_alter()函数:Drupal每次需要构建表单时都会通过钩子机制为您执行此操作,并且在$form_id=='user_login'时,它会将登录表单修改为允许您的自定义。 Drupal的方式在drupal.org中有详细讨论,只需按照我在本答案开头写的链接。

用户登录表单以user.module

的方式声明
// Display login form:
  $form['name'] = array('#type' => 'textfield',
    '#title' => t('Username'),
    '#size' => 60,
    '#maxlength' => USERNAME_MAX_LENGTH,
    '#required' => TRUE,
  );

  $form['name']['#description'] = t('Enter your @s username.', array('@s' => variable_get('site_name', 'Drupal')));
  $form['pass'] = array('#type' => 'password',
    '#title' => t('Password'),
    '#description' => t('Enter the password that accompanies your username.'),
    '#required' => TRUE,
  );
  $form['#validate'] = user_login_default_validators();
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Log in'));

$ form数组在呈现之前通过引用传递给hook_form_alter(),允许自定义。所以,假设您想要将用户名的文本字段标签从“用户名”更改为“用户名”,您可以写

$form['name']['#title'] = t("Name of the User");

在您的自定义代码中。如果要在表单中添加另一个字段(例如textarea),则执行

$form['otherfield'] = array(
  '#title' => t('My new custom textarea'),
  '#type' => 'textarea',
  '#description' => t("A description of what this area is for"),
  '#cols' => 10,
  '#rows' => 3,
  '#weight' => 20,
);

和Drupal会将该字段添加到用户登录表单中。

您可以通过这种方式自定义许多不同类型的字段和属性:我建议您完全阅读Form API文档。通过这种方式,您可以让Drupal处理表单生成,翻译,呈现,验证和提交,还允许其他模块在需要时操作您的表单。

我希望很清楚,祝你有个美好的一天。

答案 1 :(得分:1)

在template.php中使用它

function themename_theme() {
  $items = array();
  $items['user_login'] = array(
    'render element' => 'form',
    'path' => drupal_get_path('theme', 'corporateclean') . '/templates',
    'template' => 'user-login',

  );

并创建一个模板文件夹,并在其中创建一个文件user-login.tpl.php,在这个文件中你可以把你的html和自定义drupal登录