Commit b7e7d545 authored by Arthur Callant's avatar Arthur Callant Committed by Oleksandr Kuzava
Browse files

Issue #3229474: Export content in a bulk

parent 4850140c
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
status: true
dependencies:
  module:
    - node
id: content_bulk_export
label: 'Export content'
type: 'node'
plugin: content_bulk_export
configuration: {  }
+3 −0
Original line number Diff line number Diff line
action.configuration.content_bulk_export:
  type: action_configuration_default
  label: 'Export content'
+28 −0
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Install/update module.
 */

use Drupal\Core\Config\FileStorage;

/**
 * Import action config.
 */
function single_content_sync_update_9121(&$sandbox) {
  // Get file storage of optional configs in the module.
  $config_path = drupal_get_path('module', 'single_content_sync') . '/config/install';
  $source = new FileStorage($config_path);

  /** @var \Drupal\Core\Config\StorageInterface $config_storage */
  $config_storage = \Drupal::service('config.storage');

  $config_names = [
    'system.action.content_bulk_export',
  ];

  foreach ($config_names as $config_name) {
    $config_storage->write($config_name, $source->read($config_name));
  }
}
+8 −0
Original line number Diff line number Diff line
@@ -41,3 +41,11 @@ single_content_sync.import:
    _title: 'Import content'
  requirements:
    _permission: 'import single content'

single_content_sync.bulk_export:
  path: '/admin/content/bulk-export'
  defaults:
    _form: '\Drupal\single_content_sync\Form\ContentBulkExportForm'
    _title: 'Bulk export content'
  requirements:
    _permission: 'export single content'
+50 −7
Original line number Diff line number Diff line
@@ -61,9 +61,6 @@ class ContentFileGenerator implements ContentFileGeneratorInterface {
   * {@inheritdoc}
   */
  public function generateYamlFile(FieldableEntityInterface $entity, bool $extract_translations = FALSE): FileInterface {
    // Clean up storage with assets before we start exporting new content.
    $this->privateTempStore->delete('export.assets');

    $output = $this->contentExporter->doExportToYml($entity, $extract_translations);
    $default_scheme = $this->contentSyncHelper->getDefaultFileScheme();
    $directory = "{$default_scheme}://export";
@@ -77,14 +74,14 @@ class ContentFileGenerator implements ContentFileGeneratorInterface {
   * {@inheritdoc}
   */
  public function generateZipFile(FieldableEntityInterface $entity, bool $extract_translations = FALSE): FileInterface {
    // Clean up storage with assets before we start exporting a content.
    $this->privateTempStore->delete('export.assets');

    $export_file = $this->generateYamlFile($entity, $extract_translations);
    $default_scheme = $this->contentSyncHelper->getDefaultFileScheme();

    // Generate an empty zip file to be used for storing the exported content.
    $directory = "{$default_scheme}://export/zip";
    $this->contentSyncHelper->prepareFilesDirectory($directory);
    $zip_name = $this->contentSyncHelper->generateContentFileName($entity);
    $zip_file = $this->contentSyncHelper->saveFileContentTemporary('', "{$directory}/{$zip_name}.zip");
    $zip_file = $this->generateEmptyZipFile($zip_name);

    $zip_file_path = $this->fileSystem->realpath($zip_file->getFileUri());
    $zip = $this->contentSyncHelper->createZipInstance($zip_file_path);
@@ -99,6 +96,52 @@ class ContentFileGenerator implements ContentFileGeneratorInterface {
    return $zip_file;
  }

  /**
   * {@inheritdoc}
   */
  public function generateBulkZipFile(array $entities, bool $extract_translations = FALSE, bool $extract_assets = FALSE): FileInterface {
    // Generate an empty zip file to be used for storing the exported content.
    $zip_name = sprintf('content-bulk-export-%s', date('d_m_Y-H_i'));
    $zip_file = $this->generateEmptyZipFile($zip_name);

    $zip_file_path = $this->fileSystem->realpath($zip_file->getFileUri());
    $zip = $this->contentSyncHelper->createZipInstance($zip_file_path);

    // Clean up storage with assets before we start exporting a content.
    $this->privateTempStore->delete('export.assets');

    // Fill the zip with content and assets.
    foreach ($entities as $entity) {
      // Generate the yml files and add to the zip.
      $export_file = $this->generateYamlFile($entity, $extract_translations);
      $content_file_path = $this->fileSystem->realpath($export_file->getFileUri());
      $zip->getArchive()->addFile($content_file_path, $export_file->getFilename());
    }

    if ($extract_assets) {
      $this->addAssetsToZip($zip);
    }

    return $zip_file;
  }

  /**
   * Generate an empty zip folder.
   *
   * @param string $name
   *   String containing the name of the zip file to generate.
   *
   * @return \Drupal\file\FileInterface
   *   The generated empty zip file.
   */
  protected function generateEmptyZipFile(string $name): FileInterface {
    $default_scheme = $this->contentSyncHelper->getDefaultFileScheme();
    $directory = "{$default_scheme}://export/zip";
    $this->contentSyncHelper->prepareFilesDirectory($directory);

    return $this->contentSyncHelper->saveFileContentTemporary('', "{$directory}/{$name}.zip");
  }

  /**
   * Add assets to zip file.
   *
Loading