Commit 9b23b7bd authored by APolitsin's avatar APolitsin
Browse files

Update DrushContentImport.php

parent a6ea1f77
......@@ -3,80 +3,131 @@
namespace Drupal\synhelper\Drush;
use Drush\Commands\DrushCommands;
use Symfony\Component\Yaml\Yaml;
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 App\Utility\StepBase;
// use Symfony\Component\Yaml\Yaml;
/**
* A Drush commandfile.
*/
class DrushContentImport extends DrushCommands {
/**
* Synhrlper config import.
* Synhrlper content import.
*
* @param string $directory
* Config directory.
* Content directory.
*
* @command synhelper:content
* @aliases scont
* @options arr An option that takes multiple values.
* @usage synhelper:scont /var/www/content
* Import config from /var/www/content.
* @usage synhelper:scont /var/www/console/src/Template/solution/content/
* Import сontacts from
* /var/www/console/src/Template/solution/content/contacts.
* Import nodes from
* /var/www/console/src/Template/solution/content/nodes.
* Import taxonomy from
* /var/www/console/src/Template/solution/content/taxonomy.
* Resets SynLandyng config from
* /var/www/console/src/Template/solution/content/synlanding.
*/
public function import($directory) {
$this->output()->writeln("Import content from: $directory");
$source = new FileStorage($directory);
foreach ($source->listAll() as $file) {
$data = $source->read($file);
$contactsDirectory = $directory . '/contacts';
$this->output()->writeln("Import Contacts from: $contactsDirectory");
$sourceContacts = new FileStorage($contactsDirectory);
foreach ($sourceContacts->listAll() as $file) {
$data = $sourceContacts->read($file);
$this->importContacts($data);
}
$this->output()->writeln("!NOTE Done.");
$taxonomyDirectory = $directory . '/taxonomy';
$this->output()->writeln("Import Taxonomy from: $taxonomyDirectory");
$sourceTaxonomy = new FileStorage($taxonomyDirectory);
foreach ($sourceTaxonomy->listAll() as $file) {
$data = $sourceTaxonomy->read($file);
$this->importTaxonomy($data);
}
$this->output()->writeln("!NOTE Done.");
$nodeDirectory = $directory . '/nodes';
$this->output()->writeln("Import Nodes from: $nodeDirectory");
$sourceNode = new FileStorage($nodeDirectory);
foreach ($sourceNode->listAll() as $file) {
$data = $sourceNode->read($file);
$this->importNode($data);
}
}
$this->output()->writeln("!NOTE Done.");
/**
* Import.
*/
private function importNode($data) {
$storage = \Drupal::entityTypeManager()->getStorage('node');
$n = [
'type' => $data['type'],
'title' => $data['title'],
'created' => $data['created'],
'changed' => $data['changed'],
'path' => $data['path'],
'uid' => 1,
'body' => $data['body'],
'field_short' => $data['field_short'],
'field_image' => $this->importImage($data['field_image']),
'field_paragraph' => $this->importParagraph($data['field_paragraph']['about']),
];
$node = $storage->create($n);
$node->save();
$id = $node->id();
$config = \Drupal::service('config.factory')->getEditable('system.site');
$config->set('page.front', "/node/$id");
$config->save();
$synLandingDirectory = $directory . '/synlanding';
$this->output()->writeln("Import Image for SynLanding(form_bg) from: $synLandingDirectory");
$sourceSynLanding = new FileStorage($synLandingDirectory);
foreach ($sourceSynLanding->listAll() as $file) {
$data = $sourceSynLanding->read($file);
$this->setSynLandingConfig($data);
}
$this->output()->writeln("!NOTE Done.");
}
/**
* Import.
* Import Paragraph.
*/
private function importParagraph($field_paragraph) {
$storage = \Drupal::service('entity_type.manager')->getStorage('paragraph');
/* Importing About Us */
$items = [];
foreach ($field_paragraph['field_about_items'] as $about_item) {
$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'],
];
}
$field_paragraph['field_about_items'] = $items;
$about['field_about_items'] = $items;
$paragraph = $storage->create($about);
$paragraph->save();
$paragraphs = [];
$paragraph = $storage->create($field_paragraph);
$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'],
];
}
$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(),
];
......@@ -84,9 +135,157 @@ class DrushContentImport extends DrushCommands {
}
/**
* Import.
* Import Taxonomy.
*/
private function importImage($filenames) {
private function importTaxonomy($data) {
$storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$t = [
'vid' => $data['vid'],
'name' => $data['name'],
];
$taxonomyTerm = $storage->create($t);
$taxonomyTerm->save();
}
/**
* Import TaxonomyField.
*/
private function importTaxonomyField($taxonomyTermName) {
if (!empty($taxonomyTermName)) {
$taxonomyTermId = $this->getTidByName($taxonomyTermName);
return $taxonomyTermId;
}
}
/**
* Import Contacts.
*/
private function importContacts($data) {
$path = \Drupal::service('path_alias.manager')->getPathByAlias('/kontakty');
$params = Url::fromUri("internal:" . $path)->getRouteParameters();
$entity_type = key($params);
$node = \Drupal::entityTypeManager()->getStorage($entity_type)->load($params[$entity_type]);
$node->body->format = $data['body']['format'];
$node->body->value = $data['body']['value'];
$node->save();
}
/**
* Import Node.
*/
private function importNode($data) {
$storage = \Drupal::entityTypeManager()->getStorage('node');
if ($data['type'] == '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']),
];
}
elseif ($data['type'] == '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']),
];
}
elseif ($data['type'] == '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']),
];
}
elseif ($data['type'] == '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']),
];
}
$node = $storage->create($n);
if (!empty($data['field_image_big'])) {
$node->set('field_image_big', $this->importMedia($data['field_image_big']));
}
$node->save();
if (array_key_exists('is_front', $data) && $data['is_front'] == TRUE) {
$id = $node->id();
$config = \Drupal::service('config.factory')->getEditable('system.site');
$config->set('page.front', "/node/$id");
$config->save();
}
}
/**
* Set SynlandingConfig for form_bg.
*/
private function setSynLandingConfig($data) {
$image = $this->importImages($data['form_bg_path']);
$idFile = array_shift($image)['target_id'];
$config = \Drupal::config($data['config_id']);
\Drupal::configFactory()
->getEditable($data['config_id'])
->set('form_bg', [$idFile])
->save();
}
/**
* Import File.
*
* @TODO: FileSystemInterface::EXISTS_RENAME.
*/
private function importFiles($filenames) {
$file_storage = \Drupal::service('entity_type.manager')->getStorage('file');
$tmp = "/tmp";
$result = [];
......@@ -104,20 +303,120 @@ class DrushContentImport extends DrushCommands {
$filename = end($filename);
$f = file_get_contents("$fname");
$destination = "{$directory}{$filename}";
$uri = \Drupal::service('file_system')->saveData($f, $destination, FileSystemInterface::EXISTS_REPLACE);
$uri = \Drupal::service('file_system')->saveData($f, $destination, FileSystemInterface::EXISTS_RENAME);
$file = $file_storage->create([
'filename' => $destination,
'filename' => $filename,
'uri' => $uri,
'status' => 1,
]);
$file->save();
$fid = $file->id();
if (!empty($fid)) {
$result[$fid] = ['target_id' => $fid];
$result[$fid] = [
'target_id' => $fid,
];
}
}
}
return $result;
}
/**
* Import Images.
*/
private function importImages($images) {
if (!empty($images)) {
$files = $this->importFiles($images);
return $files;
}
}
/**
* Import Attach.
*/
private function importAttach($attachFiles) {
if (!empty($attachFiles)) {
$i = 0;
$filenames = [];
$descriptions = [];
foreach ($attachFiles as $attach) {
$filenames[$i] = $attach['source'];
$descriptions[$i] = $attach['description'];
$i++;
}
$files = $this->importFiles($filenames);
$i = 0;
foreach ($files as $k => $file) {
$files[$k]['description'] = $descriptions[$i];
$files[$k]['display'] = 1;
$i++;
}
return $files;
}
}
/**
* Import Media.
*
* @TODO: FileSystemInterface::EXISTS_RENAME.
*/
private function importMedia($filenames) {
$file_storage = \Drupal::service('entity_type.manager')->getStorage('file');
$tmp = "/tmp";
$result = [];
if (is_string($filenames)) {
$filenames = [$filenames];
}
if (is_array($filenames) && !empty($filenames)) {
$fs = \Drupal::service('file_system');
$directory = "public://import/media";
if (!file_exists($fs->realpath($directory))) {
\Drupal::service('file_system')->mkdir($directory, NULL, TRUE);
}
foreach ($filenames as $fname) {
$filename = explode("/", $fname);
$filename = end($filename);
$f = file_get_contents("$fname");
$destination = "{$directory}{$filename}";
$uri = \Drupal::service('file_system')->saveData($f, $destination, FileSystemInterface::EXISTS_REPLACE);
$file = $file_storage->create([
'filename' => $filename,
'uri' => $uri,
'status' => 1,
]);
$file->save();
$fid = $file->id();
$media = Media::create([
'bundle' => 'image',
'uid' => 1,
'field_media_image' => [
'target_id' => $fid,
],
]);
$media->setName($filename)->setPublished(TRUE)->save();
$mid = $media->id();
}
if (!empty($mid)) {
$result[] = $mid;
}
}
return $result;
}
/**
* Get taxonomy term id by name or by name and vocabulary id.
*/
private function getTidByName($name = NULL, $vid = NULL) {
$properties = [];
if (!empty($name)) {
$properties['name'] = $name;
}
if (!empty($vid)) {
$properties['vid'] = $vid;
}
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties($properties);
$term = reset($terms);
return !empty($term) ? $term->id() : 0;
}
}
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