You are here

focus.module in Autofocus 5

Same filename and directory in other branches
  1. 6 focus.module
  2. 7 focus.module

Simple module that sets focus on the first field in a form.

@author Anders Fajerson (http://perifer.se)

File

focus.module
View source
<?php

/**
 * @file 
 * Simple module that sets focus on the first field in a form.
 * 
 * @author Anders Fajerson (http://perifer.se)
 */

/**
 * Default forms, can be overriden at admin/settings/focus.
 */
define('FOCUS_FORMS', "!content_type!_node_form\n" . "block_box_form\n" . "filter_admin_format_form\n" . "menu_edit_item_form\n" . "menu_edit_menu_form\n" . "search_form\n" . "taxonomy_form_vocabulary\n" . "user_admin_new_role\n" . "user_login\n" . "user_login_block\n" . "user_pass\n" . "user_register\n" . "_locale_string_seek_form\n");

/**
 * Implementation of hook_menu().
 * Settings link.
 */
function focus_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/focus',
      'title' => t('Form field focus'),
      'description' => t('set the input fields.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => 'focus_admin_settings',
      'access' => user_access('administer site configuration'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Settings form.
 */
function focus_admin_settings() {
  $form['focus_forms'] = array(
    '#type' => 'textarea',
    '#title' => t('Forms'),
    '#default_value' => variable_get('focus_forms', FOCUS_FORMS),
    '#rows' => 20,
    '#description' => t('Enter one form_id per line. !content_type! can be used as a wildcard for all content types. The form_id of a form can be found by looking at the HTML source. Look for &lt;input type="hidden" name="form_id" value="search_form"  /&gt;. The value is the form_id, in this example "search_form".'),
  );
  return system_settings_form($form);
}

/**
 * Implementation of hook_form_alter().
 * Add javascript (jQuery) to set focus on the first field in the defined forms. 
 */
function focus_form_alter($form_id, &$form) {
  global $_focus_exit;
  $type = $form['type']['#value'];

  // Get default or user defined forms
  $focus_forms = variable_get('focus_forms', FOCUS_FORMS);

  //Replace user variable !type with the node type
  $focus_forms = strtr($focus_forms, array(
    "!content_type!" => $type,
  ));

  // Create an array
  $focus_forms = preg_split("/[\\s,]+/", $focus_forms);

  // If thera are several forms on the same page, only add the javascript for the first one.
  // Don't set focus on edit forms as it can interrupt with user scrolling the page.
  if (!$_focus_exit && arg(2) != 'edit') {
    foreach ($focus_forms as $focus_form) {

      // Check if this form is listed to have focus.
      if ($focus_form == $form_id) {

        // Add the javascript for the first input field, not including hidden input fields.
        $jquery_snippet = '$(document).ready(function(){$("#' . $form['#id'] . ' input").not("[@type=\'hidden\']")[0].focus()});';
        drupal_add_js($jquery_snippet, 'inline');

        // Don't add javascrip for another form.
        $_focus_exit = true;
      }
    }
  }
}

Functions

Namesort descending Description
focus_admin_settings Settings form.
focus_form_alter Implementation of hook_form_alter(). Add javascript (jQuery) to set focus on the first field in the defined forms.
focus_menu Implementation of hook_menu(). Settings link.

Constants

Namesort descending Description
FOCUS_FORMS Default forms, can be overriden at admin/settings/focus.