Exporter.php in Tome 8
File
modules/tome_sync/src/Exporter.php
View source
<?php
namespace Drupal\tome_sync;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountSwitcherInterface;
use Drupal\file\FileInterface;
use Drupal\tome_base\PathTrait;
use Drupal\tome_sync\Event\ContentCrudEvent;
use Drupal\tome_sync\Event\TomeSyncEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Serializer\Serializer;
use Drupal\Core\File\FileSystemInterface;
class Exporter implements ExporterInterface {
use PathTrait;
use ContentIndexerTrait;
use AccountSwitcherTrait;
protected $contentStorage;
protected $serializer;
protected $entityTypeManager;
protected $eventDispatcher;
protected $fileSystem;
protected $fileSync;
protected static $excludedTypes = [
'content_moderation_state',
];
public function __construct(StorageInterface $content_storage, Serializer $serializer, EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, AccountSwitcherInterface $account_switcher, FileSyncInterface $file_sync, FileSystemInterface $file_system) {
$this->contentStorage = $content_storage;
$this->serializer = $serializer;
$this->entityTypeManager = $entity_type_manager;
$this->eventDispatcher = $event_dispatcher;
$this->accountSwitcher = $account_switcher;
$this->fileSync = $file_sync;
$this->fileSystem = $file_system;
}
public function getContentToExport() {
$entities = [];
$definitions = array_diff_key($this->entityTypeManager
->getDefinitions(), array_flip(static::$excludedTypes));
foreach ($definitions as $entity_type) {
if (is_a($entity_type
->getClass(), '\\Drupal\\Core\\Entity\\ContentEntityInterface', TRUE)) {
$storage = $this->entityTypeManager
->getStorage($entity_type
->id());
$entities[$entity_type
->id()] = $storage
->getQuery()
->execute();
}
}
return $entities;
}
public function deleteExportDirectories() {
$this->contentStorage
->deleteAll();
$this
->deleteContentIndex();
if (!$this->fileSync
->deleteExportDirectory()) {
return FALSE;
}
return TRUE;
}
public function exportContent(ContentEntityInterface $entity) {
if (in_array($entity
->getEntityTypeId(), static::$excludedTypes, TRUE)) {
return;
}
$this
->switchToAdmin();
$data = $this->serializer
->normalize($entity, 'json');
$this->contentStorage
->write(TomeSyncHelper::getContentName($entity), $data);
$this
->indexContent($entity);
if ($entity instanceof FileInterface) {
$this->fileSync
->exportFile($entity);
}
$event = new ContentCrudEvent($entity);
$this->eventDispatcher
->dispatch(TomeSyncEvents::EXPORT_CONTENT, $event);
$this
->switchBack();
}
public function deleteContentExport(ContentEntityInterface $entity) {
foreach (array_keys($entity
->getTranslationLanguages()) as $langcode) {
$this->contentStorage
->delete(TomeSyncHelper::getContentName($entity
->getTranslation($langcode)));
$this
->unIndexContent($entity);
}
if ($entity instanceof FileInterface) {
$this->fileSync
->deleteFileExport($entity);
}
$event = new ContentCrudEvent($entity);
$this->eventDispatcher
->dispatch(TomeSyncEvents::DELETE_CONTENT, $event);
}
}
Classes
Name |
Description |
Exporter |
Handles exporting of content and file entities. |