Last updated 2 October 2011. Created on 15 May 2010.
Edited by JuliaKM, figaro, arianek, dman. Log in to edit this page.

Drupal has many forms that provide off the shelf functionality, one of these is the login block. Below is code for a mini-module that will provide a second login block that has a different validation rule to the standard login block. It will also allow us to display both the new block and the standard user login block on the same page.

It may be possible to display the block more than once, with the Multiblock module. As there is going to be more manipulation of the block than the Multiblock module can provide, this code uses hook_block() instead.

The code below will create another block that can be enabled and placed like any other block on the block administration page.

 * Implementation of hook_block()
function MODULENAME_block($op = 'list', $delta = 'custom_login', $edit = array()) {
  $blocks['custom_login'] = array(
    'info' => t('Custom Login'),
    'form_id' => 'custom_login_block',
  switch($op) {
    case 'list':
      return $blocks;
    case 'view':
      $block['subject'] = $blocks[$delta]['info'];
      $block['content'] = drupal_get_form($blocks[$delta]['form_id']);
      return $block;  

(More on hook_block)

If using this function, change MODULENAME to the name of the module being developed. Also, leave out the closing ?> tag.

The block won't draw anything yet, as when drupal_get_form() is called it is called as drupal_get_form('custom_login_block'). Drupal will then look for a function called 'custom_login_block' which has not been defined. To get around this, hook_forms() (note, not hook_form) is used to tell Drupal what to do if drupal_get_form('custom_login_block') is called.

 * Implementation of hook_forms()
function MODULENAME_forms($form_id, $args) {
  $forms['custom_login_block']['callback'] = 'user_login_block';
  return $forms;

Normally to change the validation rules of a form, a module developer will use hook_form_alter() and switch on the $form_id which identifies a particular form. With the two functions in place above the developer can now use the code below and then create a _MODULENAME_custom_login_validate() function to perform whatever validation is required. Note the use of array_unshift() to insert the validation rule before any other validation rules. This is required to get the custom validation rule to fire before the normal login validation functions.

 * Implementation of hook_form_alter()
function MODULENAME_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'custom_login_block':
      array_unshift($form['#validate'], '_MODULENAME_custom_login_validate');
      // $form['#validate'][] = '_MODULENAME_custom_login_validate';

Putting it all together

The above three functions can be put into your own module with appropriate re-naming, or used to make a new stand-alone module.
To makes a new one, make a new file called MODULENAME.module. Also create an info file called in the same folder.

Looking for support? Visit the forums, or join #drupal-support in IRC.