Drupal login form customize

你说的曾经没有我的故事 提交于 2019-12-06 12:19:04

问题


Hi i have used this code

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

    echo $form;
?>

to get the default Drupal login form for my site but I need to customize the HTML, I have found some pages in module/users but did not understand how to customize the structure.


回答1:


use this in template.php

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

  );

and create a template folder and within that create a file user-login.tpl.php and in this file you can put your html and could customize drupal login




回答2:


The user login form for Drupal is built by the user_login function in user.module using Drupal Form API. If you need to customize it, you should do it using hook_form_alter() in your module

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

     // YOUR CUSTOM CODE FOR THE FORM GOES HERE

  }
}

** EDIT, AFTER YOUR COMMENT **

You don't need to call the YOUR_MODULE_NAME_form_alter() function: Drupal does that for you via the hook mechanism everytime it needs to build a form, and, when $form_id=='user_login', it modifies the login form to allow your customization. The way Drupal does that is discussed in detail in drupal.org, just follow the link I wrote at the beginning of this answer.

The user login form is declared this way in 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'));

The $form array is passed by reference to your hook_form_alter() before being rendered, allowing for customization. So, let's say that you want to change the label of the textfield for the user name from "Username" to "Name of the User", you write

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

in your custom code. If you want to add another field to the form (a textarea, for example), you do

$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,
);

and Drupal will add the field to the user login form.

There are many different kind of fields and properties that you can customize this way: I encourage you to fully read the Form API documentation. This way you let Drupal take care of form generation, translation, rendering, validation and submission, also permitting to other modules to manipulate your form if needed.

I hope it's clear, have a good day.



来源:https://stackoverflow.com/questions/20216530/drupal-login-form-customize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!