Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide locale field when only 1 locale is available (#968) #969

Merged
merged 1 commit into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could reuse $locales here. Could be mirrored on line 519 for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be more preferable to use:

        $localePathBuilder = $this->ci->localePathBuilder;

        // Get locale information
        $currentLocales = $localePathBuilder->getLocales();

vs

        // Get a list of all locales
        $locales = $config->getDefined('site.locales.available');

in all cases?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure, but I think localePathBuilder will only return the locale associated with the one currently in use? It will at least return them in the inheritance order.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case, this behavior will be changed in #850, so I guess it doesn't matters for now.

$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