You are here

function lingotek_operation_content_download in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 8 lingotek.batch.inc \lingotek_operation_content_download()
  2. 8.2 lingotek.batch.inc \lingotek_operation_content_download()
  3. 4.0.x lingotek.batch.inc \lingotek_operation_content_download()
  4. 3.0.x lingotek.batch.inc \lingotek_operation_content_download()
  5. 3.1.x lingotek.batch.inc \lingotek_operation_content_download()
  6. 3.2.x lingotek.batch.inc \lingotek_operation_content_download()
  7. 3.3.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 119
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\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);
  $entityTypeBundleInfo = \Drupal::service('entity_type.bundle.info');
  $bundleInfos = $entityTypeBundleInfo
    ->getBundleInfo($entity
    ->getEntityTypeId());
  if (!$entity
    ->getEntityType()
    ->isTranslatable() || !$bundleInfos[$entity
    ->bundle()]['translatable']) {
    \Drupal::messenger()
      ->addWarning(t('Cannot download @type %label. That @bundle_label is not enabled for translation.', [
      '@type' => $bundleInfos[$entity
        ->bundle()]['label'],
      '%label' => $entity
        ->label(),
      '@bundle_label' => $entity
        ->getEntityType()
        ->getBundleLabel(),
    ]));
    return;
  }

  /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $lingotek_configuration */
  $lingotek_configuration = \Drupal::service('lingotek.configuration');
  if (!$lingotek_configuration
    ->isEnabled($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    \Drupal::messenger()
      ->addWarning(t('Cannot download @type %label. That @bundle_label is not enabled for Lingotek translation.', [
      '@type' => $bundleInfos[$entity
        ->bundle()]['label'],
      '%label' => $entity
        ->label(),
      '@bundle_label' => $entity
        ->getEntityType()
        ->getBundleLabel(),
    ]));
    return;
  }

  /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
  $translation_service = \Drupal::service('lingotek.content_translation');
  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(),
    ]);
  }
}