You are here

public function LingotekContentTranslationService::downloadDocument in Lingotek Translation 3.5.x

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

Downloads a document from the Lingotek service for a given locale.

Parameters

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

string $locale: Lingotek translation language which we want to download.

Return value

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

Overrides LingotekContentTranslationServiceInterface::downloadDocument

File

src/LingotekContentTranslationService.php, line 1222

Class

LingotekContentTranslationService
Service for managing Lingotek content translations.

Namespace

Drupal\lingotek

Code

public function downloadDocument(ContentEntityInterface &$entity, $locale) {
  $profile = $this->lingotekConfiguration
    ->getEntityProfile($entity);
  if ($profile
    ->id() === Lingotek::PROFILE_DISABLED || $this
    ->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED) {
    \Drupal::logger('lingotek')
      ->warning('Avoided download for (%entity_id,%revision_id): Source status is %source_status.', [
      '%entity_id' => $entity
        ->id(),
      '%revision_id' => $entity
        ->getRevisionId(),
      '%source_status' => $this
        ->getSourceStatus($entity),
    ]);
    return FALSE;
  }
  if ($document_id = $this
    ->getDocumentId($entity)) {
    $source_status = $this
      ->getSourceStatus($entity);
    $drupal_language = $this->languageLocaleMapper
      ->getConfigurableLanguageForLocale($locale);
    $langcode = $drupal_language
      ->id();
    $data = [];
    try {
      if ($this->lingotek
        ->getDocumentTranslationStatus($document_id, $locale) !== FALSE) {
        $data = $this->lingotek
          ->downloadDocument($document_id, $locale);
      }
      else {
        \Drupal::logger('lingotek')
          ->warning('Avoided download for (%entity_id,%revision_id): Source status is %source_status.', [
          '%entity_id' => $entity
            ->id(),
          '%revision_id' => $entity
            ->getRevisionId(),
          '%source_status' => $this
            ->getSourceStatus($entity),
        ]);
        return NULL;
      }
    } catch (LingotekApiException $exception) {
      \Drupal::logger('lingotek')
        ->error('Error happened downloading %document_id %locale: %message', [
        '%document_id' => $document_id,
        '%locale' => $locale,
        '%message' => $exception
          ->getMessage(),
      ]);
      $this
        ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
      throw $exception;
    }
    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 (LingotekContentEntityStorageException $storageException) {
        $this
          ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
        \Drupal::logger('lingotek')
          ->error('Error happened (storage) saving %document_id %locale: %message', [
          '%document_id' => $document_id,
          '%locale' => $locale,
          '%message' => $storageException
            ->getMessage(),
        ]);
        throw $storageException;
      } catch (\Exception $exception) {
        \Drupal::logger('lingotek')
          ->error('Error happened (unknown) saving %document_id %locale: %message', [
          '%document_id' => $document_id,
          '%locale' => $locale,
          '%message' => $exception
            ->getMessage(),
        ]);
        $this
          ->setTargetStatus($entity, $langcode, Lingotek::STATUS_ERROR);
        $transaction
          ->rollBack();
        return FALSE;
      }
      return TRUE;
    }
  }
  if ($this
    ->getSourceStatus($entity) == Lingotek::STATUS_DISABLED) {
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
  }
  \Drupal::logger('lingotek')
    ->warning('Error happened trying to download (%entity_id,%revision_id): no document id found.', [
    '%entity_id' => $entity
      ->id(),
    '%revision_id' => $entity
      ->getRevisionId(),
  ]);
  return FALSE;
}