You are here

public function LingotekContentTranslationService::downloadDocuments in Lingotek Translation 3.8.x

Same name and namespace in other branches
  1. 8 src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  2. 8.2 src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  3. 4.0.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  4. 3.0.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  5. 3.1.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  6. 3.2.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  7. 3.3.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  8. 3.4.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  9. 3.5.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  10. 3.6.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  11. 3.7.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()

Downloads a document from the Lingotek service for all available locales.

Parameters

\Drupal\Core\Entity\ContentEntityInterface &$entity: The entity being downloaded.

Return value

bool TRUE if the document was downloaded successfully, FALSE if not.

Throws

\Drupal\lingotek\Exception\LingotekDocumentNotFoundException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekContentTranslationServiceInterface::downloadDocuments

File

src/LingotekContentTranslationService.php, line 1493

Class

LingotekContentTranslationService
Service for managing Lingotek content translations.

Namespace

Drupal\lingotek

Code

public function downloadDocuments(ContentEntityInterface &$entity) {
  $profile = $this->lingotekConfiguration
    ->getEntityProfile($entity);
  if ($profile
    ->id() === Lingotek::PROFILE_DISABLED || $this
    ->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED) {
    return FALSE;
  }
  if ($document_id = $this
    ->getDocumentId($entity)) {
    $source_status = $this
      ->getSourceStatus($entity);
    $target_languages = $this->languageManager
      ->getLanguages();
    $target_languages = array_filter($target_languages, function (LanguageInterface $language) {
      $configLanguage = ConfigurableLanguage::load($language
        ->getId());
      return $this->lingotekConfiguration
        ->isLanguageEnabled($configLanguage);
    });
    $entity_langcode = $entity
      ->getUntranslated()
      ->language()
      ->getId();
    foreach ($target_languages as $langcode => $language) {
      $locale = $this->languageLocaleMapper
        ->getLocaleForLangcode($langcode);
      if ($langcode !== $entity_langcode) {
        try {
          if ($this->lingotek
            ->getDocumentTranslationStatus($document_id, $locale) !== FALSE) {
            $data = $this->lingotek
              ->downloadDocument($document_id, $locale);
            if ($data) {

              // Check the real status, because it may still need review or anything.
              $status = $this->lingotek
                ->getDocumentTranslationStatus($document_id, $locale);
              $transaction = $this->connection
                ->startTransaction();
              try {
                $saved = $this
                  ->saveTargetData($entity, $langcode, $data);
                if ($saved) {

                  // If the status was "Importing", and the target was added
                  // successfully, we can ensure that the content is current now.
                  if ($source_status == Lingotek::STATUS_IMPORTING) {
                    $this
                      ->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
                  }
                  if ($source_status == Lingotek::STATUS_EDITED) {
                    $this
                      ->setTargetStatus($entity, $langcode, Lingotek::STATUS_EDITED);
                  }
                  elseif ($status === TRUE) {
                    $this
                      ->setTargetStatus($entity, $langcode, Lingotek::STATUS_CURRENT);
                  }
                  else {
                    $this
                      ->setTargetStatus($entity, $langcode, Lingotek::STATUS_INTERMEDIATE);
                  }
                }
              } catch (LingotekDocumentNotFoundException $exception) {
                $this
                  ->setDocumentId($entity, NULL);
                $this
                  ->deleteMetadata($entity);
                throw $exception;
              } catch (LingotekApiException $exception) {

                // TODO: log issue
                $this
                  ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
                throw $exception;
              } catch (LingotekContentEntityStorageException $storageException) {
                $this
                  ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
                throw $storageException;
              } catch (\Exception $exception) {
                $transaction
                  ->rollBack();
                $this
                  ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
              }
            }
            else {
              return NULL;
            }
          }
        } catch (LingotekDocumentNotFoundException $exception) {
          $this
            ->setDocumentId($entity, NULL);
          $this
            ->deleteMetadata($entity);
          throw $exception;
        } catch (LingotekApiException $exception) {

          // TODO: log issue
          $this
            ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
          throw $exception;
        }
      }
    }
  }
  if ($this
    ->getSourceStatus($entity) == Lingotek::STATUS_DISABLED) {
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
  }
  return FALSE;
}