View source
<?php
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_exposed_form\n" . "views_ui_add_form\n");
function focus_menu() {
$items['admin/config/user-interface/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;
}
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);
}
function focus_form_alter(&$form, &$form_state, $form_id) {
static $add_js = TRUE;
$focus_forms = variable_get('focus_forms', FOCUS_FORMS);
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)) {
if ($form_id == 'user_login') {
$selector = '#edit-name';
}
else {
$selector = '#' . $form['#id'] . ' :input:visible:enabled:first';
}
$jquery_snippet = 'jQuery(document).ready(function(){var i = jQuery("' . $selector . '");if (i.val() == "") {i[0].focus();}});';
drupal_add_js($jquery_snippet, 'inline');
$add_js = FALSE;
}
}