Skip to content
Snippets Groups Projects
Commit 48c17754 authored by Oleksandr Kuzava's avatar Oleksandr Kuzava
Browse files

Fix node alias issue. Improve file importing.

parent 608ea1ec
No related branches found
Tags 1.1.0-rc4
No related merge requests found
......@@ -4,4 +4,4 @@ services:
arguments: ['@entity_type.manager', '@module_handler']
single_content_sync.importer:
class: Drupal\single_content_sync\ContentImporter
arguments: [ '@entity_type.manager', '@entity.repository', '@module_handler']
arguments: [ '@entity_type.manager', '@entity.repository', '@module_handler', '@file_system']
......@@ -8,6 +8,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\field\FieldConfigInterface;
use Drupal\media\MediaInterface;
......@@ -67,7 +68,7 @@ class ContentExporter implements ContentExporterInterface {
'created' => $entity->getCreatedTime(),
'changed' => $entity->getChangedTime(),
'author' => $owner ? $owner->getEmail() : NULL,
'url' => $entity->toUrl()->toString(),
'url' => $entity->get('path')->alias ?? NULL,
];
}
break;
......
......@@ -32,6 +32,13 @@ class ContentImporter implements ContentImporterInterface {
*/
protected $moduleHandler;
/**
* The file system.
*
* @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
/**
* ContentExporter constructor.
*
......@@ -41,11 +48,14 @@ class ContentImporter implements ContentImporterInterface {
* The entity repository.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
* @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, ModuleHandlerInterface $module_handler) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository, ModuleHandlerInterface $module_handler, FileSystemInterface $file_system) {
$this->entityTypeManager = $entity_type_manager;
$this->entityRepository = $entity_repository;
$this->moduleHandler = $module_handler;
$this->fileSystem = $file_system;
}
/**
......@@ -70,7 +80,7 @@ class ContentImporter implements ContentImporterInterface {
'status' => $content['base_fields']['status'],
'path' => [
'alias' => $content['base_fields']['url'],
'pathauto' => 0,
'pathauto' => empty($content['base_fields']['url']),
],
]);
}
......@@ -181,13 +191,22 @@ class ContentImporter implements ContentImporterInterface {
break;
case 'paragraph':
$entity = $this->entityRepository->loadEntityByUuid('paragraph', $content['uuid']);
$paragraph_storage = $this->entityTypeManager->getStorage('paragraph');
$entity = $paragraph_storage->create([
'type' => $content['bundle'],
'langcode' => $content['base_fields']['langcode'],
'created' => $content['base_fields']['created'],
'status' => $content['base_fields']['status'],
]);
if (!$entity) {
$entity = $paragraph_storage->create([
'uuid' => $content['uuid'],
'type' => $content['bundle'],
'langcode' => $content['base_fields']['langcode'],
'created' => $content['base_fields']['created'],
'status' => $content['base_fields']['status'],
]);
}
else {
$entity->set('status', $content['base_fields']['status']);
$entity->set('langcode', $content['base_fields']['langcode']);
}
break;
}
......@@ -292,6 +311,9 @@ class ContentImporter implements ContentImporterInterface {
}
$content = file_get_contents($file_item['url']);
$directory = $this->fileSystem->dirname($file_item['uri']);
$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
if ($file = file_save_data($content, $file_item['uri'], FileSystemInterface::EXISTS_REPLACE)) {
$file->setOwnerId(1);
$file->setPermanent();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment