Skip to content

Commit 3cc8a61

Browse files
author
Nathaniel Catchpole
committed
Issue #3015752 by alexpott, bnjmnm: Properly deprecate USERNAME_MAX_LENGTH and USER_REGISTER_* constants
1 parent 140e97e commit 3cc8a61

File tree

20 files changed

+63
-69
lines changed

20 files changed

+63
-69
lines changed

core/lib/Drupal/Core/Installer/Form/SiteConfigureForm.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Drupal\Core\Site\Settings;
1010
use Drupal\Core\State\StateInterface;
1111
use Drupal\user\UserStorageInterface;
12+
use Drupal\user\UserInterface;
1213
use Symfony\Component\DependencyInjection\ContainerInterface;
1314

1415
/**
@@ -178,7 +179,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
178179
$form['admin_account']['account']['name'] = [
179180
'#type' => 'textfield',
180181
'#title' => $this->t('Username'),
181-
'#maxlength' => USERNAME_MAX_LENGTH,
182+
'#maxlength' => UserInterface::USERNAME_MAX_LENGTH,
182183
'#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
183184
'#required' => TRUE,
184185
'#attributes' => ['class' => ['username']],

core/modules/comment/src/CommentManager.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Drupal\field\Entity\FieldStorageConfig;
1818
use Drupal\field\Entity\FieldConfig;
1919
use Drupal\user\RoleInterface;
20+
use Drupal\user\UserInterface;
2021

2122
/**
2223
* Comment manager contains common functions to manage comment fields.
@@ -174,7 +175,7 @@ public function forbiddenMessage(EntityInterface $entity, $field_name) {
174175
$destination = ['destination' => $entity->toUrl('canonical', ['fragment' => 'comment-form'])->toString()];
175176
}
176177

177-
if ($this->userConfig->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
178+
if ($this->userConfig->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
178179
// Users can register themselves.
179180
return $this->t('<a href=":login">Log in</a> or <a href=":register">register</a> to post comments', [
180181
':login' => Url::fromRoute('user.login', [], ['query' => $destination])->toString(),

core/modules/user/src/Access/RegisterAccessCheck.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Drupal\Core\Access\AccessResult;
66
use Drupal\Core\Routing\Access\AccessInterface;
77
use Drupal\Core\Session\AccountInterface;
8+
use Drupal\user\UserInterface;
89

910
/**
1011
* Access check for user registration routes.
@@ -22,7 +23,7 @@ class RegisterAccessCheck implements AccessInterface {
2223
*/
2324
public function access(AccountInterface $account) {
2425
$user_settings = \Drupal::config('user.settings');
25-
return AccessResult::allowedIf($account->isAnonymous() && $user_settings->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY)->cacheUntilConfigurationChanges($user_settings);
26+
return AccessResult::allowedIf($account->isAnonymous() && $user_settings->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY)->cacheUntilConfigurationChanges($user_settings);
2627
}
2728

2829
}

core/modules/user/src/AccountForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public function form(array $form, FormStateInterface $form_state) {
104104
$form['account']['name'] = [
105105
'#type' => 'textfield',
106106
'#title' => $this->t('Username'),
107-
'#maxlength' => USERNAME_MAX_LENGTH,
107+
'#maxlength' => UserInterface::USERNAME_MAX_LENGTH,
108108
'#description' => $this->t("Several special characters are allowed, including space, period (.), hyphen (-), apostrophe ('), underscore (_), and the @ sign."),
109109
'#required' => TRUE,
110110
'#attributes' => [
@@ -184,7 +184,7 @@ public function form(array $form, FormStateInterface $form_state) {
184184
$status = $account->get('status')->value;
185185
}
186186
else {
187-
$status = $config->get('register') == USER_REGISTER_VISITORS ? 1 : 0;
187+
$status = $config->get('register') == UserInterface::REGISTER_VISITORS ? 1 : 0;
188188
}
189189

190190
$form['account']['status'] = [

core/modules/user/src/AccountSettingsForm.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,9 @@ public function buildForm(array $form, FormStateInterface $form_state) {
151151
'#title' => $this->t('Who can register accounts?'),
152152
'#default_value' => $config->get('register'),
153153
'#options' => [
154-
USER_REGISTER_ADMINISTRATORS_ONLY => $this->t('Administrators only'),
155-
USER_REGISTER_VISITORS => $this->t('Visitors'),
156-
USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => $this->t('Visitors, but administrator approval is required'),
154+
UserInterface::REGISTER_ADMINISTRATORS_ONLY => $this->t('Administrators only'),
155+
UserInterface::REGISTER_VISITORS => $this->t('Visitors'),
156+
UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL => $this->t('Visitors, but administrator approval is required'),
157157
],
158158
];
159159
$form['registration_cancellation']['user_email_verification'] = [
@@ -203,7 +203,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
203203
$form['email_admin_created'] = [
204204
'#type' => 'details',
205205
'#title' => $this->t('Welcome (new user created by administrator)'),
206-
'#open' => $config->get('register') == USER_REGISTER_ADMINISTRATORS_ONLY,
206+
'#open' => $config->get('register') == UserInterface::REGISTER_ADMINISTRATORS_ONLY,
207207
'#description' => $this->t('Edit the welcome email messages sent to new member accounts created by an administrator.') . ' ' . $email_token_help,
208208
'#group' => 'email',
209209
];
@@ -223,7 +223,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
223223
$form['email_pending_approval'] = [
224224
'#type' => 'details',
225225
'#title' => $this->t('Welcome (awaiting approval)'),
226-
'#open' => $config->get('register') == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
226+
'#open' => $config->get('register') == UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
227227
'#description' => $this->t('Edit the welcome email messages sent to new members upon registering, when administrative approval is required.') . ' ' . $email_token_help,
228228
'#group' => 'email',
229229
];
@@ -243,7 +243,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
243243
$form['email_pending_approval_admin'] = [
244244
'#type' => 'details',
245245
'#title' => $this->t('Admin (user awaiting approval)'),
246-
'#open' => $config->get('register') == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
246+
'#open' => $config->get('register') == UserInterface::REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL,
247247
'#description' => $this->t('Edit the email notifying the site administrator that there are new members awaiting administrative approval.') . ' ' . $email_token_help,
248248
'#group' => 'email',
249249
];
@@ -263,7 +263,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
263263
$form['email_no_approval_required'] = [
264264
'#type' => 'details',
265265
'#title' => $this->t('Welcome (no approval required)'),
266-
'#open' => $config->get('register') == USER_REGISTER_VISITORS,
266+
'#open' => $config->get('register') == UserInterface::REGISTER_VISITORS,
267267
'#description' => $this->t('Edit the welcome email messages sent to new members upon registering, when no administrator approval is required.') . ' ' . $email_token_help,
268268
'#group' => 'email',
269269
];

core/modules/user/src/Form/UserLoginForm.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Drupal\Core\Form\FormStateInterface;
88
use Drupal\Core\Render\RendererInterface;
99
use Drupal\user\UserAuthInterface;
10+
use Drupal\user\UserInterface;
1011
use Drupal\user\UserStorageInterface;
1112
use Symfony\Component\DependencyInjection\ContainerInterface;
1213

@@ -94,7 +95,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
9495
'#type' => 'textfield',
9596
'#title' => $this->t('Username'),
9697
'#size' => 60,
97-
'#maxlength' => USERNAME_MAX_LENGTH,
98+
'#maxlength' => UserInterface::USERNAME_MAX_LENGTH,
9899
'#description' => $this->t('Enter your @s username.', ['@s' => $config->get('name')]),
99100
'#required' => TRUE,
100101
'#attributes' => [

core/modules/user/src/Form/UserPasswordForm.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Drupal\Core\Form\FormStateInterface;
77
use Drupal\Core\Language\LanguageManagerInterface;
88
use Drupal\Core\Render\Element\Email;
9+
use Drupal\user\UserInterface;
910
use Drupal\user\UserStorageInterface;
1011
use Symfony\Component\DependencyInjection\ContainerInterface;
1112

@@ -68,7 +69,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
6869
'#type' => 'textfield',
6970
'#title' => $this->t('Username or email address'),
7071
'#size' => 60,
71-
'#maxlength' => max(USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
72+
'#maxlength' => max(UserInterface::USERNAME_MAX_LENGTH, Email::EMAIL_MAX_LENGTH),
7273
'#required' => TRUE,
7374
'#attributes' => [
7475
'autocorrect' => 'off',

core/modules/user/src/Plugin/Block/UserLoginBlock.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Drupal\Core\Url;
1010
use Drupal\Core\Session\AccountInterface;
1111
use Drupal\Core\Block\BlockBase;
12+
use Drupal\user\UserInterface;
1213
use Symfony\Component\DependencyInjection\ContainerInterface;
1314

1415
/**
@@ -111,7 +112,7 @@ public function build() {
111112

112113
// Build action links.
113114
$items = [];
114-
if (\Drupal::config('user.settings')->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
115+
if (\Drupal::config('user.settings')->get('register') != UserInterface::REGISTER_ADMINISTRATORS_ONLY) {
115116
$items['create_account'] = [
116117
'#type' => 'link',
117118
'#title' => $this->t('Create new account'),

core/modules/user/src/Plugin/Validation/Constraint/UserNameConstraintValidator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Drupal\user\Plugin\Validation\Constraint;
44

5+
use Drupal\user\UserInterface;
56
use Symfony\Component\Validator\Constraint;
67
use Symfony\Component\Validator\ConstraintValidator;
78

@@ -52,8 +53,8 @@ public function validate($items, Constraint $constraint) {
5253
) {
5354
$this->context->addViolation($constraint->illegalMessage);
5455
}
55-
if (mb_strlen($name) > USERNAME_MAX_LENGTH) {
56-
$this->context->addViolation($constraint->tooLongMessage, ['%name' => $name, '%max' => USERNAME_MAX_LENGTH]);
56+
if (mb_strlen($name) > UserInterface::USERNAME_MAX_LENGTH) {
57+
$this->context->addViolation($constraint->tooLongMessage, ['%name' => $name, '%max' => UserInterface::USERNAME_MAX_LENGTH]);
5758
}
5859
}
5960

core/modules/user/src/Plugin/migrate/destination/EntityUser.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Drupal\migrate\Plugin\MigrationInterface;
1212
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
1313
use Drupal\migrate\Row;
14+
use Drupal\user\UserInterface;
1415
use Symfony\Component\DependencyInjection\ContainerInterface;
1516

1617
/**
@@ -167,8 +168,8 @@ protected function processStubRow(Row $row) {
167168
if (is_array($name)) {
168169
$name = reset($name);
169170
}
170-
if (mb_strlen($name) > USERNAME_MAX_LENGTH) {
171-
$row->setDestinationProperty('name', mb_substr($name, 0, USERNAME_MAX_LENGTH));
171+
if (mb_strlen($name) > UserInterface::USERNAME_MAX_LENGTH) {
172+
$row->setDestinationProperty('name', mb_substr($name, 0, UserInterface::USERNAME_MAX_LENGTH));
172173
}
173174
}
174175

0 commit comments

Comments
 (0)