Posted by doublejosh on September 2, 2010 at 10:11am
1 follower
Jump to:
| Project: | Known User Role |
| Version: | 6.x-1.3 |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Issue Summary
Would be really nice to offer a super easy way to login for know users by just presentting the password field.
Insert the login form into the with the username/email field hidden and the same text showing as does now "Hello Username". The "not you?" text would go best under the password field.
Would require moving away from drupal_get_form('user_login_block') though.
Comments
#1
Was able to do it like this...
<?php
/**
* welcome message presented in a theme function so it can
* be easily overridden
*
* @param $known_user
* Object: standard Drupal user object from a user_load()
*
* @return
* HTML string
*/
function theme_known_user_welcome($known_user) {
global $user;
$output = '<div>'.t('Hello %user.', array('%user' => theme('username',$known_user) )).'</div>';
$output .= drupal_get_form('alt_user_login_block');
if (!$user->uid || $user->uid == 0) {
$items = array();
$items[] = l(t('Not you? Click to reset'), 'known-user/reset');
$items[] = l(t('Create new account'), 'user/register', array('attributes' => array('title' => t('Create a new user account.'))));
$items[] = l(t('Forgot password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));
$output .= theme('item_list', $items);
} else {
$output .= l(t('Log out'), 'logout');
}
return $output;
}
/**
* login form presented with username pre-filled
*
* @return
* HTML string
*/
function alt_user_login_block() {
$form = array(
'#action' => url($_GET['q'], array('query' => drupal_get_destination())),
'#id' => 'user-login-form',
'#validate' => user_login_default_validators(),
'#submit' => array('user_login_submit'),
);
$form['name'] = array('#type' => 'hidden',
'#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'),
);
return $form;
}
?>
Great outcome in my opinion.
#2
Whops forgot to set the default value for the username field.
Still having trouble with...
#1) Passing the $known_user object to the alt_login_block function.
#2) Setting a hidden field for the username.
<?php
function theme_known_user_welcome($known_user) {
global $user;
$output = '<div>'.t('Hello %user.', array('%user' => theme('username',$known_user) )).'</div>';
// This won't seem to pass through...
$output .= drupal_get_form('alt_user_login_block',$known_user);
if (!$user->uid || $user->uid == 0) {
$items = array();
$items[] = l(t('Not you? Click to reset'), 'known-user/reset');
$items[] = l(t('Create new account'), 'user/register', array('attributes' => array('title' => t('Create a new user account.'))));
$items[] = l(t('Forgot password'), 'user/password', array('attributes' => array('title' => t('Request new password via e-mail.'))));
$output .= theme('item_list', $items);
} else {
$output .= l(t('Log out'), 'logout');
}
return $output;
}
function alt_user_login_block(&$known_user) {
$form = array(
'#action' => url($_GET['q'], array('query' => drupal_get_destination())),
'#id' => 'user-login-form',
'#validate' => user_login_default_validators(),
'#submit' => array('user_login_submit'),
);
// Keeping it shown until I can get the value pulled through...
//$form['name'] = array('#type' => 'value', '#value' => $known_user->name);
$form['name'] = array('#type' => 'textfield',
'#title' => t('Username'),
'#maxlength' => USERNAME_MAX_LENGTH,
'#size' => 15,
'#default_value' => $known_user->name,
'#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'),
);
return $form;
}
?>