You are here

public function LingotekContentTranslationService::uploadDocument in Lingotek Translation 3.2.x

Same name and namespace in other branches
  1. 8 src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  2. 8.2 src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  3. 4.0.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  4. 3.0.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  5. 3.1.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  6. 3.3.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  7. 3.4.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  8. 3.5.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  9. 3.6.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  10. 3.7.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()
  11. 3.8.x src/LingotekContentTranslationService.php \Drupal\lingotek\LingotekContentTranslationService::uploadDocument()

Uploads a document to the Lingotek service.

Parameters

\Drupal\Core\Entity\ContentEntityInterface $entity: The entity 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\LingotekApiException

Propagated from @throws \Drupal\lingotek\Exception\LingotekDocumentArchivedException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

Overrides LingotekContentTranslationServiceInterface::uploadDocument

See also

::updateDocument :

File

src/LingotekContentTranslationService.php, line 849

Class

LingotekContentTranslationService
Service for managing Lingotek content translations.

Namespace

Drupal\lingotek

Code

public function uploadDocument(ContentEntityInterface $entity, $job_id = NULL) {
  $profile = $this->lingotekConfiguration
    ->getEntityProfile($entity);

  // We can reupload if the document is cancelled.
  if (!$this->lingotekConfiguration
    ->isEnabled($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    return FALSE;
  }
  if ($profile
    ->id() === Lingotek::PROFILE_DISABLED) {
    return FALSE;
  }

  // If job id was not set in the form, it may be already assigned.
  if ($job_id === NULL) {
    $job_id = $this
      ->getJobId($entity) ?: NULL;
  }
  if ($document_id = $this
    ->getDocumentId($entity)) {
    return $this
      ->updateDocument($entity, $job_id);
  }
  $source_data = $this
    ->getSourceData($entity);
  $extended_name = $entity
    ->bundle() . ' (' . $entity
    ->getEntityTypeId() . '): ' . $entity
    ->label();
  $profile_preference = $profile
    ->getAppendContentTypeToTitle();
  $global_preference = $this->lingotekConfiguration
    ->getPreference('append_type_to_title');
  switch ($profile_preference) {
    case 'yes':
      $document_name = $extended_name;
      break;
    case 'no':
      $document_name = $entity
        ->label();
      break;
    case 'global_setting':
      $document_name = $global_preference ? $extended_name : $entity
        ->label();
      break;
    default:
      $document_name = $extended_name;
  }
  $url = $entity
    ->hasLinkTemplate('canonical') ? $entity
    ->toUrl()
    ->setAbsolute(TRUE)
    ->toString() : NULL;

  // Allow other modules to alter the data before is uploaded.
  \Drupal::moduleHandler()
    ->invokeAll('lingotek_content_entity_document_upload', [
    &$source_data,
    &$entity,
    &$url,
  ]);
  try {
    $document_id = $this->lingotek
      ->uploadDocument($document_name, $source_data, $this
      ->getSourceLocale($entity), $url, $profile, $job_id);
  } catch (LingotekPaymentRequiredException $exception) {
    $this
      ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
    throw $exception;
  } catch (LingotekApiException $exception) {
    $this
      ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
    throw $exception;
  }
  if ($document_id) {
    $this->lingotekConfiguration
      ->setProfile($entity, $profile
      ->id());
    $this
      ->setDocumentId($entity, $document_id);
    $this
      ->setSourceStatus($entity, Lingotek::STATUS_IMPORTING);
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_REQUEST);
    $this
      ->setJobId($entity, $job_id);
    return $document_id;
  }
  if ($this
    ->getSourceStatus($entity) == Lingotek::STATUS_DISABLED) {
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
  }
  return FALSE;
}