You are here

function lingotek_operation_content_upload in Lingotek Translation 3.8.x

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

Wrapper function for running content uploads.

1 string reference to 'lingotek_operation_content_upload'
LingotekBatchController::getUploadOperations in src/Controller/LingotekBatchController.php

File

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

Code

function lingotek_operation_content_upload($entity_type_id, $entity_id, &$context) {
  if ($context) {
    $context['message'] = t('Uploading @entity_type @entity_id', [
      '@entity_type' => $entity_type_id,
      '@entity_id' => $entity_id,
    ]);
  }

  /** @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 upload @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 upload @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');

  // Use upload with new entities.
  if (!$translation_service
    ->getDocumentId($entity)) {
    $document_id = NULL;
    try {
      $document_id = $translation_service
        ->uploadDocument($entity);
    } catch (LingotekDocumentNotFoundException $exception) {
      $context['results']['error'][] = t('Document @entity_type %title was not found. Please upload again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]);
    } catch (LingotekPaymentRequiredException $exception) {
      $context['results']['error'][] = t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.');
    } catch (LingotekApiException $exception) {
      $context['results']['error'][] = t('The upload for @entity_type %title failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]);
    }
    if ($document_id) {
      $context['results']['uploads'] = !empty($context['results']['uploads']) ? $context['results']['uploads'] + 1 : 1;
    }
    else {

      // Mark the document as failed.
      $translation_service
        ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
      $context['results']['error'][] = t('The upload for @entity_type %title failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]);
    }
  }
  else {
    try {
      if ($translation_service
        ->updateDocument($entity)) {
        $context['results']['uploads'] = !empty($context['results']['uploads']) ? $context['results']['uploads'] + 1 : 1;
      }
      else {

        // ToDo: Log a problem happened updating the document.
      }
    } catch (LingotekDocumentNotFoundException $exception) {
      $context['results']['error'][] = t('Document @entity_type %title was not found. Please upload again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]);
    } catch (LingotekDocumentLockedException $exception) {
      $context['results']['error'][] = t('Document @entity_type %title has a new version. The document id has been updated for all future interactions. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]);
    } catch (LingotekDocumentArchivedException $exception) {
      $context['results']['error'][] = t('Document @entity_type %title has been archived. Please upload again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]);
    } catch (LingotekPaymentRequiredException $exception) {
      $context['results']['error'][] = t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.');
    } catch (LingotekApiException $exception) {
      $context['results']['error'][] = t('The update for @entity_type %title failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]);
    }
  }
}