You are here

public function LingotekConfigTranslationService::updateDocument in Lingotek Translation 3.4.x

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

Resends a document to the translation service.

Parameters

\Drupal\Core\Config\Entity\ConfigEntityInterface $entity: The entity being updated.

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

Return value

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

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekDocumentArchivedException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekConfigTranslationServiceInterface::updateDocument

1 call to LingotekConfigTranslationService::updateDocument()
LingotekConfigTranslationService::uploadDocument in src/LingotekConfigTranslationService.php
Uploads a document to the Lingotek service.

File

src/LingotekConfigTranslationService.php, line 445

Class

LingotekConfigTranslationService
Service for managing Lingotek configuration translations.

Namespace

Drupal\lingotek

Code

public function updateDocument(ConfigEntityInterface &$entity, $job_id = NULL) {
  $profile = $this->lingotekConfiguration
    ->getConfigEntityProfile($entity);
  if ($profile
    ->id() === Lingotek::PROFILE_DISABLED || $this
    ->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED) {
    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;
  }
  $source_data = $this
    ->getSourceData($entity);
  $document_id = $this
    ->getDocumentId($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);
  $newDocumentID = NULL;
  try {
    $newDocumentID = $this->lingotek
      ->updateDocument($document_id, $source_data, $url, $document_name, $this->lingotekConfiguration
      ->getConfigEntityProfile($entity), $job_id, $this
      ->getSourceLocale($entity));
  } catch (LingotekDocumentLockedException $exception) {
    $this
      ->setDocumentId($entity, $exception
      ->getNewDocumentId());

    // TODO: It shouldn't be needed here, EDITED status should already be set.
    $this
      ->setSourceStatus($entity, Lingotek::STATUS_EDITED);
    throw $exception;
  } catch (LingotekDocumentArchivedException $exception) {
    $this
      ->setDocumentId($entity, NULL);
    $this
      ->deleteMetadata($entity);
    throw $exception;
  } catch (LingotekPaymentRequiredException $exception) {
    $this
      ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
    throw $exception;
  } catch (LingotekApiException $exception) {
    $this
      ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
    throw $exception;
  }
  if ($newDocumentID) {
    if (is_string($newDocumentID)) {
      $this
        ->setDocumentId($entity, $newDocumentID);
    }
    $this
      ->setSourceStatus($entity, Lingotek::STATUS_IMPORTING);
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_PENDING);
    $this
      ->setJobId($entity, $job_id);
    $this
      ->setLastUpdated($entity, \Drupal::time()
      ->getRequestTime());
    return $newDocumentID;
  }
  if ($this
    ->getSourceStatus($entity) == Lingotek::STATUS_DISABLED) {
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
  }
  return FALSE;
}