Skip to content

Commit 056f69d

Browse files
committed
Issue #2956944 by bojanz: [Regression] PreparedAttribute doesn't accept optional attributes
1 parent 6c18289 commit 056f69d

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

modules/product/src/PreparedAttribute.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class PreparedAttribute {
3535
*
3636
* @var bool
3737
*/
38-
protected $required = TRUE;
38+
protected $required;
3939

4040
/**
4141
* The attribute values.
@@ -51,7 +51,7 @@ final class PreparedAttribute {
5151
* The definition.
5252
*/
5353
public function __construct(array $definition) {
54-
foreach (['id', 'label', 'element_type', 'required', 'values'] as $required_property) {
54+
foreach (['id', 'label', 'element_type', 'values'] as $required_property) {
5555
if (empty($definition[$required_property])) {
5656
throw new \InvalidArgumentException(sprintf('Missing required property "%s".', $required_property));
5757
}
@@ -63,7 +63,7 @@ public function __construct(array $definition) {
6363
$this->id = $definition['id'];
6464
$this->label = $definition['label'];
6565
$this->elementType = $definition['element_type'];
66-
$this->required = $definition['required'];
66+
$this->required = isset($definition['required']) ? $definition['required'] : TRUE;
6767
$this->values = $definition['values'];
6868
}
6969

modules/product/tests/src/Kernel/ProductVariationAttributeMapperTest.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Drupal\commerce_product\Entity\ProductVariation;
99
use Drupal\commerce_product\Entity\ProductVariationType;
1010
use Drupal\commerce_product\Entity\ProductVariationTypeInterface;
11+
use Drupal\field\Entity\FieldConfig;
1112
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
1213

1314
/**
@@ -122,7 +123,7 @@ protected function setUp() {
122123
'1tb' => '1TB',
123124
'2tb' => '2TB',
124125
'3tb' => '3TB',
125-
]);
126+
], FALSE);
126127
}
127128

128129
/**
@@ -288,7 +289,7 @@ public function testPrepareAttributesOptional() {
288289
$this->assertEquals('disk2', $disk2_attribute->getId());
289290
$this->assertEquals('Disk2', $disk2_attribute->getLabel());
290291
$this->assertEquals('select', $disk2_attribute->getElementType());
291-
$this->assertTrue($disk2_attribute->isRequired());
292+
$this->assertFalse($disk2_attribute->isRequired());
292293
$this->assertEquals(['_none' => ''], $disk2_attribute->getValues());
293294

294295
// Test from the 16GB x 1TB x None variation.
@@ -312,7 +313,7 @@ public function testPrepareAttributesOptional() {
312313
$this->assertEquals('disk2', $disk2_attribute->getId());
313314
$this->assertEquals('Disk2', $disk2_attribute->getLabel());
314315
$this->assertEquals('select', $disk2_attribute->getElementType());
315-
$this->assertTrue($disk2_attribute->isRequired());
316+
$this->assertFalse($disk2_attribute->isRequired());
316317
$this->assertEquals(['_none' => '', '17' => '1TB'], $disk2_attribute->getValues());
317318
}
318319

@@ -375,7 +376,7 @@ public function testMutuallyExclusiveAttributeMatrixTwoByTwoByTwo() {
375376
$this->assertEquals('disk2', $disk2_attribute->getId());
376377
$this->assertEquals('Disk2', $disk2_attribute->getLabel());
377378
$this->assertEquals('select', $disk2_attribute->getElementType());
378-
$this->assertTrue($disk2_attribute->isRequired());
379+
$this->assertFalse($disk2_attribute->isRequired());
379380
$this->assertEquals(['18' => '2TB'], $disk2_attribute->getValues());
380381

381382
// Test 8GB x 1TB x 2TB.
@@ -401,7 +402,7 @@ public function testMutuallyExclusiveAttributeMatrixTwoByTwoByTwo() {
401402
$this->assertEquals('disk2', $disk2_attribute->getId());
402403
$this->assertEquals('Disk2', $disk2_attribute->getLabel());
403404
$this->assertEquals('select', $disk2_attribute->getElementType());
404-
$this->assertTrue($disk2_attribute->isRequired());
405+
$this->assertFalse($disk2_attribute->isRequired());
405406
// There should only be one Disk 2 option, since the other 8GB RAM option
406407
// has a Disk 1 value of 2TB.
407408
$this->assertEquals(['18' => '2TB'], $disk2_attribute->getValues());
@@ -427,7 +428,7 @@ public function testMutuallyExclusiveAttributeMatrixTwoByTwoByTwo() {
427428
$this->assertEquals('disk2', $disk2_attribute->getId());
428429
$this->assertEquals('Disk2', $disk2_attribute->getLabel());
429430
$this->assertEquals('select', $disk2_attribute->getElementType());
430-
$this->assertTrue($disk2_attribute->isRequired());
431+
$this->assertFalse($disk2_attribute->isRequired());
431432
// There should only be one Disk 2 option, since the other 8GB RAM option
432433
// has a Disk 1 value of 2TB.
433434
$this->assertEquals(['17' => '1TB'], $disk2_attribute->getValues());
@@ -635,17 +636,25 @@ protected function generateThreeByTwoOptionalScenario() {
635636
* The attribute field name.
636637
* @param array $options
637638
* Associative array of key name values. [red => Red].
639+
* @param bool $required
640+
* Whether the created attribute should be required.
638641
*
639642
* @return \Drupal\commerce_product\Entity\ProductAttributeValueInterface[]
640643
* Array of attribute entities.
641644
*/
642-
protected function createAttributeSet(ProductVariationTypeInterface $variation_type, $name, array $options) {
645+
protected function createAttributeSet(ProductVariationTypeInterface $variation_type, $name, array $options, $required = TRUE) {
643646
$attribute = ProductAttribute::create([
644647
'id' => $name,
645648
'label' => ucfirst($name),
646649
]);
647650
$attribute->save();
648651
$this->attributeFieldManager->createField($attribute, $variation_type->id());
652+
// The field is always created as required by default.
653+
if (!$required) {
654+
$field = FieldConfig::loadByName('commerce_product_variation', $variation_type->id(), 'attribute_' . $name);
655+
$field->setRequired(FALSE);
656+
$field->save();
657+
}
649658

650659
$attribute_set = [];
651660
foreach ($options as $key => $value) {

0 commit comments

Comments
 (0)