Commit 3f1ce308 authored by Aleksey.Alekseev's avatar Aleksey.Alekseev
Browse files

Fix paragraph import + add more variable node import + add existing menu import

parent a634c37c
......@@ -3,22 +3,19 @@
namespace Drupal\synhelper\Drush;
use Drush\Commands\DrushCommands;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Url;
use Drupal\media\Entity\Media;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\File\FileSystemInterface;
use Drupal\menu_link_content\Entity\MenuLinkContent;
// use App\Utility\StepBase;
// use Symfony\Component\Yaml\Yaml;
/**
* A Drush commandfile.
*/
class DrushContentImport extends DrushCommands {
/**
* Synhrlper content import.
* Synhelper content import.
*
* @param string $directory
* Content directory.
......@@ -33,12 +30,13 @@ class DrushContentImport extends DrushCommands {
* $directory/nodes.
* Import taxonomy from
* $directory/taxonomy.
* Import menu from
* $directory/menu.
* Resets SynLandyng config from
* $directory/synlanding.
*/
public function import($directory) {
$this->output()->writeln("Import content from: $directory");
$contactsDirectory = $directory . '/contacts';
$this->output()->writeln("Import Contacts from: $contactsDirectory");
$sourceContacts = new FileStorage($contactsDirectory);
......@@ -66,6 +64,15 @@ class DrushContentImport extends DrushCommands {
}
$this->output()->writeln("!NOTE Done.");
$menuDirectory = $directory . '/menu';
$this->output()->writeln("Import Menu from: $menuDirectory");
$sourceMenu = new FileStorage($menuDirectory);
foreach ($sourceMenu->listAll() as $file) {
$data = $sourceMenu->read($file);
$this->importMenu($data);
}
$this->output()->writeln("!NOTE Done.");
$synLandingDirectory = $directory . '/synlanding';
$this->output()->writeln("Import Image for SynLanding(form_bg) from: $synLandingDirectory");
$sourceSynLanding = new FileStorage($synLandingDirectory);
......@@ -73,64 +80,71 @@ class DrushContentImport extends DrushCommands {
$data = $sourceSynLanding->read($file);
$this->setSynLandingConfig($data);
}
$this->output()->writeln("!NOTE Done.");
}
/**
* Import Paragraph.
* Import Paragraph(about && image_blocks).
* If needed more just copy one of them and
* set your fields
*/
private function importParagraph($field_paragraph) {
$storage = \Drupal::service('entity_type.manager')->getStorage('paragraph');
/* Importing About Us */
$items = [];
$about = $field_paragraph['about'];
foreach ($about['field_about_items'] as $about_item) {
$item = $storage->create($about_item);
$item->save();
$items[] = [
'type' => $about_item['type'],
'target_id' => $item->id(),
'target_revision_id' => $item->getRevisionId(),
'field_title' => $about_item['field_title'],
'field_description' => $about_item['field_description'],
if (!empty($field_paragraph['about'])) {
$items = [];
$about = $field_paragraph['about'];
foreach ($about['field_about_items'] as $about_item) {
$item = $storage->create($about_item);
$item->save();
$items[] = [
'type' => $about_item['type'],
'target_id' => $item->id(),
'target_revision_id' => $item->getRevisionId(),
'field_title' => $about_item['field_title'],
'field_description' => $about_item['field_description'],
];
}
$about['field_about_items'] = $items;
$paragraph = $storage->create($about);
$paragraph->save();
$paragraphs = [];
$paragraphs[] = [
'type' => $about['type'],
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
];
}
$about['field_about_items'] = $items;
$paragraph = $storage->create($about);
$paragraph->save();
$paragraphs = [];
$paragraphs[] = [
'type' => $about['type'],
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
];
/* Importing Image Blocks */
$items = [];
$imageBloсks = $field_paragraph['image_blocks'];
foreach ($imageBloсks['field_image_blocks_items'] as $image_blocks_item) {
$image_blocks_item['field_image'] = $this->importImages($image_blocks_item['field_image']);
$item = $storage->create($image_blocks_item);
$item->save();
$items[] = [
'type' => $image_blocks_item['type'],
'target_id' => $item->id(),
'target_revision_id' => $item->getRevisionId(),
'field_title' => $image_blocks_item['field_title'],
'field_body' => $image_blocks_item['field_body'],
'field_image_text_sort' => $image_blocks_item['field_image_text_sort'],
if (!empty($field_paragraph['image_blocks'])) {
$items = [];
$imageBloсks = $field_paragraph['image_blocks'];
foreach ($imageBloсks['field_image_blocks_items'] as $image_blocks_item) {
$image_blocks_item['field_image'] = $this->importImages($image_blocks_item['field_image']);
$item = $storage->create($image_blocks_item);
$item->save();
$items[] = [
'type' => $image_blocks_item['type'],
'target_id' => $item->id(),
'target_revision_id' => $item->getRevisionId(),
'field_title' => $image_blocks_item['field_title'],
'field_body' => $image_blocks_item['field_body'],
'field_image_text_sort' => $image_blocks_item['field_image_text_sort'],
];
}
$imageBloсks['field_image_blocks_items'] = $items;
$paragraph = $storage->create($imageBloсks);
$paragraph->save();
$paragraphs[] = [
'type' => $imageBloсks['type'],
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
];
}
$imageBloсks['field_image_blocks_items'] = $items;
$paragraph = $storage->create($imageBloсks);
$paragraph->save();
$paragraphs[] = [
'type' => $imageBloсks['type'],
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
];
return $paragraphs;
}
......@@ -142,13 +156,14 @@ class DrushContentImport extends DrushCommands {
$t = [
'vid' => $data['vid'],
'name' => $data['name'],
'field_catalog_image' => $this->importImages($data['field_catalog_image']),
];
$taxonomyTerm = $storage->create($t);
$taxonomyTerm->save();
}
/**
* Import TaxonomyField.
* Import TaxonomyField by name or name and vocabulary.
*/
private function importTaxonomyField($taxonomyTermName) {
if (!empty($taxonomyTermName)) {
......@@ -157,11 +172,58 @@ class DrushContentImport extends DrushCommands {
}
}
/**
* Import Menu.
*/
private function importMenu($data) {
// TODO: Create menu
// \Drupal::entityTypeManager()->getStorage('menu')->create([
// 'id' => $data['menu']['id'],
// 'label' => 'Test menu',
// 'description' => 'Description text',
// ])->save();
$this->addMenuLink($data['links'], NULL);
}
/**
* Import Menu Links.
*/
private function addMenuLink($links, $parent_id) {
if (empty($links)) {
return;
}
$link = \Drupal::entityTypeManager()->getStorage('menu_link_content');
foreach ($links as $menu_link) {
$t = [
'title' => $menu_link['title'],
'link' => ['uri' => $menu_link['uri']],
'menu_name' => 'main',
'expanded' => $menu_link['expanded'],
'weight' => $menu_link['weight'],
'parent' => $parent_id,
];
$children = $menu_link['children'];
$menu_link = $link->create($t);
$menu_link->save();
if (!empty($children[0])) {
$bundle = $menu_link->bundle();
$uuid = $menu_link->uuid();
$parent_id1 = $bundle . ":" . $uuid;
}
else {
$parent_id1 = NULL;
}
$this->addMenuLink($children, $parent_id1);
}
}
/**
* Import Contacts.
*/
private function importContacts($data) {
$path = \Drupal::service('path_alias.manager')->getPathByAlias('/kontakty');
$path = \Drupal::service('path_alias.manager')->getPathByAlias('/contacts');
$params = Url::fromUri("internal:" . $path)->getRouteParameters();
$entity_type = key($params);
......@@ -173,106 +235,39 @@ class DrushContentImport extends DrushCommands {
/**
* Import Node.
* If you need to add a custom
* image/gallery, attach or taxonomy field,
* add a new 'case' to existing ones.
*/
private function importNode($data) {
$storage = \Drupal::entityTypeManager()->getStorage('node');
switch ($data['type']) {
case 'project':
$n = [
'uid' => 1,
'type' => $data['type'],
'title' => $data['title'],
'status' => $data['status'],
'path' => $data['path'],
'created' => $data['created'],
'changed' => $data['changed'],
'promote' => $data['promote'],
'sticky' => $data['sticky'],
'field_short' => $data['field_short'],
'field_size' => $data['field_size'],
'field_material' => $data['field_material'],
'field_area' => $data['field_area'],
'body' => $data['body'],
'field_count_floors' => $data['field_count_floors'],
'field_count_rooms' => $data['field_count_rooms'],
'field_price' => $data['field_price'],
'field_object_type' => $this->importTaxonomyField($data['field_object_type']),
'field_image' => $this->importImages($data['field_image']),
'field_attach' => $this->importAttach($data['field_attach']),
'field_gallery' => $this->importImages($data['field_gallery']),
];
break;
foreach ($data as $key => $value) {
switch ($key) {
case 'field_image':
case 'field_gallery':
$n[$key] = $this->importImages($value);
break;
case 'portfolio':
$n = [
'uid' => 1,
'type' => $data['type'],
'title' => $data['title'],
'status' => $data['status'],
'path' => $data['path'],
'created' => $data['created'],
'changed' => $data['changed'],
'promote' => $data['promote'],
'sticky' => $data['sticky'],
'field_short' => $data['field_short'],
'body' => $data['body'],
'field_image' => $this->importImages($data['field_image']),
'field_attach' => $this->importAttach($data['field_attach']),
'field_gallery' => $this->importImages($data['field_gallery']),
];
break;
case 'field_attach':
$n[$key] = $this->importAttach($value);
break;
case 'page':
$n = [
'uid' => 1,
'type' => $data['type'],
'title' => $data['title'],
'status' => $data['status'],
'path' => $data['path'],
'created' => $data['created'],
'changed' => $data['changed'],
'promote' => $data['promote'],
'sticky' => $data['sticky'],
'body' => $data['body'],
'field_metatag' => $data['field_metatag'],
'field_attach' => $this->importAttach($data['field_attach']),
];
break;
case 'field_type_service':
case 'field_object_type':
$n[$key] = $this->importAttach($value);
break;
case 'service':
$n = [
'uid' => 1,
'type' => $data['type'],
'title' => $data['title'],
'status' => $data['status'],
'path' => $data['path'],
'created' => $data['created'],
'changed' => $data['changed'],
'promote' => $data['promote'],
'sticky' => $data['sticky'],
'field_service_info' => $data['field_service_info'],
'field_short' => $data['field_short'],
'field_metatag' => $data['field_metatag'],
'field_type_service' => $this->importTaxonomyField($data['field_type_service']),
'field_attach' => $this->importAttach($data['field_attach']),
'field_paragraph' => $this->importParagraph($data['field_paragraph']),
];
break;
case 'field_paragraph':
$n[$key] = $this->importParagraph($value);
break;
default:
$n[$key] = $value;
break;
}
default:
$n = [
'uid' => 1,
'type' => $data['type'],
'title' => $data['title'],
'status' => $data['status'],
'path' => $data['path'],
'field_short' => $data['field_short'],
'field_metatag' => $data['field_metatag'],
'field_image' => $this->importImages($data['field_image']),
'field_paragraph' => $this->importParagraph($data['field_paragraph']),
];
break;
}
$node = $storage->create($n);
if (!empty($data['field_image_big'])) {
$node->set('field_image_big', $this->importMedia($data['field_image_big']));
......@@ -287,7 +282,7 @@ class DrushContentImport extends DrushCommands {
}
/**
* Set SynlandingConfig for form_bg.
* Set Synlanding form background.
*/
private function setSynLandingConfig($data) {
$image = $this->importImages($data['form_bg_path']);
......@@ -438,4 +433,4 @@ class DrushContentImport extends DrushCommands {
return !empty($term) ? $term->id() : 0;
}
}
}
\ No newline at end of file
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