Wednesday, June 28, 2017 05:08 pm EDT

Alter the output of Logintoboggan on User Registration Form

Bri's picture

Recently I was faced with a problem that gave me a bit of trouble. I was using the Logintoboggan module to create a unified login form as well as to allow logins with username or e-mail address. In addition to using Logintoboggan, I had created a small helper module to remove and alter some of the cruft in the user registration form and the user profile form.

My helper module was using a hook_form_alter to add in an "agree to terms" checkbox on initial account creation, as well as unsetting the username input. We would instead assign the email address to the username field on submission.

Then I saw the output from logintoboggan which read "You may login with either your assigned username or your e-mail address." I decided I wanted to change this since my users never see a username box and are unaware that I have assigned the email address to this field.

I tried, unsuccessfully, to override the output from LT module with a form alter. I even changed the weight of the module to make sure my helper module's form_alter executed after LT's. What I failed to realize is that LT uses a theme function, so my form alter was never going to work. Instead, you must override all of the theme functions you want to change. If you look at the very bottom of the logintoboggan.module file you will see a list of these. Put them in your theme and override them and you will be good to go.

As an example, I copied the following right from the LT .module file:

/**
 * Theme the username title of the user login form
 * and the user login block.
 */
function theme_lt_username_title($variables) {
  switch ($variables['form_id']) {
    case 'user_login':
      // Label text for the username field on the /user/login page.
      return t('Username or e-mail address');
      break;

    case 'user_login_block':
      // Label text for the username field when shown in a block.
      return t('Username or e-mail');
      break;
  }
}

/**
 * Theme the username description of the user login form
 * and the user login block.
 */
function theme_lt_username_description($variables) {
  switch ($variables['form_id']) {
    case 'user_login':
      // The username field's description when shown on the /user/login page.
      return t('You may login with either your assigned username or your e-mail address.');
      break;
    case 'user_login_block':
      return '';
      break;
  }
}

and pasted them in to my new theme, changing the word "theme" to the name of my theme:

/**
 * Theme the username title of the user login form
 * and the user login block. This is an override of what the logintoboggan module defines.
 */
function zurb_foundation_lt_username_title($variables) {
  switch ($variables['form_id']) {
    case 'user_login':
      // Label text for the username field on the /user/login page.
      return t('E-mail address');
      break;

    case 'user_login_block':
      // Label text for the username field when shown in a block.
      return t('E-mail');
      break;
  }
}

/**
 * Theme the username description of the user login form
 * and the user login block. This is an override of what the logintoboggan module defines.
 */
function zurb_foundation_lt_username_description($variables) {
  switch ($variables['form_id']) {
    case 'user_login':
      // The username field's description when shown on the /user/login page.
      return '';
      break;
    case 'user_login_block':
      return '';
      break;
  }
}