focus.module in Autofocus 6
Same filename and directory in other branches
Simple module that sets focus on the first field in a form.
@author Anders Fajerson (http://perifer.se)
File
focus.moduleView 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', "aggregator_form_category\n" . "aggregator_form_feed\n" . "!content_type!_node_form\n" . "block_add_block_form\n" . "filter_admin_format_form\n" . "forum_form_container\n" . "forum_form_forum\n" . "locale_translate_seek_form\n" . "menu_edit_menu\n" . "menu_edit_item\n" . "node_type_form\n" . "path_admin_form\n" . "profile_field_form\n" . "search_form\n" . "taxonomy_form_vocabulary\n" . "taxonomy_form_term\n" . "user_admin_new_role\n" . "user_admin_role\n" . "user_login\n" . "user_pass\n" . "user_register\n" . "views_ui_add_form\n");
/**
* Implementation of hook_menu().
*/
function focus_menu() {
$items['admin/settings/focus'] = array(
'title' => 'Form field focus',
'description' => 'Set the input fields.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'focus_admin_settings',
),
'access arguments' => array(
'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 <input type="hidden" name="form_id" value="search_form" />. 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, &$form_state, $form_id) {
static $add_js = TRUE;
// Get defined forms.
$focus_forms = variable_get('focus_forms', FOCUS_FORMS);
// Replace !content_type! with the current node type.
if (isset($form['type']['#value'])) {
$focus_forms = strtr($focus_forms, array(
"!content_type!" => $form['type']['#value'],
));
}
$focus_forms = preg_split("/[\\s,]+/", $focus_forms);
if ($add_js && in_array($form_id, $focus_forms)) {
// Treat the user login form as a special case (otherwise
// OpenID, if enabled, will break it).
if ($form_id == 'user_login') {
$selector = '#edit-name';
}
else {
$selector = '#' . $form['#id'] . ' :input:visible:enabled:first';
}
$jquery_snippet = '$(document).ready(function(){var i = $("' . $selector . '");if (i.val() == "") {i[0].focus();}});';
drupal_add_js($jquery_snippet, 'inline');
// Don't add javascrip for another form.
$add_js = FALSE;
}
}
Functions
Name![]() |
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(). |
Constants
Name![]() |
Description |
---|---|
FOCUS_FORMS | Default forms, can be overriden at admin/settings/focus. |