diff --git a/core/modules/contact/config/contact.settings.yml b/core/modules/contact/config/contact.settings.yml new file mode 100644 index 0000000..b23538a --- /dev/null +++ b/core/modules/contact/config/contact.settings.yml @@ -0,0 +1,4 @@ +flood: + limit: '5' + interval: '3600' +user_default_enabled: '1' diff --git a/core/modules/contact/contact.install b/core/modules/contact/contact.install index 5956740..f956242 100644 --- a/core/modules/contact/contact.install +++ b/core/modules/contact/contact.install @@ -84,10 +84,14 @@ function contact_install() { } /** - * Implements hook_uninstall(). + * Moves contact setting from variable to config. + * + * @ingroup config_upgrade */ -function contact_uninstall() { - variable_del('contact_default_status'); - variable_del('contact_threshold_limit'); - variable_del('contact_threshold_window'); +function contact_update_8000() { + update_variables_to_config('contact.settings', array( + 'contact_default_status' => 'user_default_enabled', + 'contact_threshold_limit' => 'flood.limit', + 'contact_threshold_window' => 'flood.interval', + )); } diff --git a/core/modules/contact/contact.module b/core/modules/contact/contact.module index a6c8aed..044886f 100644 --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -234,7 +234,7 @@ function contact_form_user_profile_form_alter(&$form, &$form_state) { * Implements hook_user_presave(). */ function contact_user_presave($account) { - $account->data['contact'] = isset($account->contact) ? $account->contact : variable_get('contact_default_status', 1); + $account->data['contact'] = isset($account->contact) ? $account->contact : config('contact.settings')->get('user_default_enabled'); } /** @@ -250,10 +250,23 @@ function contact_form_user_admin_settings_alter(&$form, &$form_state) { '#title' => t('Contact settings'), '#weight' => 0, ); - $form['contact']['contact_default_status'] = array( + $form['contact']['user_default_enabled'] = array( '#type' => 'checkbox', '#title' => t('Enable the personal contact form by default for new users.'), '#description' => t('Changing this setting will not affect existing users.'), - '#default_value' => variable_get('contact_default_status', 1), + '#default_value' => config('contact.settings')->get('user_default_enabled'), ); + // Add submit handler to save contact configuration. + $form['#submit'][] = 'contact_form_user_admin_settings_submit'; +} + +/** + * Form builder submit handler; Handle submission for user contact default. + * + * @ingroup forms + */ +function contact_form_user_admin_settings_submit($form, &$form_state) { + config('contact.settings') + ->set('user_default_enabled', $form_state['values']['user_default_enabled']) + ->save(); } diff --git a/core/modules/contact/contact.pages.inc b/core/modules/contact/contact.pages.inc index d20d79a..0866821 100644 --- a/core/modules/contact/contact.pages.inc +++ b/core/modules/contact/contact.pages.inc @@ -20,10 +20,11 @@ function contact_site_form($form, &$form_state) { global $user; // Check if flood control has been activated for sending e-mails. - $limit = variable_get('contact_threshold_limit', 5); - $window = variable_get('contact_threshold_window', 3600); - if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms')) { - drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error'); + $config = config('contact.settings'); + $limit = $config->get('flood.limit'); + $interval = $config->get('flood.interval'); + if (!flood_is_allowed('contact', $limit, $interval) && !user_access('administer contact forms')) { + drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($interval))), 'error'); throw new AccessDeniedHttpException(); } @@ -180,7 +181,7 @@ function contact_site_form_submit($form, &$form_state) { drupal_mail('contact', 'page_autoreply', $from, $language_interface, $values, $to); } - flood_register_event('contact', variable_get('contact_threshold_window', 3600)); + flood_register_event('contact', config('contact.settings')->get('flood.interval')); watchdog('mail', '%sender-name (@sender-from) sent an e-mail regarding %category.', array('%sender-name' => $values['name'], '@sender-from' => $from, '%category' => $values['category']['category'])); // Jump to home page rather than back to contact page to avoid @@ -201,10 +202,11 @@ function contact_personal_form($form, &$form_state, $recipient) { global $user; // Check if flood control has been activated for sending e-mails. - $limit = variable_get('contact_threshold_limit', 5); - $window = variable_get('contact_threshold_window', 3600); - if (!flood_is_allowed('contact', $limit, $window) && !user_access('administer contact forms') && !user_access('administer users')) { - drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($window))), 'error'); + $config = config('contact.settings'); + $limit = $config->get('flood.limit'); + $interval = $config->get('flood.interval'); + if (!flood_is_allowed('contact', $limit, $interval) && !user_access('administer contact forms') && !user_access('administer users')) { + drupal_set_message(t("You cannot send more than %limit messages in @interval. Try again later.", array('%limit' => $limit, '@interval' => format_interval($interval))), 'error'); throw new AccessDeniedHttpException(); } @@ -315,7 +317,7 @@ function contact_personal_form_submit($form, &$form_state) { drupal_mail('contact', 'user_copy', $from, $language_interface, $values, $from); } - flood_register_event('contact', variable_get('contact_threshold_window', 3600)); + flood_register_event('contact', config('contact.settings')->get('flood.interval')); watchdog('mail', '%sender-name (@sender-from) sent %recipient-name an e-mail.', array('%sender-name' => $values['name'], '@sender-from' => $from, '%recipient-name' => $values['recipient']->name)); // Jump to the contacted user's profile page. diff --git a/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php b/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php index 0d5ff96..3dcecb2 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php +++ b/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php @@ -32,7 +32,7 @@ class ContactPersonalTest extends WebTestBase { $this->admin_user = $this->drupalCreateUser(array('administer contact forms', 'administer users')); // Create some normal users with their contact forms enabled by default. - variable_set('contact_default_status', TRUE); + config('contact.settings')->set('user_default_enabled', 1)->save(); $this->web_user = $this->drupalCreateUser(array('access user contact forms')); $this->contact_user = $this->drupalCreateUser(); } @@ -84,7 +84,7 @@ class ContactPersonalTest extends WebTestBase { // Disable the personal contact form. $this->drupalLogin($this->admin_user); - $edit = array('contact_default_status' => FALSE); + $edit = array('user_default_enabled' => FALSE); $this->drupalPost('admin/config/people/accounts', $edit, t('Save configuration')); $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.')); $this->drupalLogout(); @@ -123,7 +123,7 @@ class ContactPersonalTest extends WebTestBase { */ function testPersonalContactFlood() { $flood_limit = 3; - variable_set('contact_threshold_limit', $flood_limit); + config('contact.settings')->set('flood.limit', $flood_limit)->save(); // Clear flood table in preparation for flood test and allow other checks to complete. db_delete('flood')->execute(); @@ -140,7 +140,7 @@ class ContactPersonalTest extends WebTestBase { // Submit contact form one over limit. $this->drupalGet('user/' . $this->contact_user->uid. '/contact'); - $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => $flood_limit, '@interval' => format_interval(variable_get('contact_threshold_window', 3600)))), 'Normal user denied access to flooded contact form.'); + $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => $flood_limit, '@interval' => format_interval(config('contact.settings')->get('flood.interval')))), 'Normal user denied access to flooded contact form.'); // Test that the admin user can still access the contact form even though // the flood limit was reached. diff --git a/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php index 6519267..9a93390 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php +++ b/core/modules/contact/lib/Drupal/contact/Tests/ContactSitewideTest.php @@ -34,12 +34,14 @@ class ContactSitewideTest extends WebTestBase { $this->drupalLogin($admin_user); $flood_limit = 3; - variable_set('contact_threshold_limit', $flood_limit); - variable_set('contact_threshold_window', 600); + config('contact.settings') + ->set('flood.limit', $flood_limit) + ->set('flood.interval', 600) + ->save(); // Set settings. $edit = array(); - $edit['contact_default_status'] = TRUE; + $edit['user_default_enabled'] = TRUE; $this->drupalPost('admin/config/people/accounts', $edit, t('Save configuration')); $this->assertText(t('The configuration options have been saved.'), t('Setting successfully saved.')); @@ -158,7 +160,7 @@ class ContactSitewideTest extends WebTestBase { // Submit contact form one over limit. $this->drupalGet('contact'); $this->assertResponse(403, t('Access denied to anonymous user after reaching message treshold.')); - $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => variable_get('contact_threshold_limit', 3), '@interval' => format_interval(600))), t('Message threshold reached.')); + $this->assertRaw(t('You cannot send more than %number messages in @interval. Try again later.', array('%number' => config('contact.settings')->get('flood.limit'), '@interval' => format_interval(600))), t('Message threshold reached.')); // Delete created categories. $this->drupalLogin($admin_user);