Skip to content
Snippets Groups Projects
Commit 80b0e5bd authored by Joshua Sedler's avatar Joshua Sedler :cartwheel_tone2:
Browse files

Merge branch '3441513-meta-introduce-new' into '2.x'

Draft: Issue #3441513 by Grevil: [Meta] Introduce new 2.x branch, which refactors, cleans and adjust existing 8.x-1.x code

See merge request !8
parents b1127729 bb2a6d19
No related branches found
No related tags found
No related merge requests found
Pipeline #151212 passed with warnings
min_char: 1
max_char: 128
disallow_spaces: false
ajax_validation: false
user_label: ''
user_desc: ''
skip_existing_username: false
username_validation:
username_validation.config:
type: config_object
label: 'Username Validation Configuration'
mapping:
min_char:
type: integer
label: 'Minimum username characters'
max_char:
type: integer
label: 'Maximum username characters'
disallow_spaces:
type: boolean
label: 'Disallow spaces'
ajax_validation:
type: boolean
label: 'Enable AJAX validation'
user_label:
type: string
label: 'Overwrite "Username" label'
user_desc:
type: string
label: 'Overwrite "Username" description'
skip_existing_username:
type: boolean
label: 'Skip existing username'
......@@ -8,15 +8,13 @@ use Drupal\Core\Form\FormStateInterface;
/**
* The username_validation config form.
*/
class UserNameValidationConfig extends ConfigFormBase {
class UserNameValidationConfigForm extends ConfigFormBase {
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'username_validation.usernamevalidationconfig',
];
return ['username_validation.config'];
}
/**
......@@ -30,26 +28,11 @@ class UserNameValidationConfig extends ConfigFormBase {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('username_validation.usernamevalidationconfig');
$default_min_char = $config->get('min_char');
$default_max_char = $config->get('max_char');
$default_ajax_validation = $config->get('ajax_validation');
$default_user_label = $config->get('user_label');
$default_user_desc = $config->get('user_desc');
$default_avoid_spaces = $config->get('avoid_spaces');
$default_skip_existing_username = $config->get('skip_existing_username');
$config = $this->config('username_validation.config');
$form['username_validation_rule'] = [
'#type' => 'fieldset',
'#title' => $this->t('Username condition'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#tree' => TRUE,
];
$form['username_validation_config'] = [
'#type' => 'fieldset',
'#title' => $this->t('Username Configuration'),
'#title' => $this->t('Username conditions'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#tree' => TRUE,
......@@ -59,66 +42,73 @@ class UserNameValidationConfig extends ConfigFormBase {
'#type' => 'textarea',
'#default_value' => $config->get('blacklist_char'),
'#title' => $this->t('Blacklist Characters/Words'),
'#description' => '<p>' . $this->t("Comma separated characters or words to avoided while saving username. Ex: !,@,#,$,%,^,&,*,(,),1,2,3,4,5,6,7,8,9,0,have,has,were,aren't.") . '</p>' . '<p>' . $this->t('If any of the blacklisted characters/words found in username ,would return validation error on user save.') . '</p>',
'#description' => '<p>' . $this->t("Comma-separated characters or words that, if included in the username, will fail validation when creating / updating a user.<br> Example: !,@,#,$,%,^,&,*,(,),1,2,3,4,5,6,7,8,9,0,have,has,were,aren't."),
];
$form['username_validation_rule']['disallow_spaces'] = [
'#type' => 'checkbox',
'#title' => $this->t("Disallow spaces"),
'#description' => $this->t('Disallow spaces included in the username'),
'#default_value' => $config->get('disallow_spaces'),
];
$form['username_validation_rule']['min_char'] = [
'#type' => 'textfield',
'#title' => $this->t("Minimum characters"),
'#type' => 'number',
'#title' => $this->t("Minimum username characters"),
'#required' => TRUE,
'#description' => $this->t("Minimum number of characters username should contain"),
'#size' => 12,
'#maxlength' => 3,
'#default_value' => $default_min_char ?? '1',
'#min' => 2,
'#description' => $this->t("The minimum number of characters the username should contain"),
'#default_value' => $config->get('min_char'),
];
$form['username_validation_rule']['max_char'] = [
'#type' => 'textfield',
'#title' => $this->t("Maximum characters"),
'#type' => 'number',
'#title' => $this->t("Maximum username characters"),
'#required' => TRUE,
'#description' => $this->t("Maximum number of characters username should contain"),
'#size' => 12,
'#maxlength' => 3,
'#default_value' => $default_max_char ?? '128',
'#min' => 0,
'#max' => 128,
'#description' => $this->t("The maximum number of characters the username should contain"),
'#default_value' => $config->get('max_char'),
];
$form['username_validation_rule']['avoid_spaces'] = [
$form['username_validation_rule']['ajax_validation'] = [
'#type' => 'checkbox',
'#title' => $this->t("Avoid spaces"),
'#description' => $this->t('Avoid spaces in username'),
'#default_value' => $default_avoid_spaces ?? '',
'#title' => $this->t('Enable AJAX Validation'),
'#description' => $this->t("Adds AJAX validation to the user and user register form."),
'#default_value' => $config->get('ajax_validation'),
];
$form['username_validation_rule']['ajax_validation'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable Ajax Validation'),
'#description' => $this->t("By default ajax will be triggered on 'change' event."),
'#default_value' => $default_ajax_validation ?? '',
$form['username_validation_config'] = [
'#type' => 'fieldset',
'#title' => $this->t('Username Configuration'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#tree' => TRUE,
];
$form['username_validation_config']['user_label'] = [
'#type' => 'textfield',
'#title' => $this->t("Username Label"),
'#description' => $this->t("This value will display instead of username in the registration form"),
'#title' => $this->t('Overwrite "Username" label'),
'#description' => $this->t('This value will be display instead of the "Username" label in the registration form and when editing a profile.'),
'#size' => 60,
'#maxlength' => 255,
'#default_value' => $default_user_label ?? '',
'#default_value' => $config->get('user_label'),
];
$form['username_validation_config']['user_desc'] = [
'#type' => 'textfield',
'#title' => $this->t("Username description"),
'#description' => $this->t("This value will display as username description"),
'#title' => $this->t('Overwrite "Username" description'),
'#description' => $this->t('This value will be display instead of the "Username" description in the registration form and when editing a profile.'),
'#size' => 60,
'#maxlength' => 255,
'#default_value' => $default_user_desc ?? '',
'#default_value' => $config->get('user_desc'),
];
$form['username_validation_config']['skip_existing_username'] = [
'#type' => 'checkbox',
'#title' => $this->t("Skip validation for existing usernames"),
'#description' => $this->t('Skips validation for existing usernames if they are unchanged.'),
'#default_value' => $default_skip_existing_username ?? FALSE,
'#default_value' => $config->get('skip_existing_username'),
];
$form['actions']['reset'] = [
......@@ -138,29 +128,10 @@ class UserNameValidationConfig extends ConfigFormBase {
parent::validateForm($form, $form_state);
$min = $form_state->getValue('username_validation_rule')['min_char'];
$max = $form_state->getValue('username_validation_rule')['max_char'];
// Validate field is numerical.
if (!is_numeric($max)) {
$form_state->setErrorByName('max_char', $this->t('These value should be Numerical'));
}
// Validate field should be between 0 and 128.
if ($max <= 0 || $max > 128) {
$form_state->setErrorByName('max_char', $this->t('These value should be between 0 and 128'));
}
// Validate field is numerical.
if (!is_numeric($min)) {
$form_state->setErrorByName('min_char', $this->t('These value should be Numerical'));
}
// Validate field is greater than 1.
if ($min < 1) {
$form_state->setErrorByName('min_char', $this->t('These value should be more than 1'));
}
// Validate min is less than max value.
if ($min > $max) {
$form_state->setErrorByName('min_char', $this->t('Minimum length should not be more than Max length'));
$form_state->setErrorByName('username_validation_rule][min_char', $this->t('Minimum length should not be more than Max length'));
}
}
......@@ -169,11 +140,11 @@ class UserNameValidationConfig extends ConfigFormBase {
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$this->config('username_validation.usernamevalidationconfig')
$this->config('username_validation.config')
->set('blacklist_char', $form_state->getValue('username_validation_rule')['blacklist_char'])
->set('min_char', $form_state->getValue('username_validation_rule')['min_char'])
->set('max_char', $form_state->getValue('username_validation_rule')['max_char'])
->set('avoid_spaces', $form_state->getValue('username_validation_rule')['avoid_spaces'])
->set('disallow_spaces', $form_state->getValue('username_validation_rule')['disallow_spaces'])
->set('ajax_validation', $form_state->getValue('username_validation_rule')['ajax_validation'])
->set('user_label', $form_state->getValue('username_validation_config')['user_label'])
->set('user_desc', $form_state->getValue('username_validation_config')['user_desc'])
......@@ -185,7 +156,7 @@ class UserNameValidationConfig extends ConfigFormBase {
* Delete saved configuration on Clear configuration button press.
*/
public function clearConfiguration() {
$this->config('username_validation.usernamevalidationconfig')->delete();
$this->config('username_validation.config')->delete();
}
}
<?php
namespace Drupal\Tests\username_validation\Functional;
use Drupal\Tests\system\Functional\Module\GenericModuleTestBase;
/**
* Generic module test for username_validation.
*
* @group username_validation
*/
class UsernameValidationGenericTest extends GenericModuleTestBase {
/**
* {@inheritDoc}
*/
protected function assertHookHelp(string $module): void {
// Don't do anything here. Just overwrite this useless method, so we do
// don't have to implement hook_help().
}
}
......@@ -15,3 +15,21 @@ function username_validation_update_9101() {
->set('skip_existing_username', FALSE)
->save();
}
/**
* Add "preview" entity view display for xyz paragraph type.
*/
function username_validation_update_9102(&$sandbox) {
$oldConfig = \Drupal::configFactory()->getEditable('username_validation.usernamevalidationconfig');
$newConfig = \Drupal::configFactory()->getEditable('username_validation.config');
$newConfig
->set('min_char', (integer) $oldConfig->get('min_char'))
->set('max_char', (integer) $oldConfig->get('max_char'))
->set('disallow_spaces', !empty($oldConfig->get('avoid_spaces')))
->set('ajax_validation', !empty($oldConfig->get('ajax_validation')))
->set('user_label', $oldConfig->get('user_label'))
->set('user_desc', $oldConfig->get('user_desc'))
->set('skip_existing_username', $oldConfig->get('skip_existing_username'))
->save();
$oldConfig->delete();
}
......@@ -28,7 +28,7 @@ function username_validation_help($route_name, RouteMatchInterface $route_match)
* Implements hook_form_alter().
*/
function username_validation_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$config = \Drupal::config('username_validation.usernamevalidationconfig');
$config = \Drupal::config('username_validation.config');
$username_label = $config->get('user_label');
$username_desc = $config->get('user_desc');
$username_ajax_value = $config->get('ajax_validation');
......@@ -80,13 +80,13 @@ function username_validation_get_username_from_db() {
* The ajax response.
*/
function username_validation_ajax(array &$form, FormStateInterface &$form_state) {
$config = \Drupal::config('username_validation.usernamevalidationconfig');
$config = \Drupal::config('username_validation.config');
$username = $form_state->getValue('name');
$label = $form['account']['name']['#title'];
$blacklist_char_value = $config->get('blacklist_char');
$max_char = $config->get('max_char');
$min_char = $config->get('min_char');
$avoid_spaces = $config->get('avoid_spaces');
$disallow_spaces = $config->get('disallow_spaces');
$blacklist_char = $blacklist_char_value ?? '';
$skip_existing_username = $config->get('skip_existing_username');
$name = username_validation_get_username_from_db();
......@@ -128,7 +128,7 @@ function username_validation_ajax(array &$form, FormStateInterface &$form_state)
$ajax_response->addCommand(new HtmlCommand('#username-validation-ajax', $value));
}
if ($avoid_spaces && preg_match('/\s/', $username)) {
if ($disallow_spaces && preg_match('/\s/', $username)) {
$value = '<div class ="messages messages--error">Username contains spaces</div>';
$ajax_response->addCommand(new HtmlCommand('#username-validation-ajax', $value));
}
......@@ -145,13 +145,13 @@ function username_validation_ajax(array &$form, FormStateInterface &$form_state)
* The form state object.
*/
function username_validation_username_validate(array $form, FormStateInterface &$form_state) {
$config = \Drupal::config('username_validation.usernamevalidationconfig');
$config = \Drupal::config('username_validation.config');
$username = $form_state->getValue('name');
$label = $form['account']['name']['#title'];
$blacklist_char = $config->get('blacklist_char');
$max_char = $config->get('max_char');
$min_char = $config->get('min_char');
$avoid_spaces = $config->get('avoid_spaces');
$disallow_spaces = $config->get('disallow_spaces');
$skip_existing_username = $config->get('skip_existing_username');
$name = username_validation_get_username_from_db();
......@@ -178,7 +178,7 @@ function username_validation_username_validate(array $form, FormStateInterface &
}
// Check for spaces.
if ($avoid_spaces && preg_match('/\s/', $username)) {
if ($disallow_spaces && preg_match('/\s/', $username)) {
$form_state->setErrorByName('name', t('name contains spaces'));
}
......
access configuration pages:
description: 'To access configuration page'
title: 'Username validation config page access'
description: 'Enables the user to access the "Username Validation Configuration Form"'
title: 'Username Validation Configuration Form access'
restrict access: TRUE
......@@ -2,7 +2,7 @@
username_validation.username_validation_config:
path: '/admin/config/people/user-name-validation'
defaults:
_form: '\Drupal\username_validation\Form\UserNameValidationConfig'
_form: '\Drupal\username_validation\Form\UserNameValidationConfigForm'
_title: 'Username Validation Configuration'
requirements:
_permission: 'access configuration pages'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment