diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index 90813e5..40b8f5a 100644 --- a/core/config/schema/core.entity.schema.yml +++ b/core/config/schema/core.entity.schema.yml @@ -268,33 +268,36 @@ field.formatter.settings.language: type: boolean label: 'Display in native language' -field.formatter.settings.number_decimal: +field_formatter_settings_numeric_base: type: mapping - label: 'Number decimal display format settings' mapping: thousand_separator: type: string label: 'Thousand marker' + prefix_suffix: + type: boolean + label: 'Display prefix and suffix' + format_plural: + type: boolean + label: 'Format plural' + format_plural_string: + type: label + label: 'Singular and one or more plurals' + +field.formatter.settings.number_decimal: + type: field_formatter_settings_numeric_base + label: 'Number decimal display format settings' + mapping: decimal_separator: type: string label: 'Decimal marker' scale: type: integer label: 'Scale' - prefix_suffix: - type: boolean - label: 'Display prefix and suffix.' field.formatter.settings.number_integer: - type: mapping + type: field_formatter_settings_numeric_base label: 'Number integer display format settings' - mapping: - thousand_separator: - type: string - label: 'Thousand marker' - prefix_suffix: - type: boolean - label: 'Display prefix and suffix.' field.formatter.settings.number_unformatted: type: mapping diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/DecimalFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/DecimalFormatter.php index 0e43ac1..7c9a17b 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/DecimalFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/DecimalFormatter.php @@ -32,10 +32,8 @@ class DecimalFormatter extends NumericFormatterBase { */ public static function defaultSettings() { return array( - 'thousand_separator' => '', 'decimal_separator' => '.', 'scale' => 2, - 'prefix_suffix' => TRUE, ) + parent::defaultSettings(); } diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/IntegerFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/IntegerFormatter.php index 8768474..20e5d4b 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/IntegerFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/IntegerFormatter.php @@ -27,16 +27,6 @@ class IntegerFormatter extends NumericFormatterBase { /** * {@inheritdoc} */ - public static function defaultSettings() { - return array( - 'thousand_separator' => '', - 'prefix_suffix' => TRUE, - ) + parent::defaultSettings(); - } - - /** - * {@inheritdoc} - */ protected function numberFormat($number) { return number_format($number, 0, '', $this->getSetting('thousand_separator')); } 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 c30a9d2..2c48ad9 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php @@ -22,6 +22,18 @@ /** * {@inheritdoc} */ + public static function defaultSettings() { + return array( + 'thousand_separator' => '', + 'prefix_suffix' => TRUE, + 'format_plural' => FALSE, + 'format_plural_string' => '1' . LOCALE_PLURAL_DELIMITER . '@count', + ) + parent::defaultSettings(); + } + + /** + * {@inheritdoc} + */ public function settingsForm(array $form, FormStateInterface $form_state) { $options = array( '' => t('- None -'), @@ -46,6 +58,36 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#weight' => 10, ); + $form['format_plural'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Format plural'), + '#description' => $this->t('If checked, special handling will be used for plurality.'), + '#default_value' => $this->getSetting('format_plural'), + ); + $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), + ), + ), + ); + } + 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; } @@ -73,6 +115,10 @@ public function viewElements(FieldItemListInterface $items) { foreach ($items as $delta => $item) { $output = $this->numberFormat($item->value); + if ($this->getSetting('format_plural')) { + $output = $this->formatPluralTranslated($output, $this->getSetting('format_plural_string')); + } + // Account for prefix and suffix. if ($this->getSetting('prefix_suffix')) { $prefixes = isset($settings['prefix']) ? array_map(array($this, 'fieldFilterXss'), explode('|', $settings['prefix'])) : array(''); diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateFieldFormatterSettingsTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateFieldFormatterSettingsTest.php index 2175cb0..f34a490 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateFieldFormatterSettingsTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateFieldFormatterSettingsTest.php @@ -125,16 +125,20 @@ public function testEntityDisplaySettings() { $expected['settings'] = array( 'thousand_separator' => ',', 'prefix_suffix' => TRUE, + 'format_plural' => FALSE, + 'format_plural_string' => '1' . LOCALE_PLURAL_DELIMITER . '@count', ); $component = $display->getComponent('field_test_two'); $this->assertIdentical($expected, $component); $expected['weight'] = 2; $expected['type'] = 'number_decimal'; $expected['settings'] = array( - 'scale' => 2, - 'decimal_separator' => '.', - 'thousand_separator' => ',', - 'prefix_suffix' => TRUE, + 'scale' => 2, + 'decimal_separator' => '.', + 'thousand_separator' => ',', + 'prefix_suffix' => TRUE, + 'format_plural' => FALSE, + 'format_plural_string' => '1' . LOCALE_PLURAL_DELIMITER . '@count', ); $component = $display->getComponent('field_test_three'); $this->assertIdentical($expected, $component);