protected function CsvTestBase::createAttribute in Commerce Migrate 8.2
Same name and namespace in other branches
- 3.1.x tests/src/Kernel/CsvTestBase.php \Drupal\Tests\commerce_migrate\Kernel\CsvTestBase::createAttribute()
- 3.0.x tests/src/Kernel/CsvTestBase.php \Drupal\Tests\commerce_migrate\Kernel\CsvTestBase::createAttribute()
Creates attributes.
Parameters
array $attributes: The attribute names to create.
3 calls to CsvTestBase::createAttribute()
- AttributeValueTest::setUp in modules/
csv_example/ tests/ src/ Kernel/ Migrate/ AttributeValueTest.php - ProductTest::setUp in modules/
csv_example/ tests/ src/ Kernel/ Migrate/ ProductTest.php - ProductVariationTest::setUp in modules/
csv_example/ tests/ src/ Kernel/ Migrate/ ProductVariationTest.php
File
- tests/
src/ Kernel/ CsvTestBase.php, line 111
Class
- CsvTestBase
- Test base for migrations tests with CSV source file.
Namespace
Drupal\Tests\commerce_migrate\KernelCode
protected function createAttribute(array $attributes) {
if (is_array($attributes)) {
foreach ($attributes as $attribute) {
$id = strtolower($attribute);
$id = preg_replace('/[^a-z0-9_]+/', '_', $id);
preg_replace('/_+/', '_', $id);
$field_name = 'attribute_' . $id;
$field_storage_definition = [
'field_name' => $field_name,
'entity_type' => 'commerce_product_variation',
'type' => 'entity_reference',
'cardinality' => 1,
'settings' => [
'target_type' => 'commerce_product_attribute_value',
],
];
$storage = FieldStorageConfig::create($field_storage_definition);
$storage
->save();
$field_instance = [
'field_name' => $field_name,
'entity_type' => 'commerce_product_variation',
'bundle' => 'default',
'label' => $attribute,
'settings' => [
'handler' => 'default:commerce_product_attribute_value',
'handler_settings' => [
'target_bundles' => [
$id,
],
],
],
];
$field = FieldConfig::create($field_instance);
$field
->save();
$ret = ProductAttribute::create([
'id' => strtolower($id),
'label' => $attribute,
]);
$ret
->save();
}
}
}