You are here

public function LingotekConfigTranslationService::uploadDocument in Lingotek Translation 3.7.x

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

Uploads a document to the Lingotek service.

Parameters

\Drupal\Core\Config\Entity\ConfigEntityInterface $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 LingotekConfigTranslationServiceInterface::uploadDocument

See also

::updateDocument :

File

src/LingotekConfigTranslationService.php, line 360

Class

LingotekConfigTranslationService
Service for managing Lingotek configuration translations.

Namespace

Drupal\lingotek

Code

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

  // We can reupload if the document is cancelled.
  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 (!empty($this
    ->getDocumentId($entity))) {
    return $this
      ->updateDocument($entity, $job_id);
  }
  $source_data = $this
    ->getSourceData($entity);
  $extended_name = $entity
    ->id() . ' (config): ' . $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('edit-form') ? $entity
    ->toUrl()
    ->setAbsolute()
    ->toString() : NULL;

  // Allow other modules to alter the data before is uploaded.
  \Drupal::moduleHandler()
    ->invokeAll('lingotek_config_entity_document_upload', [
    &$source_data,
    &$entity,
    &$url,
  ]);
  $encoded_data = json_encode($source_data);
  $process_id = NULL;
  try {
    $document_id = $this->lingotek
      ->uploadDocument($document_name, $encoded_data, $this
      ->getSourceLocale($entity), $url, $this->lingotekConfiguration
      ->getConfigEntityProfile($entity), $job_id, $process_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
      ->setDocumentId($entity, $document_id);
    $this->lingotekConfiguration
      ->setConfigEntityProfile($entity, $profile
      ->id());
    $this
      ->setSourceStatus($entity, Lingotek::STATUS_IMPORTING);
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_REQUEST);
    $this
      ->setJobId($entity, $job_id);
    $this
      ->setLastUploaded($entity, \Drupal::time()
      ->getRequestTime());
    if ($process_id !== NULL) {
      $this
        ->storeUploadProcessId($entity, $process_id);
    }
    return $document_id;
  }
  if ($this
    ->getSourceStatus($entity) == Lingotek::STATUS_DISABLED) {
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
  }
  return FALSE;
}