You are here

function lingotek_operation_content_download in Lingotek Translation 8.2

Same name and namespace in other branches
  1. 8 lingotek.batch.inc \lingotek_operation_content_download()
  2. 4.0.x lingotek.batch.inc \lingotek_operation_content_download()
  3. 3.0.x lingotek.batch.inc \lingotek_operation_content_download()
  4. 3.1.x lingotek.batch.inc \lingotek_operation_content_download()
  5. 3.2.x lingotek.batch.inc \lingotek_operation_content_download()
  6. 3.3.x lingotek.batch.inc \lingotek_operation_content_download()
  7. 3.4.x lingotek.batch.inc \lingotek_operation_content_download()
  8. 3.5.x lingotek.batch.inc \lingotek_operation_content_download()
  9. 3.6.x lingotek.batch.inc \lingotek_operation_content_download()
  10. 3.7.x lingotek.batch.inc \lingotek_operation_content_download()
  11. 3.8.x lingotek.batch.inc \lingotek_operation_content_download()

Wrapper function for running content downloads.

File

./lingotek.batch.inc, line 104
Lingotek batch functions

Code

function lingotek_operation_content_download($entity_type_id, $entity_id, $locale, &$context) {
  if ($context) {
    $context['message'] = t('Downloading translation "@locale" for @entity_type #@entity_id', [
      '@entity_type' => $entity_type_id,
      '@entity_id' => $entity_id,
      '@locale' => $locale,
    ]);
  }

  /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
  $translation_service = \Drupal::service('lingotek.content_translation');

  /** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
  $entity_storage = \Drupal::entityTypeManager()
    ->getStorage($entity_type_id);

  // Prepare the entity's translatable content for upload.
  $entity = $entity_storage
    ->load($entity_id);
  try {
    if ($translation_service
      ->downloadDocument($entity, $locale)) {
      $context['results']['downloads'] = !empty($context['results']['downloads']) ? $context['results']['downloads'] + 1 : 1;
    }
    else {
      $context['results']['error'][] = t('The download for @entity_type %title failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]);
    }
  } catch (LingotekApiException $exception) {
    $context['results']['error'][] = t('The download for @entity_type %title failed. Please try again.', [
      '@entity_type' => $entity
        ->getEntityTypeId(),
      '%title' => $entity
        ->label(),
    ]);
  }
}