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

Issue #3282218: Importing content with translations not in standard language...

Issue #3282218: Importing content with translations not in standard language environment throws error if translation already exists
parent 8b6d17f0
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ services:
      - '@module_handler'
      - '@messenger'
      - '@single_content_sync.store'
      - '@language_manager'

  single_content_sync.importer:
    class: Drupal\single_content_sync\ContentImporter
@@ -32,6 +33,7 @@ services:
      - '@plugin.manager.archiver'
      - '@entity_type.manager'
      - '@config.factory'
      - '@entity.repository'

  single_content_sync.store:
    class: \Drupal\Core\TempStore\PrivateTempStore
+13 −4
Original line number Diff line number Diff line
@@ -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\LanguageManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\StringTranslation\StringTranslationTrait;
@@ -72,6 +73,13 @@ class ContentExporter implements ContentExporterInterface {
   */
  private $entityOutputCache = [];

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * ContentExporter constructor.
   *
@@ -84,11 +92,12 @@ class ContentExporter implements ContentExporterInterface {
   * @param \Drupal\Core\TempStore\PrivateTempStore $store
   *   The private temp store of the module.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, MessengerInterface $messenger, PrivateTempStore $store) {
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, MessengerInterface $messenger, PrivateTempStore $store, LanguageManagerInterface $language_manager) {
    $this->entityTypeManager = $entity_type_manager;
    $this->moduleHandler = $module_handler;
    $this->messenger = $messenger;
    $this->privateTempStore = $store;
    $this->languageManager = $language_manager;
  }

  /**
@@ -192,8 +201,8 @@ class ContentExporter implements ContentExporterInterface {
    if ($this->extractTranslationsMode && $entity->isTranslatable()) {
      $translations = $entity->getTranslationLanguages();

      // Exclude the active language from the translations.
      unset($translations[$entity->language()->getId()]);
      // Exclude the default language from the translations.
      unset($translations[$this->languageManager->getDefaultLanguage()->getId()]);

      if (count($translations)) {
        foreach ($translations as $language) {
@@ -451,7 +460,7 @@ class ContentExporter implements ContentExporterInterface {
      case 'layout_section':
        $block_storage = $this->entityTypeManager->getStorage('block_content');
        $block_list = [];
        $section = [];
        $sections = [];

        foreach ($field->getValue() as $section_array) {
          /** @var \Drupal\layout_builder\Section $section */
+23 −1
Original line number Diff line number Diff line
@@ -7,11 +7,13 @@ use Drupal\Core\Archiver\ArchiverInterface;
use Drupal\Core\Archiver\ArchiverManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Serialization\Yaml;
use Drupal\file\FileInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\ParameterBag;

class ContentSyncHelper implements ContentSyncHelperInterface {

@@ -52,6 +54,13 @@ class ContentSyncHelper implements ContentSyncHelperInterface {
   */
  protected $configFactory;

  /**
   * The entity repository.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * ContentSyncHelper constructor.
   *
@@ -66,12 +75,13 @@ class ContentSyncHelper implements ContentSyncHelperInterface {
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   */
  public function __construct(UuidInterface $uuid, FileSystemInterface $file_system, ArchiverManager $archiver_manager, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
  public function __construct(UuidInterface $uuid, FileSystemInterface $file_system, ArchiverManager $archiver_manager, EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory, EntityRepositoryInterface $entity_repository) {
    $this->uuid = $uuid;
    $this->fileSystem = $file_system;
    $this->archiverManager = $archiver_manager;
    $this->entityTypeManager = $entity_type_manager;
    $this->configFactory = $config_factory;
    $this->entityRepository = $entity_repository;
  }

  /**
@@ -165,4 +175,16 @@ class ContentSyncHelper implements ContentSyncHelperInterface {
    return $this->fileSystem->realpath($file->getFileUri());
  }

  /**
   * {@inheritDoc}
   */
  public function getDefaultLanguageEntity(ParameterBag $parameters): EntityInterface {
    $entity_uuid = $parameters->getIterator()->current()->uuid();
    $entity_type_id = $parameters->getIterator()->current()->getEntityTypeId();
    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $this->entityRepository->loadEntityByUuid($entity_type_id, $entity_uuid);

    return $entity;
  }

}
+12 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ namespace Drupal\single_content_sync;
use Drupal\Core\Archiver\ArchiverInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\file\FileInterface;
use Symfony\Component\HttpFoundation\ParameterBag;

interface ContentSyncHelperInterface {

@@ -89,4 +90,15 @@ interface ContentSyncHelperInterface {
   */
  public function getFileRealPathById(int $fid): string;

  /**
   * Get an entity object from the default language configuration.
   *
   * @param \Symfony\Component\HttpFoundation\ParameterBag $parameters
   *   The parameters from which to get the entity object.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The entity interface from the default language configuration.
   */
  public function getDefaultLanguageEntity(ParameterBag $parameters): EntityInterface;

}
+15 −4
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@ use Drupal\Core\Url;
use Drupal\file\FileInterface;
use Drupal\single_content_sync\ContentExporterInterface;
use Drupal\single_content_sync\ContentFileGeneratorInterface;
use Drupal\single_content_sync\ContentSyncHelperInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
@@ -40,6 +41,13 @@ class ContentExportForm extends FormBase {
   */
  protected $fileGenerator;

  /**
   * The content sync helper.
   *
   * @var \Drupal\single_content_sync\ContentSyncHelperInterface
   */
  protected $contentSyncHelper;

  /**
   * ContentExportForm constructor.
   *
@@ -50,10 +58,11 @@ class ContentExportForm extends FormBase {
   * @param \Drupal\single_content_sync\ContentFileGeneratorInterface $file_generator
   *   The content file generator.
   */
  public function __construct(ContentExporterInterface $content_exporter, EntityTypeManagerInterface $entity_type_manager, ContentFileGeneratorInterface $file_generator) {
  public function __construct(ContentExporterInterface $content_exporter, EntityTypeManagerInterface $entity_type_manager, ContentFileGeneratorInterface $file_generator, ContentSyncHelperInterface $content_sync_helper) {
    $this->contentExporter = $content_exporter;
    $this->entityTypeManager = $entity_type_manager;
    $this->fileGenerator = $file_generator;
    $this->contentSyncHelper = $content_sync_helper;
  }

  /**
@@ -63,7 +72,8 @@ class ContentExportForm extends FormBase {
    return new static(
      $container->get('single_content_sync.exporter'),
      $container->get('entity_type.manager'),
      $container->get('single_content_sync.file_generator')
      $container->get('single_content_sync.file_generator'),
      $container->get('single_content_sync.helper')
    );
  }

@@ -121,7 +131,8 @@ class ContentExportForm extends FormBase {

    $extract_translations = $form_state->getValue('translation', FALSE);
    $parameters = $this->getRouteMatch()->getParameters();
    $entity = $parameters->getIterator()->current();
    $entity = $this->contentSyncHelper->getDefaultLanguageEntity($parameters);

    $export_in_yaml = $this->contentExporter->doExportToYml($entity, $extract_translations);

    $form['output'] = [
@@ -192,7 +203,7 @@ class ContentExportForm extends FormBase {
    $button = $form_state->getTriggeringElement();
    $extract_translations = $form_state->getValue('translation', FALSE);
    $parameters = $this->getRouteMatch()->getParameters();
    $entity = $parameters->getIterator()->current();
    $entity = $this->contentSyncHelper->getDefaultLanguageEntity($parameters);

    switch ($button['#name']) {
      case 'download_file':