You are here

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

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

Uploads a document to the Lingotek service.

Parameters

string $mapper_id: 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\LingotekDocumentNotFoundException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

Overrides LingotekConfigTranslationServiceInterface::uploadConfig

See also

::updateConfig :

File

src/LingotekConfigTranslationService.php, line 1145

Class

LingotekConfigTranslationService
Service for managing Lingotek configuration translations.

Namespace

Drupal\lingotek

Code

public function uploadConfig($mapper_id, $job_id = NULL) {
  $mapper = $this->mappers[$mapper_id];
  $profile = $this->lingotekConfiguration
    ->getConfigProfile($mapper_id, FALSE);

  // We can reupload if the document is cancelled.
  if ($profile !== NULL && $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
      ->getConfigJobId($mapper) ?: NULL;
  }
  if (!empty($this
    ->getConfigDocumentId($mapper))) {
    return $this
      ->updateConfig($mapper_id);
  }

  // Get the provide providing a default.
  $profile = $this->lingotekConfiguration
    ->getConfigProfile($mapper_id);
  $source_data = $this
    ->getConfigSourceData($mapper);

  // Allow other modules to alter the data before is uploaded.
  $config_names = $mapper
    ->getConfigNames();
  $config_name = reset($config_names);
  \Drupal::moduleHandler()
    ->invokeAll('lingotek_config_object_document_upload', [
    &$source_data,
    $config_name,
  ]);
  $source_data = json_encode($source_data);
  $extended_name = $mapper_id . ' (config): ' . $mapper
    ->getTitle();
  $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 = (string) $mapper
        ->getTitle();
      break;
    case 'global_setting':
      $document_name = $global_preference ? $extended_name : (string) $mapper
        ->getTitle();
      break;
    default:
      $document_name = $extended_name;
  }
  $process_id = NULL;
  try {
    $document_id = $this->lingotek
      ->uploadDocument($document_name, $source_data, $this
      ->getConfigSourceLocale($mapper), NULL, $this->lingotekConfiguration
      ->getConfigProfile($mapper_id), $job_id, $process_id);
  } catch (LingotekPaymentRequiredException $exception) {
    $this
      ->setConfigSourceStatus($mapper, Lingotek::STATUS_ERROR);
    throw $exception;
  } catch (LingotekApiException $exception) {
    $this
      ->setConfigSourceStatus($mapper, Lingotek::STATUS_ERROR);
    throw $exception;
  }
  if ($document_id) {
    $this
      ->setConfigDocumentId($mapper, $document_id);
    $this
      ->setConfigSourceStatus($mapper, Lingotek::STATUS_IMPORTING);
    $this
      ->setConfigTargetStatuses($mapper, Lingotek::STATUS_REQUEST);
    $this
      ->setConfigJobId($mapper, $job_id);
    $this
      ->setConfigLastUploaded($mapper, \Drupal::time()
      ->getRequestTime());
    if ($process_id !== NULL) {
      $this
        ->storeConfigUploadProcessId($mapper_id, $process_id);
    }
    return $document_id;
  }
  if ($this
    ->getConfigSourceStatus($mapper) == Lingotek::STATUS_DISABLED) {
    $this
      ->setConfigTargetStatuses($mapper, Lingotek::STATUS_DISABLED);
  }
  return FALSE;
}