You are here

public function LingotekInterfaceTranslationService::uploadDocument in Lingotek Translation 4.0.x

Same name and namespace in other branches
  1. 3.2.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::uploadDocument()
  2. 3.3.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::uploadDocument()
  3. 3.4.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::uploadDocument()
  4. 3.5.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::uploadDocument()
  5. 3.6.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::uploadDocument()
  6. 3.7.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::uploadDocument()
  7. 3.8.x src/LingotekInterfaceTranslationService.php \Drupal\lingotek\LingotekInterfaceTranslationService::uploadDocument()

Uploads a document to the Lingotek service.

Parameters

string $component: The component being uploaded.

string $job_id: (optional) The job ID that will be associated.

Return value

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

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekDocumentArchivedException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekInterfaceTranslationServiceInterface::uploadDocument

See also

::updateDocument

File

src/LingotekInterfaceTranslationService.php, line 601

Class

LingotekInterfaceTranslationService
Service for managing Lingotek interface translations.

Namespace

Drupal\lingotek

Code

public function uploadDocument($component, $job_id = NULL) {

  // If job id was not set in the form, it may be already assigned.
  if ($job_id === NULL) {
    $job_id = $this
      ->getJobId($component) ?: NULL;
  }
  if ($document_id = $this
    ->getDocumentId($component)) {
    return $this
      ->updateDocument($component, $job_id);
  }
  $source_data = $this
    ->getSourceData($component);
  $document_name = 'Interface translation: ' . $component;

  // Allow other modules to alter the data before is uploaded.
  \Drupal::moduleHandler()
    ->invokeAll('lingotek_interface_translation_document_upload', [
    &$source_data,
    &$component,
  ]);
  $process_id = NULL;
  try {
    $document_id = $this->lingotek
      ->uploadDocument($document_name, $source_data, $this
      ->getSourceLocale($component), NULL, NULL, $job_id, $process_id);
  } catch (LingotekPaymentRequiredException $exception) {
    $this
      ->setSourceStatus($component, Lingotek::STATUS_ERROR);
    throw $exception;
  } catch (LingotekApiException $exception) {
    $this
      ->setSourceStatus($component, Lingotek::STATUS_ERROR);
    throw $exception;
  }
  if ($document_id) {
    $this
      ->setDocumentId($component, $document_id);
    $this
      ->setSourceStatus($component, Lingotek::STATUS_IMPORTING);
    $this
      ->setTargetStatuses($component, Lingotek::STATUS_REQUEST);
    $this
      ->setJobId($component, $job_id);
    $this
      ->setLastUploaded($component, \Drupal::time()
      ->getRequestTime());
    if ($process_id !== NULL) {
      $this
        ->storeUploadProcessId($component, $process_id);
    }
    return $document_id;
  }
  return FALSE;
}