You are here

public function LingotekEntityController::download in Lingotek Translation 3.3.x

Same name and namespace in other branches
  1. 8 src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  2. 8.2 src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  3. 4.0.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  4. 3.0.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  5. 3.1.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  6. 3.2.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  7. 3.4.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  8. 3.5.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  9. 3.6.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  10. 3.7.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
  11. 3.8.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::download()
1 string reference to 'LingotekEntityController::download'
lingotek.routing.yml in ./lingotek.routing.yml
lingotek.routing.yml

File

src/Controller/LingotekEntityController.php, line 296

Class

LingotekEntityController

Namespace

Drupal\lingotek\Controller

Code

public function download($doc_id, $locale) {

  /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
  $translation_service = \Drupal::service('lingotek.content_translation');
  $entity = $translation_service
    ->loadByDocumentId($doc_id);
  if (!$entity) {

    // TODO: log warning
    return $this
      ->translationsPageRedirect($entity);
  }
  $bundleInfos = $this->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 $this
      ->translationsPageRedirect($entity);
  }
  if (!$this->lingotekConfiguration
    ->isEnabled($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    $this
      ->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 $this
      ->translationsPageRedirect($entity);
  }
  try {
    if ($translation_service
      ->downloadDocument($entity, $locale)) {
      $this
        ->messenger()
        ->addStatus(t('The translation of @entity_type %title into @locale has been downloaded.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
        '@locale' => $locale,
      ]));
    }
    else {
      \Drupal::logger('lingotek')
        ->warning($this
        ->t('Error happened trying to download (%entity_id,%revision_id).', [
        '%entity_id' => $entity
          ->id(),
        '%revision_id' => $entity
          ->getRevisionId(),
      ]));
      $this
        ->messenger()
        ->addError(t('The translation of @entity_type %title into @locale failed to download.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
        '@locale' => $locale,
      ]));
    }
  } catch (LingotekApiException $exception) {
    $this
      ->messenger()
      ->addError(t('The download for @entity_type %title failed. Please try again.', [
      '@entity_type' => $entity
        ->getEntityTypeId(),
      '%title' => $entity
        ->label(),
    ]));
  } catch (LingotekContentEntityStorageException $storage_exception) {
    \Drupal::logger('lingotek')
      ->error('The download for @entity_type %title failed because of the length of one field translation (%locale) value: %table.', [
      '@entity_type' => $entity
        ->getEntityTypeId(),
      '%title' => $entity
        ->label(),
      '%locale' => $locale,
      '%table' => $storage_exception
        ->getTable(),
    ]);
    $this
      ->messenger()
      ->addError(t('The download for @entity_type %title failed because of the length of one field translation (%locale) value: %table.', [
      '@entity_type' => $entity
        ->getEntityTypeId(),
      '%title' => $entity
        ->label(),
      '%locale' => $locale,
      '%table' => $storage_exception
        ->getTable(),
    ]));
  }
  return $this
    ->translationsPageRedirect($entity);
}