Skip to content

Commit

Permalink
Hide locale field when only 1 locale is available (userfrosting#968)
Browse files Browse the repository at this point in the history
  • Loading branch information
avsdev-cw committed Apr 18, 2019
1 parent d312d1f commit 36d57a9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
35 changes: 35 additions & 0 deletions app/sprinkles/account/src/Controller/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,12 +509,22 @@ public function pageRegister(Request $request, Response $response, $args)
// Get locale information
$currentLocales = $localePathBuilder->getLocales();

// Hide the locale field if there is only 1 locale available
$fields = [
'hidden' => [],
'disabled' => []
];
if (count($config->getDefined('site.locales.available')) <= 1) {
$fields['hidden'][] = 'locale';
}

return $this->ci->view->render($response, 'pages/register.html.twig', [
'page' => [
'validators' => [
'register' => $validatorRegister->rules('json', false)
]
],
'fields' => $fields,
'locales' => [
'available' => $config['site.locales.available'],
'current' => end($currentLocales)
Expand Down Expand Up @@ -658,8 +668,18 @@ public function pageSettings(Request $request, Response $response, $args)
// Get a list of all locales
$locales = $config->getDefined('site.locales.available');

// Hide the locale field if there is only 1 locale available
$fields = [
'hidden' => [],
'disabled' => []
];
if (count($config->getDefined('site.locales.available')) <= 1) {
$fields['hidden'][] = 'locale';
}

return $this->ci->view->render($response, 'pages/account-settings.html.twig', [
'locales' => $locales,
'fields' => $fields,
'page' => [
'validators' => [
'account_settings' => $validatorAccountSettings->rules('json', false),
Expand Down Expand Up @@ -766,6 +786,11 @@ public function profile(Request $request, Response $response, $args)

$error = false;

// Ensure that in the case of using a single locale, that the locale is set
if (count($config->getDefined('site.locales.available')) <= 1) {
$data['locale'] = $currentUser->locale;
}

// Validate, and halt on validation errors.
$validator = new ServerSideValidator($schema, $this->ci->translator);
if (!$validator->validate($data)) {
Expand Down Expand Up @@ -877,6 +902,11 @@ public function register(Request $request, Response $response, $args)

$error = false;

// Ensure that in the case of using a single locale, that the locale is set
if (count($config->getDefined('site.locales.available')) <= 1) {
$data['locale'] = $config['site.registration.user_defaults.locale'];
}

// Validate request data
$validator = new ServerSideValidator($schema, $this->ci->translator);
if (!$validator->validate($data)) {
Expand Down Expand Up @@ -1163,6 +1193,11 @@ public function settings(Request $request, Response $response, $args)

$error = false;

// Ensure that in the case of using a single locale, that the locale is set
if (count($config->getDefined('site.locales.available')) <= 1) {
$data['locale'] = $currentUser->locale;
}

// Validate, and halt on validation errors.
$validator = new ServerSideValidator($schema, $this->ci->translator);
if (!$validator->validate($data)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</div>
</div>

{% if 'locale' not in fields.hidden %}
<div class="form-group">
<label for="input-locale" class="control-label">{{translate("LOCALE")}}</label>
<select id="input-locale" class="form-control js-select2" name="locale" {{page.visibility}}>
Expand All @@ -31,6 +32,7 @@
</select>
<p class="help-block">{{translate("LOCALE.ACCOUNT")}}.</p>
</div>
{% endif %}
{% endblock %}
</div>
<div class="box-footer text-center">
Expand Down
2 changes: 2 additions & 0 deletions app/sprinkles/account/templates/pages/register.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<label class="sr-only" for="r-form-passwordc">{{translate('PASSWORD.CONFIRM')}}</label>
<input type="password" name="passwordc" placeholder="{{translate('PASSWORD.CONFIRM')}}" class="form-control" id="r-form-passwordc">
</div>
{% if 'locale' not in fields.hidden %}
<div class="form-group">
<label for="r-form-locale" class="control-label">{{translate("LOCALE")}}</label>
<select id="r-form-locale" class="form-control js-select2" name="locale">
Expand All @@ -64,6 +65,7 @@
</select>
<p class="help-block">{{translate("LOCALE.ACCOUNT")}}.</p>
</div>
{% endif %}
{% if site.registration.captcha %}
<div class="form-group">
<label class="sr-only" for="r-form-passwordc">{{translate('CAPTCHA.VERIFY')}}</label>
Expand Down
23 changes: 23 additions & 0 deletions app/sprinkles/admin/src/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ public function create(Request $request, Response $response, $args)
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
$currentUser = $this->ci->currentUser;

/** @var \UserFrosting\Support\Repository\Repository $config */
$config = $this->ci->config;

// Access-controlled page
if (!$authorizer->checkAccess($currentUser, 'create_user')) {
throw new ForbiddenException();
Expand All @@ -77,6 +80,11 @@ public function create(Request $request, Response $response, $args)

$error = false;

// Ensure that in the case of using a single locale, that the locale is set bu inheriting from current user
if (count($config->getDefined('site.locales.available')) <= 1) {
$data['locale'] = $currentUser->locale;
}

// Validate request data
$validator = new ServerSideValidator($schema, $this->ci->translator);
if (!$validator->validate($data)) {
Expand Down Expand Up @@ -573,6 +581,11 @@ public function getModalCreate(Request $request, Response $response, $args)
$fields['disabled'][] = 'group';
}

// Hide the locale field if there is only 1 locale available
if (count($config->getDefined('site.locales.available')) <= 1) {
$fields['hidden'][] = 'locale';
}

// Create a dummy user to prepopulate fields
$data = [
'group_id' => $currentUser->group_id,
Expand Down Expand Up @@ -673,6 +686,11 @@ public function getModalEdit(Request $request, Response $response, $args)
$fields['disabled'][] = 'group';
}

// Hide the locale field if there is only 1 locale available
if (count($config->getDefined('site.locales.available')) <= 1) {
$fields['hidden'][] = 'locale';
}

// Load validation rules
$schema = new RequestSchema('schema://requests/user/edit-info.yaml');
$validator = new JqueryValidationAdapter($schema, $this->ci->translator);
Expand Down Expand Up @@ -950,6 +968,11 @@ public function pageInfo(Request $request, Response $response, $args)
}
}

// Hide the locale field if there is only 1 locale available
if (count($config->getDefined('site.locales.available')) <= 1) {
$fields['hidden'][] = 'locale';
}

// Determine buttons to display
$editButtons = [
'hidden' => []
Expand Down

0 comments on commit 36d57a9

Please sign in to comment.