Skip to content
Snippets Groups Projects
Commit 72ab7c8f authored by Shibin Das's avatar Shibin Das
Browse files

Issue #3496205 by d34dman: Update option list to hint towards applicable...

Issue #3496205 by d34dman: Update option list to hint towards applicable languages during configuration
parent e8576ec1
No related branches found
No related tags found
No related merge requests found
...@@ -8,6 +8,7 @@ use Drupal\Core\Config\TypedConfigManagerInterface; ...@@ -8,6 +8,7 @@ use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface; use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\string_plural_form\StringPluralFormManager; use Drupal\string_plural_form\StringPluralFormManager;
use Drupal\string_plural_form\StringPluralFormPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase; use Drupal\Core\Form\ConfigFormBase;
...@@ -57,7 +58,6 @@ final class SettingsForm extends ConfigFormBase { ...@@ -57,7 +58,6 @@ final class SettingsForm extends ConfigFormBase {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function buildForm(array $form, FormStateInterface $form_state): array { public function buildForm(array $form, FormStateInterface $form_state): array {
$languages = $this->languageManager->getLanguages(LanguageInterface::STATE_CONFIGURABLE); $languages = $this->languageManager->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
$rules = $this->stringPluralFormManager->getDefinitions(); $rules = $this->stringPluralFormManager->getDefinitions();
$options = []; $options = [];
...@@ -81,13 +81,69 @@ final class SettingsForm extends ConfigFormBase { ...@@ -81,13 +81,69 @@ final class SettingsForm extends ConfigFormBase {
'#type' => 'select', '#type' => 'select',
'#title' => sprintf("%s (%s)", $language->getName(), $language->getId()), '#title' => sprintf("%s (%s)", $language->getName(), $language->getId()),
'#default_value' => $rules_map[$language->getId()], '#default_value' => $rules_map[$language->getId()],
'#options' => $options, '#options' => $this->getOptionsForLanguage($language->getId()),
'#required' => TRUE, '#required' => TRUE,
]; ];
} }
return parent::buildForm($form, $form_state); return parent::buildForm($form, $form_state);
} }
/**
* Get Plural Form rules categorized by applicable to provided language.
*
* @param string $langcode
* The language code for which rules list needs to be prepared.
*
* @return string[]
* <optgroup> comptible option list of rules.
*/
protected function getOptionsForLanguage(string $langcode): array {
$rules = $this->stringPluralFormManager->getDefinitions();
$recommended_rules = [];
$other_rules = [];
foreach ($rules as $rule) {
$plural_form = $this->stringPluralFormManager->createInstance($rule['id']);
if ($plural_form instanceof StringPluralFormPluginInterface) {
if ($this->isRuleRecommendedForLanguage($plural_form, $langcode)) {
$recommended_rules[$rule['id']] = $rule['admin_label'];
}
else {
$other_rules[$rule['id']] = $rule['admin_label'];
}
}
}
$options = [
(string) $this->t('Recommended Rules') => $recommended_rules,
(string) $this->t('Other Rules') => $other_rules,
];
return $options;
}
/**
* Check if given rule is applicable for the language.
*
* @param \Drupal\string_plural_form\StringPluralFormPluginInterface $rule
* Instance of Rule.
* @param string $langcode
* Language code.
*
* @return bool
* True if rule is applicable for the given language.
*/
protected function isRuleRecommendedForLanguage(StringPluralFormPluginInterface $rule, string $langcode): bool {
// In case "ISO 639-1" langcode is being used, then we split the incoming
// language code to match only the language part.
$code = explode('-', $langcode);
$match = $code[0];
foreach ($rule->getAssociatedLanguageCodes() as $code) {
if ($match == $code) {
return TRUE;
}
}
return FALSE;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -47,7 +47,7 @@ class SixFormsArabic extends StringPluralFormBase { ...@@ -47,7 +47,7 @@ class SixFormsArabic extends StringPluralFormBase {
0 => 0, 0 => 0,
1 => 1, 1 => 1,
2 => 2, 2 => 2,
default => ($n>10 && $n%100>=3 && $n%100<=10) ? 3 : ($n%100>=11 ? 4 : 5) default => ($n > 10 && $n % 100 >= 3 && $n % 100 <= 10) ? 3 : ($n % 100 >= 11 ? 4 : 5)
}; };
return $index; return $index;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment