Skip to content

Commit d8be6a1

Browse files
avsdev-cwlcharette
authored andcommitted
Hide locale field when only 1 locale is available (#968) (#969)
1 parent d312d1f commit d8be6a1

4 files changed

Lines changed: 62 additions & 0 deletions

File tree

app/sprinkles/account/src/Controller/AccountController.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,22 @@ public function pageRegister(Request $request, Response $response, $args)
509509
// Get locale information
510510
$currentLocales = $localePathBuilder->getLocales();
511511

512+
// Hide the locale field if there is only 1 locale available
513+
$fields = [
514+
'hidden' => [],
515+
'disabled' => []
516+
];
517+
if (count($config->getDefined('site.locales.available')) <= 1) {
518+
$fields['hidden'][] = 'locale';
519+
}
520+
512521
return $this->ci->view->render($response, 'pages/register.html.twig', [
513522
'page' => [
514523
'validators' => [
515524
'register' => $validatorRegister->rules('json', false)
516525
]
517526
],
527+
'fields' => $fields,
518528
'locales' => [
519529
'available' => $config['site.locales.available'],
520530
'current' => end($currentLocales)
@@ -658,8 +668,18 @@ public function pageSettings(Request $request, Response $response, $args)
658668
// Get a list of all locales
659669
$locales = $config->getDefined('site.locales.available');
660670

671+
// Hide the locale field if there is only 1 locale available
672+
$fields = [
673+
'hidden' => [],
674+
'disabled' => []
675+
];
676+
if (count($config->getDefined('site.locales.available')) <= 1) {
677+
$fields['hidden'][] = 'locale';
678+
}
679+
661680
return $this->ci->view->render($response, 'pages/account-settings.html.twig', [
662681
'locales' => $locales,
682+
'fields' => $fields,
663683
'page' => [
664684
'validators' => [
665685
'account_settings' => $validatorAccountSettings->rules('json', false),
@@ -766,6 +786,11 @@ public function profile(Request $request, Response $response, $args)
766786

767787
$error = false;
768788

789+
// Ensure that in the case of using a single locale, that the locale is set
790+
if (count($config->getDefined('site.locales.available')) <= 1) {
791+
$data['locale'] = $currentUser->locale;
792+
}
793+
769794
// Validate, and halt on validation errors.
770795
$validator = new ServerSideValidator($schema, $this->ci->translator);
771796
if (!$validator->validate($data)) {
@@ -877,6 +902,11 @@ public function register(Request $request, Response $response, $args)
877902

878903
$error = false;
879904

905+
// Ensure that in the case of using a single locale, that the locale is set
906+
if (count($config->getDefined('site.locales.available')) <= 1) {
907+
$data['locale'] = $config['site.registration.user_defaults.locale'];
908+
}
909+
880910
// Validate request data
881911
$validator = new ServerSideValidator($schema, $this->ci->translator);
882912
if (!$validator->validate($data)) {
@@ -1163,6 +1193,11 @@ public function settings(Request $request, Response $response, $args)
11631193

11641194
$error = false;
11651195

1196+
// Ensure that in the case of using a single locale, that the locale is set
1197+
if (count($config->getDefined('site.locales.available')) <= 1) {
1198+
$data['locale'] = $currentUser->locale;
1199+
}
1200+
11661201
// Validate, and halt on validation errors.
11671202
$validator = new ServerSideValidator($schema, $this->ci->translator);
11681203
if (!$validator->validate($data)) {

app/sprinkles/account/templates/forms/settings-profile.html.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
</div>
2121
</div>
2222

23+
{% if 'locale' not in fields.hidden %}
2324
<div class="form-group">
2425
<label for="input-locale" class="control-label">{{translate("LOCALE")}}</label>
2526
<select id="input-locale" class="form-control js-select2" name="locale" {{page.visibility}}>
@@ -31,6 +32,7 @@
3132
</select>
3233
<p class="help-block">{{translate("LOCALE.ACCOUNT")}}.</p>
3334
</div>
35+
{% endif %}
3436
{% endblock %}
3537
</div>
3638
<div class="box-footer text-center">

app/sprinkles/account/templates/pages/register.html.twig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<label class="sr-only" for="r-form-passwordc">{{translate('PASSWORD.CONFIRM')}}</label>
5454
<input type="password" name="passwordc" placeholder="{{translate('PASSWORD.CONFIRM')}}" class="form-control" id="r-form-passwordc">
5555
</div>
56+
{% if 'locale' not in fields.hidden %}
5657
<div class="form-group">
5758
<label for="r-form-locale" class="control-label">{{translate("LOCALE")}}</label>
5859
<select id="r-form-locale" class="form-control js-select2" name="locale">
@@ -64,6 +65,7 @@
6465
</select>
6566
<p class="help-block">{{translate("LOCALE.ACCOUNT")}}.</p>
6667
</div>
68+
{% endif %}
6769
{% if site.registration.captcha %}
6870
<div class="form-group">
6971
<label class="sr-only" for="r-form-passwordc">{{translate('CAPTCHA.VERIFY')}}</label>

app/sprinkles/admin/src/Controller/UserController.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public function create(Request $request, Response $response, $args)
6060
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
6161
$currentUser = $this->ci->currentUser;
6262

63+
/** @var \UserFrosting\Support\Repository\Repository $config */
64+
$config = $this->ci->config;
65+
6366
// Access-controlled page
6467
if (!$authorizer->checkAccess($currentUser, 'create_user')) {
6568
throw new ForbiddenException();
@@ -77,6 +80,11 @@ public function create(Request $request, Response $response, $args)
7780

7881
$error = false;
7982

83+
// Ensure that in the case of using a single locale, that the locale is set bu inheriting from current user
84+
if (count($config->getDefined('site.locales.available')) <= 1) {
85+
$data['locale'] = $currentUser->locale;
86+
}
87+
8088
// Validate request data
8189
$validator = new ServerSideValidator($schema, $this->ci->translator);
8290
if (!$validator->validate($data)) {
@@ -573,6 +581,11 @@ public function getModalCreate(Request $request, Response $response, $args)
573581
$fields['disabled'][] = 'group';
574582
}
575583

584+
// Hide the locale field if there is only 1 locale available
585+
if (count($config->getDefined('site.locales.available')) <= 1) {
586+
$fields['hidden'][] = 'locale';
587+
}
588+
576589
// Create a dummy user to prepopulate fields
577590
$data = [
578591
'group_id' => $currentUser->group_id,
@@ -673,6 +686,11 @@ public function getModalEdit(Request $request, Response $response, $args)
673686
$fields['disabled'][] = 'group';
674687
}
675688

689+
// Hide the locale field if there is only 1 locale available
690+
if (count($config->getDefined('site.locales.available')) <= 1) {
691+
$fields['hidden'][] = 'locale';
692+
}
693+
676694
// Load validation rules
677695
$schema = new RequestSchema('schema://requests/user/edit-info.yaml');
678696
$validator = new JqueryValidationAdapter($schema, $this->ci->translator);
@@ -950,6 +968,11 @@ public function pageInfo(Request $request, Response $response, $args)
950968
}
951969
}
952970

971+
// Hide the locale field if there is only 1 locale available
972+
if (count($config->getDefined('site.locales.available')) <= 1) {
973+
$fields['hidden'][] = 'locale';
974+
}
975+
953976
// Determine buttons to display
954977
$editButtons = [
955978
'hidden' => []

0 commit comments

Comments
 (0)