Commit c33a383c authored by git's avatar git Committed by wundo

Issue #2444343 by Teastwood, Chris Matthews: Configurable captcha_reponse...

Issue #2444343 by Teastwood, Chris Matthews: Configurable captcha_reponse error message in favor of other Captcha modules
parent 12988a05
......@@ -44,6 +44,11 @@ function captcha_admin_settings() {
// Use JavaScript for some added usability on admin form.
drupal_add_js(drupal_get_path('module', 'captcha') . '/captcha.js');
// Load languages for configurable texts.
if (module_exists('locale')) {
$langs = locale_language_list();
}
// Configuration of which forms to protect, with what challenge.
$form['captcha_form_protection'] = array(
'#type' => 'fieldset',
......@@ -150,6 +155,32 @@ function captcha_admin_settings() {
'#submit' => array('captcha_clear_captcha_placement_cache_submit'),
);
// Textfield(s) for editing the error message.
if (module_exists('locale')) {
$form['captcha_error_messages'] = array(
'#type' => 'fieldset',
'#title' => t('Error message'),
'#description' => t('Message displayed when the CAPTCHA has not been solved. An empty entry will reset the error message to default.'),
);
foreach ($langs as $lang_code => $lang_name) {
$form['captcha_error_messages']['captcha_error_message_' . $lang_code] = array(
'#type' => 'textfield',
'#title' => t('For language %lang_name (code %lang_code)', array('%lang_name' => $lang_name, '%lang_code' => $lang_code)),
'#default_value' => _captcha_get_error_message($lang_code),
'#maxlength' => 256,
);
}
}
else {
$form['captcha_error_message'] = array(
'#type' => 'textfield',
'#title' => t('Error message'),
'#description' => t('Message displayed when the CAPTCHA has not been solved. An empty entry will reset the error message to default.'),
'#default_value' => _captcha_get_error_message(),
'#maxlength' => 256,
);
}
// Configuration option for adding a CAPTCHA description.
$form['captcha_add_captcha_description'] = array(
'#type' => 'checkbox',
......@@ -159,7 +190,6 @@ function captcha_admin_settings() {
);
// Textfield(s) for the CAPTCHA description.
if (module_exists('locale')) {
$langs = locale_language_list();
$form['captcha_descriptions'] = array(
'#type' => 'fieldset',
'#title' => t('CAPTCHA description'),
......@@ -286,6 +316,11 @@ function captcha_admin_settings_submit($form, &$form_state) {
variable_set('captcha_default_challenge', $form_state['values']['captcha_default_challenge']);
variable_set('captcha_default_challenge_on_nonlisted_forms', $form_state['values']['captcha_default_challenge_on_nonlisted_forms']);
// Load languages for configurable texts.
if (module_exists('locale')) {
$langs = locale_language_list();
}
// Process CAPTCHA points.
if (isset($form_state['values']['captcha_form_id_overview']['captcha_captcha_points'])) {
// Load existing data.
......@@ -320,11 +355,34 @@ function captcha_admin_settings_submit($form, &$form_state) {
drupal_set_message(t('Added CAPTCHA point.'), 'status');
}
// Error message.
if (module_exists('locale')) {
foreach ($langs as $lang_code => $lang_name) {
$description = $form_state['values']['captcha_error_message_' . $lang_code];
if ($description) {
variable_set('captcha_error_message_' . $lang_code, $description);
}
else {
variable_del('captcha_error_message_' . $lang_code);
drupal_set_message(t('Reset of error message for language %language.', array('%language' => $lang_name)), 'status');
}
}
}
else {
$description = $form_state['values']['captcha_error_message'];
if ($description) {
variable_set('captcha_error_message', $description);
}
else {
variable_del('captcha_error_message');
drupal_set_message(t('Reset of error message.'), 'status');
}
}
// CAPTCHA description stuff.
variable_set('captcha_add_captcha_description', $form_state['values']['captcha_add_captcha_description']);
// Save (or reset) the CAPTCHA descriptions.
if (module_exists('locale')) {
$langs = locale_language_list();
foreach ($langs as $lang_code => $lang_name) {
$description = $form_state['values']["captcha_description_$lang_code"];
if ($description) {
......
......@@ -266,6 +266,35 @@ function _captcha_get_description($lang_code = NULL) {
return filter_xss_admin($description);
}
/**
* Get the error message as configured on the general CAPTCHA settings page.
*
* If the locale module is enabled, the error message will be returned
* for the current language the page is rendered for. This language
* can optionally been overridden with the $lang_code argument.
*
* @param string|null $lang_code
* an optional language code to get the description for.
*
* @return string
* String with (localized) error message.
*/
function _captcha_get_error_message($lang_code = NULL) {
// If no language code is given: use the language of the current page.
global $language;
$lang_code = isset($lang_code) ? $lang_code : $language->language;
// The hardcoded but localizable default.
$default = t('The answer you entered for the CAPTCHA was not correct.', array(), array('langcode' => $lang_code));
// Look up the configured error message or fall back on the (localized) default.
if (module_exists('locale')) {
$message = variable_get('captcha_error_message_' . $lang_code, $default);
}
else {
$message = variable_get('captcha_error_message', $default);
}
return filter_xss_admin($message);
}
/**
* Parse or interpret the given captcha_type.
*
......
......@@ -752,7 +752,7 @@ function captcha_validate($element, &$form_state) {
->expression('attempts', 'attempts + 1')
->execute();
// Set form error.
form_set_error('captcha_response', t('The answer you entered for the CAPTCHA was not correct.'));
form_set_error('captcha_response', _captcha_get_error_message());
// Update wrong response counter.
if (variable_get('captcha_enable_stats', FALSE)) {
variable_set('captcha_wrong_response_counter', variable_get('captcha_wrong_response_counter', 0) + 1);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment