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

doublejosh’s picture

Was able to do it like this...

/**
 * 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.

doublejosh’s picture

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.

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;
}