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 27bb234..c240fab 100644 --- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php +++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/NumericFormatterBase.php @@ -78,6 +78,8 @@ public function viewElements(FieldItemListInterface $items) { $suffix = (count($suffixes) > 1) ? format_plural($item->value, $suffixes[0], $suffixes[1]) : $suffixes[0]; $output = $prefix . $output . $suffix; } + // Output the raw value in a content attribute if the text of the HTML + // element differs from the raw value (for example when a prefix is used). if ($item->value != $output) { if (!empty($item->_attributes)) { $item->_attributes += array('content' => $item->value); diff --git a/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php b/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php index c0bd8b9..a8761f7 100644 --- a/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php +++ b/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php @@ -134,7 +134,7 @@ protected function getAbsoluteUri($entity) { } /** - * Parse a content and return the html element. + * Parses a content and return the html element. * * @param string $content * The html to parse. diff --git a/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php index eb63059..ae094bb 100644 --- a/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php +++ b/core/modules/rdf/src/Tests/Field/NumberFieldRdfaTest.php @@ -22,7 +22,7 @@ public function testIntegerFormatter() { $this->fieldType = 'integer'; $testValue = 3; $this->createTestField(); - $this->createTestEntity(); + $this->createTestEntity($testValue); $this->assertFormatterRdfa(array('type' => $this->fieldType), 'http://schema.org/baseSalary', array('value' => $testValue)); // Test that the content attribute is not created. @@ -48,7 +48,7 @@ public function testIntegerFormatterWithSettings() { 'suffix' => ' llamas.', ); $this->createTestField($field_instance_settings); - $this->createTestEntity(); + $this->createTestEntity($testValue); $this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue)); // Test that the content attribute is created. @@ -63,7 +63,7 @@ public function testFloatFormatter() { $this->fieldType = 'float'; $testValue = 3.33; $this->createTestField(); - $this->createTestEntity(); + $this->createTestEntity($testValue); $this->assertFormatterRdfa(array('type' => $this->fieldType), 'http://schema.org/baseSalary', array('value' => $testValue)); // Test that the content attribute is not created. @@ -90,7 +90,7 @@ public function testFloatFormatterWithSettings() { 'suffix' => ' more.', ); $this->createTestField($field_instance_settings); - $this->createTestEntity(); + $this->createTestEntity($testValue); $this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue)); // Test that the content attribute is created. @@ -99,7 +99,7 @@ public function testFloatFormatterWithSettings() { } /** - * Tests the float formatter with a scale. + * Tests the float formatter with a scale. Scale is not exercised. */ public function testFloatFormatterWithScale() { $this->fieldType = 'float'; @@ -111,7 +111,7 @@ public function testFloatFormatterWithScale() { ); $testValue = 3.33; $this->createTestField(); - $this->createTestEntity(); + $this->createTestEntity($testValue); $this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue)); // Test that the content attribute is not created. @@ -120,17 +120,38 @@ public function testFloatFormatterWithScale() { } /** + * Tests the float formatter with a scale. Scale is exercised. + */ + public function testFloatFormatterWithScaleExercised() { + $this->fieldType = 'float'; + $formatter = array( + 'type' => $this->fieldType, + 'settings' => array( + 'scale' => 5, + ), + ); + $testValue = 3.1234567; + $this->createTestField(); + $this->createTestEntity($testValue); + $this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue)); + + // Test that the content attribute is created. + $result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content=:testValue]', array(':testValue' => $testValue)); + $this->assertTrue($result); + } + + /** * Tests the decimal formatter. */ public function testDecimalFormatter() { $this->fieldType = 'decimal'; $testValue = 3.33; $this->createTestField(); - $this->createTestEntity(); + $this->createTestEntity($testValue); $this->assertFormatterRdfa(array('type' => $this->fieldType), 'http://schema.org/baseSalary', array('value' => $testValue)); // Test that the content attribute is not created. - $result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content]', array(':testValue' => $testValue)); + $result = $this->xpathContent($this->getRawContent(), '//div[contains(@class, "field-item") and @content]'); $this->assertFalse($result); } @@ -153,7 +174,7 @@ public function testDecimalFormatterWithSettings() { 'suffix' => ' more.', ); $this->createTestField($field_instance_settings); - $this->createTestEntity(); + $this->createTestEntity($testValue); $this->assertFormatterRdfa($formatter, 'http://schema.org/baseSalary', array('value' => $testValue)); // Test that the content attribute is created. @@ -164,7 +185,7 @@ public function testDecimalFormatterWithSettings() { /** * Creates the RDF mapping for the field. */ - protected function createTestEntity() { + protected function createTestEntity($testValue) { // Add the mapping. $mapping = rdf_get_mapping('entity_test', 'entity_test'); $mapping->setFieldMapping($this->fieldName, array(