You are here

no_autocomplete.module in No Autocomplete 6

Same filename and directory in other branches
  1. 8 no_autocomplete.module
  2. 7 no_autocomplete.module

File

no_autocomplete.module
View source
<?php

/**
 * @file
 * no_autocomplete.module
 *
 */

/**
 * This module adds the autocomplete=off attribute to selected key user forms.
 * On a browser that respects this setting, it means that the browser will not
 * try to autocomplete the password on the user login forms, or the whole
 * user edit form.
 *
 * Many browsers, including current Chrome as of this writing and LastPass
 * do not respect this setting. But it can
 * 1. Offer some security on the login form
 * 2. Prevent naive users from getting the browser-fill on the first password
 *    form and not the second, making them angry and confused.
 */

/**
 * Implements hook_menu() to add admin settings form.
 *
 * @return mixed
 */
function no_autocomplete_menu() {
  $items['admin/settings/no_autocomplete'] = array(
    'title' => 'No Autocomplete',
    'description' => 'Turn on autocomplete=off for key user forms',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'no_autocomplete_admin_settings_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_form_alter() to turn off autocomplete on key forms.
 *
 * @param $form
 * @param $form_state
 * @param $form_id
 */
function no_autocomplete_form_alter(&$form, $form_state, $form_id) {

  # Add to the user login form.

  # user_login and user_profile_form
  if (substr($form_id, 0, 10) == 'user_login' && variable_get('no_autocomplete_login_form', FALSE)) {
    if (empty($form['pass']['#attributes'])) {
      $form['pass']['#attributes'] = array();
    }
    $form['pass']['#attributes']['autocomplete'] = 'off';
  }

  # Remove autofill from the user edit form
  if ($form_id == 'user_profile_form' && variable_get('no_autocomplete_profile_form', FALSE)) {
    $form['#attributes']['autocomplete'] = 'off';
  }
}

/**
 * Form to set our options.
 *
 */
function no_autocomplete_admin_settings_form() {
  $form['no_autocomplete_login_form'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use "autocomplete=off" on user login form'),
    '#default_value' => variable_get('no_autocomplete_login_form', FALSE),
  );
  $form['no_autocomplete_profile_form'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use "autocomplete=off" on user profile edit form'),
    '#default_value' => variable_get('no_autocomplete_profile_form', FALSE),
  );
  $form = system_settings_form($form);
  return $form;
}

Functions

Namesort descending Description
no_autocomplete_admin_settings_form Form to set our options.
no_autocomplete_form_alter Implements hook_form_alter() to turn off autocomplete on key forms.
no_autocomplete_menu Implements hook_menu() to add admin settings form.