diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index 5ff02f3..e72cb7d 100644 --- a/core/config/schema/core.entity.schema.yml +++ b/core/config/schema/core.entity.schema.yml @@ -280,12 +280,9 @@ field_formatter_settings_numeric_base: format_plural: type: boolean label: 'Format plural' - format_plural_singular: + format_plural_string: type: label - label: 'Singular form' - format_plural_plural: - type: label - label: 'Plural form' + label: 'Singular and one or more plurals' field.formatter.settings.number_decimal: type: field_formatter_settings_numeric_base diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php index d7b90de..2c48ad9 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php @@ -27,8 +27,7 @@ public static function defaultSettings() { 'thousand_separator' => '', 'prefix_suffix' => TRUE, 'format_plural' => FALSE, - 'format_plural_singular' => '1', - 'format_plural_plural' => '@count', + 'format_plural_string' => '1' . LOCALE_PLURAL_DELIMITER . '@count', ) + parent::defaultSettings(); } @@ -65,28 +64,29 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#description' => $this->t('If checked, special handling will be used for plurality.'), '#default_value' => $this->getSetting('format_plural'), ); - $form['format_plural_singular'] = array( - '#type' => 'textfield', - '#title' => $this->t('Singular form'), - '#default_value' => $this->getSetting('format_plural_singular'), - '#description' => $this->t('Text to use for the singular form.'), - '#states' => array( - 'visible' => array( - ':input[name="options[format_plural]"]' => array('checked' => TRUE), + $plural_array = explode(LOCALE_PLURAL_DELIMITER, $this->getSetting('format_plural_string')); + // @todo get langcode of this formatter config to use for configuration. + // @todo figure out how to alter the form values when this is submitted. + $plurals = $this->getNumberOfPlurals(); + for ($i = 0; $i < $plurals; $i++) { + $form['format_plural_values'][$i] = array( + '#type' => 'textfield', + '#title' => ($i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form')), + '#default_value' => isset($plural_array[$i]) ? $plural_array[$i] : '', + '#description' => $this->t('Text to use for this variant, @count will be replaced with the value.'), + '#states' => array( + 'visible' => array( + ':input[name="options[format_plural]"]' => array('checked' => TRUE), + ), ), - ), - ); - $form['format_plural_plural'] = array( - '#type' => 'textfield', - '#title' => $this->t('Plural form'), - '#default_value' => $this->getSetting('format_plural_plural'), - '#description' => $this->t('Text to use for the plural form, @count will be replaced with the value.'), - '#states' => array( - 'visible' => array( - ':input[name="options[format_plural]"]' => array('checked' => TRUE), - ), - ), - ); + ); + } + if ($plurals == 2) { + // Simplify user interface text for the most common case. + $form['format_plural_values'][0]['#description'] = $this->t('Text to use for the singular form, @count will be replaced with the value.'); + $form['format_plural_values'][1]['#title'] = $this->t('Plural form'); + $form['format_plural_values'][1]['#description'] = $this->t('Text to use for the plural form, @count will be replaced with the value.'); + } return $elements; } @@ -116,7 +116,7 @@ public function viewElements(FieldItemListInterface $items) { $output = $this->numberFormat($item->value); if ($this->getSetting('format_plural')) { - $output = $this->formatPlural($output, $this->getSetting('format_plural_singular'), $this->getSetting('format_plural_plural')); + $output = $this->formatPluralTranslated($output, $this->getSetting('format_plural_string')); } // Account for prefix and suffix.