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

Expand formatting locale options #10519

Merged
merged 3 commits into from
Feb 10, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,8 @@
- The “What’ New” HUD now displays an icon and label above each announcement, identifying where it came from (Craft CMS or a plugin). ([#9747](https://github.com/craftcms/cms/discussions/9747))
- The control panel now keeps track of the currently-edited site on a per-tab basis by adding a `site` query string param to all control panel URLs. ([#8920](https://github.com/craftcms/cms/discussions/8920))
- Users are no longer required to have a username or email.
- Users can now set their Formatting Locale to any known locale; not just the available Language options. ([#10519](https://github.com/craftcms/cms/pull/10519))
- Users’ Language and Formatting Locale settings now display locale names in the current language and their native languages. ([#10519](https://github.com/craftcms/cms/pull/10519))
- User queries now return all users by default, rather than only active users.
- Filtering users by `active`, `pending`, and `locked` statuses no longer excludes suspended users.
- `credentialed` and `inactive` are now reserved user group handles.
Expand Down
73 changes: 46 additions & 27 deletions src/controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -967,41 +967,59 @@ public function actionEditUser($userId = null, ?User $user = null, ?array $error
// ---------------------------------------------------------------------

if ($isCurrentUser) {
/** @var Locale[] $appLocales */
$appLocales = ArrayHelper::index(Craft::$app->getI18n()->getAppLocales(), 'id');
ArrayHelper::multisort($appLocales, 'displayName');
$localeOptions = [];
foreach ($appLocales as $locale) {
$localeOptions[] = [
'label' => $locale->getDisplayName(),
'value' => $locale->id,
];
}
$i18n = Craft::$app->getI18n();

// Language
$appLocales = $i18n->getAppLocales();
ArrayHelper::multisort($appLocales, fn(Locale $locale) => $locale->getDisplayName());
$languageId = Craft::$app->getLocale()->getLanguageID();

$languageOptions = array_map(fn(Locale $locale) => [
'label' => $locale->getDisplayName(Craft::$app->language),
'value' => $locale->id,
'data' => [
'data' => [
'hint' => $locale->getLanguageID() !== $languageId ? $locale->getDisplayName() : false,
],
],
], $appLocales);

$userLanguage = $user->getPreferredLanguage();
if ($userLanguage !== null && !isset($appLocales[$userLanguage])) {
$userLanguage = null;
}

$userLocale = $user->getPreferredLocale();
if ($userLocale !== null && !isset($appLocales[$userLocale])) {
$userLocale = null;
if (
!$userLanguage ||
!ArrayHelper::contains($appLocales, fn(Locale $locale) => $locale->id === $userLanguage)
) {
$userLanguage = Craft::$app->language;
}

if ($userLanguage === null) {
$userLanguage = Craft::$app->language;
// Formatting Locale
$allLocales = $i18n->getAllLocales();
ArrayHelper::multisort($allLocales, fn(Locale $locale) => $locale->getDisplayName());

// Only set the locale to the defaultCpLocale by default if the language isn't set either.
// Otherwise `null` means "Same as language"
if ($userLocale === null) {
$generalConfig = Craft::$app->getConfig()->getGeneral();
if ($generalConfig->defaultCpLocale) {
$userLocale = $generalConfig->defaultCpLocale;
}
}
$localeOptions = [
['label' => Craft::t('app', 'Same as language'), 'value' => ''],
];
array_push($localeOptions, ...array_map(fn(Locale $locale) => [
'label' => $locale->getDisplayName(Craft::$app->language),
'value' => $locale->id,
'data' => [
'data' => [
'hint' => $locale->getLanguageID() !== $languageId ? $locale->getDisplayName() : false,
],
],
], $allLocales));

$userLocale = $user->getPreferredLocale();

if (
!$userLocale ||
!ArrayHelper::contains($allLocales, fn(Locale $locale) => $locale->id === $userLocale)
) {
$userLocale = Craft::$app->getConfig()->getGeneral()->defaultCpLocale;
}
} else {
$localeOptions = $userLanguage = $userLocale = null;
$languageOptions = $localeOptions = $userLanguage = $userLocale = null;
}

// Determine whether user photo uploading should be possible
Expand Down Expand Up @@ -1033,6 +1051,7 @@ public function actionEditUser($userId = null, ?User $user = null, ?array $error
'isNewUser',
'statusLabel',
'actions',
'languageOptions',
'localeOptions',
'userLanguage',
'userLocale',
Expand Down
1 change: 1 addition & 0 deletions src/templates/_includes/forms/selectize.twig
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
};

$select.selectize($.extend({
searchField: ['text', 'hint'],
render: {
option: data => {
const classes = ['option'];
Expand Down
8 changes: 4 additions & 4 deletions src/templates/users/_edit.twig
Original file line number Diff line number Diff line change
Expand Up @@ -212,21 +212,21 @@

{% if user.getIsCurrent() %}
<div id="prefs" class="flex-fields hidden">
{{ forms.selectField({
{{ forms.selectizeField({
id: 'preferredLanguage',
name: 'preferredLanguage',
label: "Language"|t('app'),
instructions: 'The language that the control panel should use.'|t('app'),
options: localeOptions,
options: languageOptions,
value: userLanguage,
}) }}

{{ forms.selectField({
{{ forms.selectizeField({
id: 'preferredLocale',
name: 'preferredLocale',
label: "Formatting Locale"|t('app'),
instructions: 'The locale that should be used for date and number formatting.'|t('app'),
options: (localeOptions ?? [])|unshift({label: 'Same as language'|t('app'), value: ''}),
options: localeOptions,
value: userLocale,
}) }}

Expand Down