diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml index cc1a57e..07e4714 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: 'Use plural formatting' + 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/Entity/Entity/EntityViewDisplay.php b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php index db56e8b..85b4eff 100644 --- a/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php +++ b/core/lib/Drupal/Core/Entity/Entity/EntityViewDisplay.php @@ -196,6 +196,9 @@ public function getRenderer($field_name) { // Instantiate the formatter object from the stored display properties. if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) { + // Record the language that the configuration was translated into, which + // is needed for plural formatting (for example). + $configuration['settings']['settings_langcode'] = $this->langcode; $formatter = $this->pluginManager->getInstance(array( 'field_definition' => $definition, 'view_mode' => $this->originalMode, diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php index 957c1e4..ce47653 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilder.php @@ -467,10 +467,20 @@ protected function getSingleFieldDisplay($entity, $field_name, $display_options) $bundle = $entity->bundle(); $key = $entity_type_id . ':' . $bundle . ':' . $field_name . ':' . crc32(serialize($display_options)); if (!isset($this->singleFieldDisplays[$key])) { + // Assign the incoming 'settings_langcode' as the langcode of the + // EntityViewDisplay, or default to the current interface language. + if (isset($display_options['settings']['settings_langcode'])) { + $langcode = $display_options['settings']['settings_langcode']; + unset($display_options['settings']['settings_langcode']); + } + else { + $langcode = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_INTERFACE)->getId(); + } $this->singleFieldDisplays[$key] = EntityViewDisplay::create(array( 'targetEntityType' => $entity_type_id, 'bundle' => $bundle, 'status' => TRUE, + 'langcode' => $langcode, ))->setComponent($field_name, $display_options); } $display = $this->singleFieldDisplays[$key]; diff --git a/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php b/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php index 0549060..e4dff16 100644 --- a/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php +++ b/core/lib/Drupal/Core/Entity/EntityViewBuilderInterface.php @@ -121,7 +121,10 @@ public function resetCache(array $entities = NULL); * 'default_formatter' for the field type. The default formatter will * also be used if the requested formatter is not available. * - settings: (array) Settings specific to the formatter. Defaults to the - * formatter's default settings. + * formatter's default settings. A settings_langcode value will be + * added; this is the language code of the parent entity (and hence of + * the settings), defaulting to the current interface language if not + * set. * - weight: (float) The weight to assign to the renderable element. * Defaults to 0. * diff --git a/core/lib/Drupal/Core/Field/FormatterPluginManager.php b/core/lib/Drupal/Core/Field/FormatterPluginManager.php index e6054a9..537d188 100644 --- a/core/lib/Drupal/Core/Field/FormatterPluginManager.php +++ b/core/lib/Drupal/Core/Field/FormatterPluginManager.php @@ -92,6 +92,9 @@ public function createInstance($plugin_id, array $configuration = array()) { * also be used if the requested formatter is not available. * - settings: (array) Settings specific to the formatter. Each setting * defaults to the default value specified in the formatter definition. + * There should be a 'settings_langcode' added, giving the language + * code of the parent entity (entity view display, view, etc. that the + * formatter is embedded in). * - third_party_settings: (array) Settings provided by other extensions * through hook_field_formatter_third_party_settings_form(). * 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/EntityReferenceEntityFormatter.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php index 1328f65..4cb961e 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/EntityReferenceEntityFormatter.php @@ -52,8 +52,6 @@ class EntityReferenceEntityFormatter extends EntityReferenceFormatterBase implem * The view mode. * @param array $third_party_settings * Any third party settings settings. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. * @param LoggerChannelFactoryInterface $logger_factory * The logger factory. */ 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..7f9caa2 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,10 +58,71 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#weight' => 10, ); + $elements['format_plural'] = array( + '#type' => 'checkbox', + '#title' => $this->t('Use plural formatting'), + '#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')); + $elements['format_plural_values'] = array( + '#after_build' => [[get_class($this), 'formatPluralValuesAfterBuild']], + ); + $plurals = $this->getNumberOfPlurals($this->getSetting('settings_langcode')); + for ($i = 0; $i < $plurals; $i++) { + $elements['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. + $elements['format_plural_values'][0]['#description'] = $this->t('Text to use for the singular form, @count will be replaced with the value.'); + $elements['format_plural_values'][1]['#title'] = $this->t('Plural form'); + $elements['format_plural_values'][1]['#description'] = $this->t('Text to use for the plural form, @count will be replaced with the value.'); + } return $elements; } /** + * After-build callback: Sets the value for the format_plural_string property. + * + * This method is assigned as an #after_build function in + * \namespace\class::settingsForm(). + * + * The user interface for setting up singular/plural forms is an array of text + * fields, for usability, but for translation purposes, the values have to be + * stored as a single string in the field settings. This function implodes the + * values. + * + * @param array $element + * Textfield element where the plural format should be set. + * @param \Drupal\Core\Form\FormStateInterface $form_state + * The current state of the form. + * + * @return array + * Textfield element with plural string set. + */ + public static function formatPluralValuesAfterBuild($element, FormStateInterface $form_state) { + if ($form_state->isProcessingInput()) { + $keys = $element['#parents']; + $array_value = $form_state->getValue($keys); + $string_value = implode(LOCALE_PLURAL_DELIMITER, $array_value); + array_pop($keys); + $keys[] = 'format_plural_string'; + $form_state->setValue($keys, $string_value); + } + return $element; + } + + /** * {@inheritdoc} */ public function settingsSummary() { @@ -73,6 +146,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/field/src/Tests/Number/NumberFieldTest.php b/core/modules/field/src/Tests/Number/NumberFieldTest.php index a3226a9..0aa4ce3 100644 --- a/core/modules/field/src/Tests/Number/NumberFieldTest.php +++ b/core/modules/field/src/Tests/Number/NumberFieldTest.php @@ -460,6 +460,21 @@ function testNumberFormatter() { $integer_formatted = number_format($random_integer, 0, '', $thousand_separator); $this->assertRaw($integer_formatted, 'Random integer formatted'); + + // Check with plural formatting settings. + $this->drupalGet("admin/structure/types/manage/$type/display"); + $this->drupalPostAjaxForm(NULL, array(), "${integer_field}_settings_edit"); + $edit["fields[${integer_field}][settings_edit_form][settings][format_plural]"] = TRUE; + $edit["fields[${integer_field}][settings_edit_form][settings][format_plural_values][0]"] = '1 item'; + $edit["fields[${integer_field}][settings_edit_form][settings][format_plural_values][1]"] = '@count items'; + $this->drupalPostAjaxForm(NULL, $edit, "${integer_field}_plugin_settings_update"); + $this->drupalPostForm(NULL, array(), t('Save')); + + $this->drupalGet('node/' . $node->id()); + + $integer_formatted = number_format($random_integer, 0, '', $thousand_separator); + $format_plural = $integer_formatted . ' item' . ($random_integer == 1 ? '' : 's'); + $this->assertRaw($format_plural, 'Random integer formatted'); } /** diff --git a/core/modules/field_ui/src/Tests/EntityDisplayTest.php b/core/modules/field_ui/src/Tests/EntityDisplayTest.php index 82dc8ab..7ecf457 100644 --- a/core/modules/field_ui/src/Tests/EntityDisplayTest.php +++ b/core/modules/field_ui/src/Tests/EntityDisplayTest.php @@ -211,7 +211,8 @@ public function testFieldComponent() { // Check that the getFormatter() method returns the correct formatter plugin. $formatter = $display->getRenderer($field_name); $this->assertEqual($formatter->getPluginId(), $default_formatter); - $this->assertEqual($formatter->getSettings(), $formatter_settings); + $expected = $formatter_settings + array('settings_langcode' => 'en'); + $this->assertEqual($formatter->getSettings(), $expected); // Check that the formatter is statically persisted, by assigning an // arbitrary property and reading it back. diff --git a/core/modules/migrate_drupal/src/Plugin/migrate/process/d6/FieldFormatterSettingsDefaults.php b/core/modules/migrate_drupal/src/Plugin/migrate/process/d6/FieldFormatterSettingsDefaults.php index a1b7856..45eb6e6 100644 --- a/core/modules/migrate_drupal/src/Plugin/migrate/process/d6/FieldFormatterSettingsDefaults.php +++ b/core/modules/migrate_drupal/src/Plugin/migrate/process/d6/FieldFormatterSettingsDefaults.php @@ -132,7 +132,8 @@ protected function numberSettings($type, $format) { ], ]; - return isset($map[$type][$format]) ? $map[$type][$format] : []; + $default = ['format_plural' => FALSE, 'format_plural_string' => '1' . LOCALE_PLURAL_DELIMITER . '@count']; + return isset($map[$type][$format]) ? $map[$type][$format] + $default : []; } } diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateFieldFormatterSettingsTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateFieldFormatterSettingsTest.php index 1516954..457ef19 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateFieldFormatterSettingsTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateFieldFormatterSettingsTest.php @@ -122,16 +122,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); diff --git a/core/modules/views/src/Entity/Render/EntityFieldRenderer.php b/core/modules/views/src/Entity/Render/EntityFieldRenderer.php index 66d4fcf..94201c4 100644 --- a/core/modules/views/src/Entity/Render/EntityFieldRenderer.php +++ b/core/modules/views/src/Entity/Render/EntityFieldRenderer.php @@ -196,6 +196,7 @@ protected function buildFields(array $values) { if ($values && ($field_ids = $this->getRenderableFieldIds())) { $entity_type_id = $this->getEntityTypeId(); + $settings_langcode = $this->view->storage->language()->getId(); // Collect the entities for the relationship, fetch the right translation, // and group by bundle. For each result row, the corresponding entity can @@ -230,6 +231,7 @@ protected function buildFields(array $values) { 'targetEntityType' => $entity_type_id, 'bundle' => $bundle, 'status' => TRUE, + 'langcode' => $settings_langcode, ]); foreach ($display_fields as $field_id => $field) { $display->setComponent($field->definition['field_name'], [ diff --git a/core/modules/views/src/Plugin/views/field/Field.php b/core/modules/views/src/Plugin/views/field/Field.php index 251d025..de15ef0 100644 --- a/core/modules/views/src/Plugin/views/field/Field.php +++ b/core/modules/views/src/Plugin/views/field/Field.php @@ -470,6 +470,8 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { $format = $this->options['type']; $settings = $this->options['settings'] + $this->formatterPluginManager->getDefaultSettings($format); + $settings_langcode = $this->view->storage->language()->getId(); + $settings['settings_langcode'] = $settings_langcode; $options = array( 'field_definition' => $field,