You are here

public function LingotekContentTranslationService::downloadDocuments in Lingotek Translation 3.3.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.4.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  8. 3.5.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  9. 3.6.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  10. 3.7.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::downloadDocuments()
  11. 3.8.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.

Overrides LingotekContentTranslationServiceInterface::downloadDocuments

File

src/LingotekContentTranslationService.php, line 1140

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 (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) {
                $this
                  ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
                $transaction
                  ->rollBack();
              }
            }
            else {
              return NULL;
            }
          }
        } 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;
}