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 a9510d9..b0f5eec 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php @@ -97,7 +97,7 @@ public function settingsForm(array $form, FormStateInterface $form_state) { $plural_string = $settings['format_plural_string']; } } - $plural_array = explode(LOCALE_PLURAL_DELIMITER, $plural_string); + $plural_array = explode(PluralTranslatableMarkup::DELIMITER, $plural_string); for ($i = 0; $i < $plurals; $i++) { $elements['format_plural_values'][$i] = array( '#type' => 'textfield', @@ -133,7 +133,7 @@ public static function formatPluralValuesAfterBuild($element, FormStateInterface $keys = $element['#parents']; $array_value = $form_state->getValue($keys); $form_state->unsetValue($keys); - $string_value = implode(LOCALE_PLURAL_DELIMITER, $array_value); + $string_value = implode(PluralTranslatableMarkup::DELIMITER, $array_value); array_pop($keys); $keys[] = 'format_plural_string'; $form_state->setValue($keys, $string_value); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php index b51e96c..a101942 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/NumericItemBase.php @@ -9,6 +9,7 @@ use Drupal\Core\Field\FieldItemBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\StringTranslation\PluralTranslatableMarkup; /** * Base class for numeric configurable field types. @@ -54,7 +55,7 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) { $langcode = $this->getFieldDefinition()->get('langcode'); $plurals = $this->getNumberOfPlurals($langcode); $labels = $this->getPluralLabels($langcode); - $plural_array = explode(LOCALE_PLURAL_DELIMITER, $settings['format_plural_string']); + $plural_array = explode(PluralTranslatableMarkup::DELIMITER, $settings['format_plural_string']); for ($i = 0; $i < $plurals; $i++) { $element['format_plural_values'][$i] = array( '#type' => 'textfield', @@ -90,7 +91,7 @@ public static function formatPluralValuesAfterBuild($element, FormStateInterface $keys = $element['#parents']; $array_value = $form_state->getValue($keys); $form_state->unsetValue($keys); - $string_value = implode(LOCALE_PLURAL_DELIMITER, $array_value); + $string_value = implode(PluralTranslatableMarkup::DELIMITER, $array_value); array_pop($keys); $keys[] = 'format_plural_string'; $form_state->setValue($keys, $string_value); diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php index f7f6420..5bcfb7c 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldWidget/NumberWidget.php @@ -11,6 +11,7 @@ use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\StringTranslation\PluralTranslatableMarkup; use Symfony\Component\Validator\ConstraintViolationInterface; /** @@ -118,7 +119,7 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen // for use in formatPlural() methods, but already translated into the // UI language. Pop off the last plural variant, and use the parts // before/after @count in that string as the prefix/suffix for this field. - $values = explode(LOCALE_PLURAL_DELIMITER, $field_settings['format_plural_string']); + $values = explode(PluralTranslatableMarkup::DELIMITER, $field_settings['format_plural_string']); $labels = explode('@count', array_pop($values)); if (isset($labels[0])) { $element['#field_prefix'] = FieldFilteredMarkup::create(trim($labels[0])); diff --git a/core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php index 4bcccd2..fc7a85b 100644 --- a/core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php +++ b/core/lib/Drupal/Core/StringTranslation/PluralTranslatableMarkup.php @@ -149,9 +149,9 @@ public function render() { } else { // If the index cannot be computed or there's no translation, use the - // second plural form as a fallback (which allows for most flexibility + // last plural form as a fallback (which allows for most flexibility // with the replaceable @count value). - $return = $translated_array[1]; + $return = $translated_array[count($translated_array) - 1]; } } diff --git a/core/modules/field/src/Plugin/migrate/process/d6/FieldFormatterSettingsDefaults.php b/core/modules/field/src/Plugin/migrate/process/d6/FieldFormatterSettingsDefaults.php index 4deeb16..41b6bd5 100644 --- a/core/modules/field/src/Plugin/migrate/process/d6/FieldFormatterSettingsDefaults.php +++ b/core/modules/field/src/Plugin/migrate/process/d6/FieldFormatterSettingsDefaults.php @@ -64,69 +64,69 @@ protected function numberSettings($type, $format) { 'scale' => 0, 'decimal_separator' => '.', 'thousand_separator' => ',', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'us_1' => [ 'scale' => 1, 'decimal_separator' => '.', 'thousand_separator' => ',', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'us_2' => [ 'scale' => 2, 'decimal_separator' => '.', 'thousand_separator' => ',', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'be_0' => [ 'scale' => 0, 'decimal_separator' => ',', 'thousand_separator' => '.', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'be_1' => [ 'scale' => 1, 'decimal_separator' => ',', 'thousand_separator' => '.', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'be_2' => [ 'scale' => 2, 'decimal_separator' => ',', 'thousand_separator' => '.', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'fr_0' => [ 'scale' => 0, 'decimal_separator' => ',', 'thousand_separator' => ' ', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'fr_1' => [ 'scale' => 1, 'decimal_separator' => ',', 'thousand_separator' => ' ', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'fr_2' => [ 'scale' => 2, 'decimal_separator' => ',', 'thousand_separator' => ' ', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], ], 'number_integer' => [ 'us_0' => [ 'thousand_separator' => ',', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'be_0' => [ 'thousand_separator' => '.', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], 'fr_0' => [ 'thousand_separator' => ' ', - 'prefix_suffix' => TRUE, + 'format_plural' => 'field', ], ], ]; diff --git a/core/modules/field/src/Plugin/migrate/process/d6/FieldInstanceSettings.php b/core/modules/field/src/Plugin/migrate/process/d6/FieldInstanceSettings.php index a678820..eaea415 100644 --- a/core/modules/field/src/Plugin/migrate/process/d6/FieldInstanceSettings.php +++ b/core/modules/field/src/Plugin/migrate/process/d6/FieldInstanceSettings.php @@ -7,10 +7,12 @@ namespace Drupal\field\Plugin\migrate\process\d6; +use Drupal\Core\StringTranslation\PluralTranslatableMarkup; use Drupal\migrate\MigrateExecutableInterface; use Drupal\migrate\ProcessPluginBase; use Drupal\migrate\Row; + /** * @MigrateProcessPlugin( * id = "d6_field_field_settings" @@ -30,8 +32,27 @@ public function transform($value, MigrateExecutableInterface $migrate_executable case 'number': $settings['min'] = $field_settings['min']; $settings['max'] = $field_settings['max']; - $settings['prefix'] = $field_settings['prefix']; - $settings['suffix'] = $field_settings['suffix']; + $prefix = $field_settings['prefix']; + $suffix = $field_settings['suffix']; + if ($prefix || $suffix) { + // In case they were using the previous system to separate + // plural values by |, make this into a proper format_plural + // object. + $prefixes = explode('|', $prefix); + $suffixes = explode('|', $suffix); + $length = max(count($prefixes), count($suffixes)); + $strings = []; + for ($i = 0; $i < $length; $i++) { + $prefix = isset($prefixes[$i]) ?: $prefixes[0]; + $suffix = isset($suffixes[$i]) ?: $suffixes[0]; + $strings[] = $prefix . '@count' . $suffix; + } + $settings['format_plural_string'] = implode(PluralTranslatableMarkup::DELIMITER, $strings); + } + else { + $settings['format_plural_string'] = ''; + } + break; case 'link': diff --git a/core/modules/field/src/Tests/Migrate/d6/MigrateFieldInstanceTest.php b/core/modules/field/src/Tests/Migrate/d6/MigrateFieldInstanceTest.php index e3f115c..e48bd5f 100644 --- a/core/modules/field/src/Tests/Migrate/d6/MigrateFieldInstanceTest.php +++ b/core/modules/field/src/Tests/Migrate/d6/MigrateFieldInstanceTest.php @@ -39,7 +39,7 @@ public function testFieldInstanceMigration() { $expected = array( 'min' => 10, 'max' => 100, - 'format_plural_string' => 'pref@count suf', + 'format_plural_string' => 'pref@countsuf', 'unsigned' => FALSE, 'size' => 'normal', ); diff --git a/core/modules/field/src/Tests/Number/NumberFieldTest.php b/core/modules/field/src/Tests/Number/NumberFieldTest.php index 64f746f..8ec3b00 100644 --- a/core/modules/field/src/Tests/Number/NumberFieldTest.php +++ b/core/modules/field/src/Tests/Number/NumberFieldTest.php @@ -9,6 +9,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\simpletest\WebTestBase; +use Drupal\Core\StringTranslation\PluralTranslatableMarkup; /** * Tests the creation of numeric fields. @@ -145,7 +146,9 @@ function testNumberIntegerField() { 'entity_type' => 'entity_test', 'bundle' => 'entity_test', 'settings' => array( - 'min' => $minimum, 'max' => $maximum, 'format_plural_string' => 'ThePrefix@count', + 'min' => $minimum, + 'max' => $maximum, + 'format_plural_string' => 'ThePrefix@count' . PluralTranslatableMarkup::DELIMITER . 'PluralPrefix@countPluralSuffix', ) ))->save(); @@ -153,7 +156,8 @@ function testNumberIntegerField() { ->setComponent($field_name, array( 'type' => 'number', 'settings' => array( - 'placeholder' => '4' + 'placeholder' => '4', + 'plural_format' => TRUE, ), )) ->save(); @@ -185,6 +189,24 @@ function testNumberIntegerField() { $this->drupalGet('entity_test/add'); $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed'); $this->assertRaw('placeholder="4"'); + $this->assertText('PluralPrefix'); + $this->assertText('PluralSuffix'); + + // Turn off the prefix and suffix and test. + entity_get_form_display('entity_test', 'entity_test', 'default') + ->setComponent($field_name, array( + 'type' => 'number', + 'settings' => array( + 'placeholder' => '4', + 'plural_format' => FALSE, + ), + )) + ->save(); + $this->drupalGet('entity_test/add'); + $this->assertFieldByName("{$field_name}[0][value]", '', 'Widget is displayed'); + $this->assertRaw('placeholder="4"'); + $this->assertNoText('PluralPrefix'); + $this->assertNoText('PluralSuffix'); // Submit a valid integer $value = rand($minimum, $maximum); @@ -258,6 +280,7 @@ function testNumberIntegerField() { ), )) ->save(); + $integer_value = '123'; $this->drupalGet('entity_test/add'); $edit = array( @@ -268,7 +291,20 @@ function testNumberIntegerField() { $id = $match[1]; $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created'); $this->drupalGet('entity_test/' . $id); - $this->assertFieldByXPath('//div[@content="' . $integer_value . '"]', 'ThePrefix' . $integer_value, 'The "content" attribute has been set to the value of the field, and the prefix is being displayed.'); + $this->assertFieldByXPath('//div[@content="' . $integer_value . '"]', 'PluralPrefix' . $integer_value . 'PluralSuffix', 'The "content" attribute has been set to the value of the field, and the prefix and suffix are being displayed.'); + + // Test with a singular value. + $integer_value = '1'; + $this->drupalGet('entity_test/add'); + $edit = array( + "{$field_name}[0][value]" => $integer_value, + ); + $this->drupalPostForm(NULL, $edit, t('Save')); + preg_match('|entity_test/manage/(\d+)|', $this->url, $match); + $id = $match[1]; + $this->assertText(t('entity_test @id has been created.', array('@id' => $id)), 'Entity was created'); + $this->drupalGet('entity_test/' . $id); + $this->assertFieldByXPath('//div[@content="' . $integer_value . '"]', 'ThePrefix' . $integer_value, 'The "content" attribute has been set to the value of the field, and the singular prefix is being displayed.'); // Change to using an overridden plural formatting string. entity_get_display('entity_test', 'entity_test', 'default') diff --git a/core/modules/system/system.install b/core/modules/system/system.install index de1bfcc..ab9ed3a 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -1880,7 +1880,7 @@ function system_update_8014($sandbox) { $settings = $field->get('settings'); $prefix = isset($settings['prefix']) ?: ''; $suffix = isset($settings['suffix']) ?: ''; - if ($prefix || $suffix) { + if (($prefix || $suffix) && (!isset($settings['format_plural_string']) || !$settings['format_plural_string'])) { // In case they were using the previous system to separate // plural values by |, make this into a proper format_plural // object. @@ -1940,16 +1940,15 @@ function system_update_8014($sandbox) { $changed = FALSE; foreach ($displays as $display_name => $display) { if (!empty($display['display_options']['fields'])) { - foreach ($views_fields_to_update as $field_name) { - if (!isset($display['display_options']['fields'][$field_name])) { - continue; + foreach ($display['display_options']['fields'] as $field_name => $field) { + if (in_array($field['field_name'], $views_fields_to_update)) { + $settings = $display['display_options']['fields'][$field_name]['settings']; + $settings['format_plural'] = (isset($settings['prefix_suffix']) && $settings['prefix_suffix']) ? 'field' : 'none'; + unset($settings['prefix_suffix']); + $settings['format_plural_string'] = ''; + $displays[$display_name]['display_options']['fields'][$field_name]['settings'] = $settings; + $changed = TRUE; } - $settings = $display['display_options']['fields'][$field_name]['settings']; - $settings['format_plural'] = (isset($settings['prefix_suffix']) && $settings['prefix_suffix']) ? 'field' : 'none'; - unset($settings['prefix_suffix']); - $settings['format_plural_string'] = ''; - $displays[$display_name]['display_options']['fields'][$field_name]['settings'] = $settings; - $changed = TRUE; } } }