Skip to content
Snippets Groups Projects
Commit 7b030373 authored by Florent Torregrosa's avatar Florent Torregrosa Committed by Mikael Meulle
Browse files

Issue #3498184 by just_like_good_vibes, grimreaper, pdureau: Normalize number prop type

parent 15353e18
Branches
Tags 1.0.3
1 merge request!316Issue #3498184 by grimreaper: [2.0.0-rc2] Normalize number prop type
Pipeline #397750 passed
......@@ -39,4 +39,18 @@ class NumberPropType extends PropTypePluginBase {
return $summary;
}
/**
* {@inheritdoc}
*/
public static function normalize(mixed $value, ?array $definition = NULL): mixed {
if (NULL === $value) {
return NULL;
}
$type = (is_array($definition) && isset($definition['type'])) ? $definition['type'] : NULL;
if (($type === 'integer') || (is_array($type) && !in_array('number', $type, TRUE) && in_array('integer', $type, TRUE))) {
return (int) $value;
}
return (float) $value;
}
}
......@@ -50,7 +50,7 @@ class NumberWidget extends SourcePluginPropValue {
// "object" type to all props to "allows deferring rendering in Twig to the
// render pipeline". Remove it as soon as this weird mechanism is removed
// from SDC.
if (in_array("integer", $this->propDefinition["type"])) {
if (in_array("integer", $this->propDefinition["type"]) && !in_array("number", $this->propDefinition["type"], TRUE)) {
$form['value']['#step'] = 1;
}
if (isset($this->propDefinition["minimum"])) {
......
......@@ -16,7 +16,7 @@ use Drupal\ui_patterns\Plugin\UiPatterns\PropType\NumberPropType;
class NumberPropTypeTest extends PropTypeNormalizationTestBase {
/**
* Test normalize static method.
* Test normalize static method with prop number.
*
* @dataProvider normalizationTests
*/
......@@ -25,6 +25,16 @@ class NumberPropTypeTest extends PropTypeNormalizationTestBase {
$this->assertEquals($normalized, $expected);
}
/**
* Test normalize static method with prop integer.
*
* @dataProvider normalizationTestsInteger
*/
public function testNormalizationInteger(mixed $value, mixed $expected) : void {
$normalized = NumberPropType::normalize($value, $this->testComponentProps['integer']);
$this->assertEquals($normalized, $expected);
}
/**
* Test rendered component with prop.
*
......@@ -40,6 +50,23 @@ class NumberPropTypeTest extends PropTypeNormalizationTestBase {
public static function normalizationTests() : array {
return [
"null value" => [NULL, NULL],
"integer value" => [1, 1],
"float value" => [1.1, 1.1],
"string value" => ["1", 1],
"float string value" => ["1.1", 1.1],
];
}
/**
* Provides data for testNormalizationInteger.
*/
public static function normalizationTestsInteger() : array {
return [
"null value" => [NULL, NULL],
"integer value to integer" => [1, 1],
"float value to integer" => [1.1, 1],
"string value to integer" => ["1", 1],
"float string value to integer" => ["1.1", 1],
];
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment