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

Issue #3495996 by d34dman: Define all Plural Form Rules as Plugins as mentioned in Gettext Manual

parent fb515f5e
No related branches found
No related tags found
No related merge requests found
# Schema for the configuration files of the String Plural Forms module.
string_plural_form.settings:
type: config_object
label: 'String Plural Forms settings'
mapping:
rules_map:
type: sequence
label: 'Rules'
orderby: key
sequence:
type: string
label: 'Langcode - Rule mapping'
<?php
declare(strict_types=1);
namespace Drupal\string_plural_form\Form;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\string_plural_form\StringPluralFormManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* Configure String Plural Forms settings for this site.
*/
final class SettingsForm extends ConfigFormBase {
public function __construct(
ConfigFactoryInterface $config_factory,
TypedConfigManagerInterface $typedConfigManager,
private LanguageManagerInterface $languageManager,
private StringPluralFormManager $stringPluralFormManager,
) {
parent::__construct($config_factory, $typedConfigManager);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('config.typed'),
$container->get('language_manager'),
$container->get('plugin.manager.string_plural_form')
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'string_plural_form_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return ['string_plural_form.settings'];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$languages = $this->languageManager->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
$rules = $this->stringPluralFormManager->getDefinitions();
$options = [];
foreach ($rules as $rule) {
$options[$rule['id']] = $rule['admin_label'];
}
$saved_rules = $this->config('string_plural_form.settings')->get('rules');
$rules_map = [];
foreach ($languages as $language) {
$rules_map[$language->getId()] = NULL;
if ($saved_rules && $saved_rules[$language->getId()]) {
$rules_map[$language->getId()] = $saved_rules[$language->getId()];
}
}
$form['rules'] = [
'#type' => 'container',
'#tree' => TRUE,
];
foreach ($languages as $language) {
$form['rules'][$language->getId()] = [
'#type' => 'select',
'#title' => sprintf("%s (%s)", $language->getName(), $language->getId()),
'#default_value' => $rules_map[$language->getId()],
'#options' => $options,
'#required' => TRUE,
];
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$rules = $form_state->getValue('rules');
$this->config('string_plural_form.settings')
->set('rules', $rules)
->save();
// @codingStandardsIgnoreLine
if (\Drupal::hasService('locale.plural.formula')) {
foreach ($rules as $langcode => $rule_id) {
/** @var \Drupal\string_plural_form\StringPluralFormPluginInterface $rule */
$rule = $this->stringPluralFormManager->createInstance($rule_id);
// @codingStandardsIgnoreLine
\Drupal::service('locale.plural.formula')
->setPluralFormula($langcode, $rule->getGettextNplurals(), $rule->generateLocalePluralIndex());
}
}
parent::submitForm($form, $form_state);
}
}
string_plural_form.settings:
title: String Plural Form
route_name: string_plural_form.settings
weight: 10
string_plural_form.settings:
path: '/admin/config/regional/language/string-plural-form'
defaults:
_title: 'String Plural Form'
_form: 'Drupal\string_plural_form\Form\SettingsForm'
requirements:
_permission: 'administer site configuration'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment