Commit 83d5df13 authored by Aleksey.Alekseev's avatar Aleksey.Alekseev
Browse files

Attributes import v1.0

parent 5b0310e0
......@@ -26,18 +26,18 @@ class DrushContentImport extends DrushCommands {
}
/**
* Synhelper Blocks import.
* Synhelper Contacts import.
*
* @param string $directory
* Content directory.
*
* @command synhelper:block_import
* @command synhelper:contacts_import
* @usage synhelper:scont $directory
* Blocks import from $directory/block.
* Contacts import from $directory/contacts.
*/
public function importBlocks($directory) {
$this->output()->writeln("Import Blocks from: $directory/block");
\Drupal::service('synhelper.content_import')->importBlocks($directory);
public function importContacts($directory) {
$this->output()->writeln("Import Contacts from: $directory/contacts");
\Drupal::service('synhelper.content_import')->importContacts($directory);
$this->output()->writeln("!NOTE Done.");
}
......@@ -58,18 +58,18 @@ class DrushContentImport extends DrushCommands {
}
/**
* Synhelper Products import.
* Synhelper Nodes import.
*
* @param string $directory
* Content directory.
*
* @command synhelper:product_import
* @command synhelper:node_import
* @usage synhelper:scont $directory
* Products import from $directory/products.
* Nodes import from $directory/nodes.
*/
public function importProducts($directory) {
$this->output()->writeln("Import Products from: $directory/products");
\Drupal::service('synhelper.content_import')->importProducts($directory);
public function importNode($directory) {
$this->output()->writeln("Import Nodes from: $directory/nodes");
\Drupal::service('synhelper.content_import')->importNode($directory);
$this->output()->writeln("!NOTE Done.");
}
......@@ -90,34 +90,34 @@ class DrushContentImport extends DrushCommands {
}
/**
* Synhelper Contacts import.
* Synhelper Products import.
*
* @param string $directory
* Content directory.
*
* @command synhelper:contacts_import
* @command synhelper:product_import
* @usage synhelper:scont $directory
* Contacts import from $directory/contacts.
* Products import from $directory/products.
*/
public function importContacts($directory) {
$this->output()->writeln("Import Contacts from: $directory/contacts");
\Drupal::service('synhelper.content_import')->importContacts($directory);
public function importProducts($directory) {
$this->output()->writeln("Import Products from: $directory/products");
\Drupal::service('synhelper.content_import')->importProducts($directory);
$this->output()->writeln("!NOTE Done.");
}
/**
* Synhelper Nodes import.
* Synhelper Blocks import.
*
* @param string $directory
* Content directory.
*
* @command synhelper:node_import
* @command synhelper:block_import
* @usage synhelper:scont $directory
* Nodes import from $directory/nodes.
* Blocks import from $directory/block.
*/
public function importNode($directory) {
$this->output()->writeln("Import Nodes from: $directory/nodes");
\Drupal::service('synhelper.content_import')->importNode($directory);
public function importBlocks($directory) {
$this->output()->writeln("Import Blocks from: $directory/block");
\Drupal::service('synhelper.content_import')->importBlocks($directory);
$this->output()->writeln("!NOTE Done.");
}
......
......@@ -9,7 +9,7 @@ use Drupal\Core\Config\FileStorage;
use Drupal\Core\File\FileSystemInterface;
use Drupal\commerce_product\Entity\ProductVariation;
use Drupal\commerce_product\Entity\Product;
use Drupal\commerce_price\Price;
use Drupal\commerce_product\Entity\ProductAttributeValue;
/**
* Defines Content Import.
......@@ -37,95 +37,6 @@ class SynhelperContentImport {
$this->setSynLandingConfig($directory);
}
/**
* Import Blocks.
*/
public function importBlocks(string $directory) {
$sourceBlock = new FileStorage($directory . '/block');
foreach ($sourceBlock->listAll() as $file) {
$data = $sourceBlock->read($file);
$storage = \Drupal::entityTypeManager()->getStorage('block_content');
foreach ($data as $key => $value) {
switch ($key) {
case 'field_image':
case 'field_gallery':
$b[$key] = $this->importImages($value);
break;
case 'field_attach':
$b[$key] = $this->importAttach($value);
break;
case 'field_paragraph':
$b[$key] = $this->importParagraph($value);
break;
default:
$b[$key] = $value;
break;
}
}
$b['uid'] = 1;
$block = $storage->create($b);
$block->save();
}
}
/**
* Import Taxonomy.
*/
public function importTaxonomy(string $directory) {
$sourceTaxonomy = new FileStorage($directory . '/taxonomy');
foreach ($sourceTaxonomy->listAll() as $file) {
$data = $sourceTaxonomy->read($file);
if (!$this->checkVocabularyExistence($data['vid'])) {
$this->createVocabulary($data);
}
$this->createTaxonomyTerm($data);
if (!empty($data['children_terms'])) {
$this->createChildTerms($data, $data['vid']);
}
}
}
/**
* Import Products.
*/
public function importProducts(string $directory) {
$sourceProducts = new FileStorage($directory . '/products');
foreach ($sourceProducts->listAll() as $file) {
$product = $sourceProducts->read($file);
if (!empty($product['variations'])) {
$variations = $this->importVariations($product);
}
$this->createProduct($product, $variations);
}
}
/**
* Import Menu.
*/
public function importMenu(string $directory) {
$sourceMenu = new FileStorage($directory . '/menu');
foreach ($sourceMenu->listAll() as $file) {
$data = $sourceMenu->read($file);
if ($menu = \Drupal::entityTypeManager()->getStorage('menu')->load($data['menu']['id'])) {
$menu->set('label', $data['menu']['label']);
$menu->set('description', $data['menu']['description']);
$menu->save();
}
else {
\Drupal::entityTypeManager()->getStorage('menu')->create([
'uid' => 1,
'id' => $data['menu']['id'],
'label' => $data['menu']['label'],
'description' => $data['menu']['description'],
])->save();
}
$this->addMenuLink($data['links'], NULL, $data['menu']['id']);
}
}
/**
* Import Contacts.
*/
......@@ -150,6 +61,23 @@ class SynhelperContentImport {
}
}
/**
* Import Taxonomy.
*/
public function importTaxonomy(string $directory) {
$sourceTaxonomy = new FileStorage($directory . '/taxonomy');
foreach ($sourceTaxonomy->listAll() as $file) {
$data = $sourceTaxonomy->read($file);
if (!$this->checkVocabularyExistence($data['vid'])) {
$this->createVocabulary($data);
}
$this->createTaxonomyTerm($data);
if (!empty($data['children_terms'])) {
$this->createChildTerms($data, $data['vid']);
}
}
}
/**
* Import Node.
* If you need to add a custom
......@@ -207,6 +135,78 @@ class SynhelperContentImport {
}
}
/**
* Import Menu.
*/
public function importMenu(string $directory) {
$sourceMenu = new FileStorage($directory . '/menu');
foreach ($sourceMenu->listAll() as $file) {
$data = $sourceMenu->read($file);
if ($menu = \Drupal::entityTypeManager()->getStorage('menu')->load($data['menu']['id'])) {
$menu->set('label', $data['menu']['label']);
$menu->set('description', $data['menu']['description']);
$menu->save();
}
else {
\Drupal::entityTypeManager()->getStorage('menu')->create([
'uid' => 1,
'id' => $data['menu']['id'],
'label' => $data['menu']['label'],
'description' => $data['menu']['description'],
])->save();
}
$this->addMenuLink($data['links'], NULL, $data['menu']['id']);
}
}
/**
* Import Products.
*/
public function importProducts(string $directory) {
$sourceProducts = new FileStorage($directory . '/products');
foreach ($sourceProducts->listAll() as $file) {
$product = $sourceProducts->read($file);
if (!empty($product['variations'])) {
$variations = $this->importVariations($product);
}
$this->createProduct($product, $variations);
}
}
/**
* Import Blocks.
*/
public function importBlocks(string $directory) {
$sourceBlock = new FileStorage($directory . '/block');
foreach ($sourceBlock->listAll() as $file) {
$data = $sourceBlock->read($file);
$storage = \Drupal::entityTypeManager()->getStorage('block_content');
foreach ($data as $key => $value) {
switch ($key) {
case 'field_image':
case 'field_gallery':
$b[$key] = $this->importImages($value);
break;
case 'field_attach':
$b[$key] = $this->importAttach($value);
break;
case 'field_paragraph':
$b[$key] = $this->importParagraph($value);
break;
default:
$b[$key] = $value;
break;
}
}
$b['uid'] = 1;
$block = $storage->create($b);
$block->save();
}
}
/**
* Set Synlanding form background.
*/
......@@ -236,12 +236,44 @@ class SynhelperContentImport {
*
*/
/**
* Import Attributes.
*/
public function importAttribute(array $fields, string $name) {
$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.
*/
private function importVariations(array $product) : array {
foreach ($product['variations'] as $vid => $variation_content) {
$variation = ProductVariation::create([
$v = [
'uid' => 1,
'id' => $vid,
'title' => $product['title'],
......@@ -252,7 +284,13 @@ class SynhelperContentImport {
'currency_code' => $variation_content['currency'],
],
'field_oldprice' => $variation_content['field_oldprice'],
]);
];
if (!empty($variation_content['attributes'])) {
foreach ($variation_content['attributes'] as $key => $value) {
$v["attribute_$key"] = $this->importAttribute($value, $key);
}
}
$variation = ProductVariation::create($v);
$variations[] = $variation;
$variation->save();
}
......
......@@ -5,7 +5,3 @@ core: 8.x
core_version_requirement: ^8 || ^9
# version: 1.0
package: Synapse
# Information added by Drupal.org packaging script on 2021-08-27
version: '8.x-1.44'
project: 'synhelper'
datestamp: 1630053286
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