protected function ProductVariationAttributeMapperTest::createAttributeSet in Commerce Core 8.2
Creates an attribute field and set of attribute values.
Parameters
\Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type: The variation type.
string $name: The attribute field name.
array $options: Associative array of key name values. [red => Red].
bool $required: Whether the created attribute should be required.
Return value
\Drupal\commerce_product\Entity\ProductAttributeValueInterface[] Array of attribute entities.
2 calls to ProductVariationAttributeMapperTest::createAttributeSet()
- ProductVariationAttributeMapperTest::setUp in modules/
product/ tests/ src/ Kernel/ ProductVariationAttributeMapperTest.php - ProductVariationAttributeMapperTest::testThreeAttributesSixVariations in modules/
product/ tests/ src/ Kernel/ ProductVariationAttributeMapperTest.php - Tests having three attributes and six variations.
File
- modules/
product/ tests/ src/ Kernel/ ProductVariationAttributeMapperTest.php, line 648
Class
- ProductVariationAttributeMapperTest
- Tests the product variation attribute mapper.
Namespace
Drupal\Tests\commerce_product\KernelCode
protected function createAttributeSet(ProductVariationTypeInterface $variation_type, $name, array $options, $required = TRUE) {
$attribute = ProductAttribute::create([
'id' => $name,
'label' => ucfirst($name),
]);
$attribute
->save();
$this->attributeFieldManager
->createField($attribute, $variation_type
->id());
// The field is always created as required by default.
if (!$required) {
$field = FieldConfig::loadByName('commerce_product_variation', $variation_type
->id(), 'attribute_' . $name);
$field
->setRequired(FALSE);
$field
->save();
}
$attribute_set = [];
foreach ($options as $key => $value) {
$attribute_set[$key] = $this
->createAttributeValue($name, $value);
}
return $attribute_set;
}