Commit aa2fc6f5 authored by Aleksey.Alekseev's avatar Aleksey.Alekseev
Browse files

Attributes import v2.0

parent aaa278df
......@@ -7,6 +7,7 @@ use Drupal\media\Entity\Media;
use Drupal\pathauto\PathautoState;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_product\Entity\ProductAttributeValue;
......@@ -236,39 +237,6 @@ class SynhelperContentImport {
*
*/
/**
* Import Attributes.
*/
public function importAttribute(array $fields, string $name) {
// TODO: check for existence.
$a = [
'attribute' => $name,
];
foreach ($fields as $key => $value) {
switch ($key) {
case 'field_image':
case 'field_gallery':
$a[$key] = $this->importImages($value);
break;
case 'field_attach':
$a[$key] = $this->importAttach($value);
break;
case 'field_paragraph':
$a[$key] = $this->importParagraph($value);
break;
default:
$a[$key] = $value;
break;
}
}
$attribute = ProductAttributeValue::create($a);
$attribute->save();
return $attribute;
}
/**
* Imports product variations.
*/
......@@ -298,6 +266,62 @@ class SynhelperContentImport {
return $variations;
}
/**
* Import Attributes.
*/
public function importAttribute(array $fields, string $name) {
$ids = $this->issetAttribute($fields, $name);
if (empty($ids)) {
$a = [
'attribute' => $name,
];
foreach ($fields as $key => $value) {
switch ($key) {
case 'field_image':
case 'field_gallery':
$a[$key] = $this->importImages($value);
break;
case 'field_hex':
$a[$key] = strtoupper($value);
break;
case 'field_attach':
$a[$key] = $this->importAttach($value);
break;
case 'field_paragraph':
$a[$key] = $this->importParagraph($value);
break;
default:
$a[$key] = $value;
break;
}
}
$attribute = ProductAttributeValue::create($a);
$attribute->save();
return $attribute;
}
else {
return ProductAttributeValue::load(array_shift($ids));
}
}
/**
* Check already created attributes.
*/
public function issetAttribute(array $fields, string $name) {
$query = \Drupal::entityQuery('commerce_product_attribute_value');
if ($name == 'color') {
$query->condition('field_hex', $fields['field_hex']);
}
else {
$query->condition('name', $fields['name']);
}
return $query->execute();
}
/**
* Creates product.
*/
......@@ -460,7 +484,7 @@ class SynhelperContentImport {
}
/**
* Import Paragraph(about && image_blocks).
* Import Paragraph(about && image_blocks && Параметры).
* If needed more just copy one of them and set your fields.
*/
private function importParagraph($field_paragraph) : array {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment