Commit a6ea1f77 authored by masdzen's avatar masdzen
Browse files

syspage import

parent 75fa00b4
......@@ -5,6 +5,7 @@ namespace Drupal\synhelper\Drush;
use Drush\Commands\DrushCommands;
use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\File\FileSystemInterface;
/**
* A Drush commandfile.
......@@ -25,8 +26,98 @@ class DrushContentImport extends DrushCommands {
*/
public function import($directory) {
$this->output()->writeln("Import content from: $directory");
$storage = \Drupal::entityTypeManager()->getStorage('node');
$source = new FileStorage($directory);
foreach ($source->listAll() as $file) {
$data = $source->read($file);
$this->importNode($data);
}
}
/**
* 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();
}
/**
* Import.
*/
private function importParagraph($field_paragraph) {
$storage = \Drupal::service('entity_type.manager')->getStorage('paragraph');
$items = [];
foreach ($field_paragraph['field_about_items'] as $about_item) {
$item = $storage->create($about_item);
$item->save();
$items[] = [
'target_id' => $item->id(),
'target_revision_id' => $item->getRevisionId(),
];
}
$field_paragraph['field_about_items'] = $items;
$paragraphs = [];
$paragraph = $storage->create($field_paragraph);
$paragraph->save();
$paragraphs[] = [
'target_id' => $paragraph->id(),
'target_revision_id' => $paragraph->getRevisionId(),
];
return $paragraphs;
}
/**
* Import.
*/
private function importImage($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/";
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' => $destination,
'uri' => $uri,
'status' => 1,
]);
$file->save();
$fid = $file->id();
if (!empty($fid)) {
$result[$fid] = ['target_id' => $fid];
}
}
}
return $result;
}
}
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