You are here

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

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

Request a translation for a given entity in the given locale.

Parameters

\Drupal\Core\Entity\ContentEntityInterface &$entity: The entity which target we want to add.

string $locale: Lingotek translation language which we want to modify.

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekDocumentArchivedException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekContentTranslationServiceInterface::addTarget

File

src/LingotekContentTranslationService.php, line 722

Class

LingotekContentTranslationService
Service for managing Lingotek content translations.

Namespace

Drupal\lingotek

Code

public function addTarget(ContentEntityInterface &$entity, $locale) {
  $profile = $this->lingotekConfiguration
    ->getEntityProfile($entity);
  if ($profile
    ->id() === Lingotek::PROFILE_DISABLED || $this
    ->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED) {
    return FALSE;
  }
  $source_langcode = $entity
    ->getUntranslated()
    ->language()
    ->getId();
  $source_locale = $this->languageLocaleMapper
    ->getLocaleForLangcode($source_langcode);
  if ($locale == $source_locale) {

    // We don't want to translate from one language to itself.
    return FALSE;
  }
  if ($document_id = $this
    ->getDocumentId($entity)) {
    $drupal_language = $this->languageLocaleMapper
      ->getConfigurableLanguageForLocale($locale);
    $source_status = $this
      ->getSourceStatus($entity);
    $current_status = $this
      ->getTargetStatus($entity, $drupal_language
      ->id());

    // When a translation is in one of these states, we know that it hasn't yet been sent up to the Lingotek API,
    // which means that we'll have to call addTarget() on it.
    //
    // TODO: should we consider STATUS_NONE as a "pristine" status?
    $pristine_statuses = [
      Lingotek::STATUS_REQUEST,
      Lingotek::STATUS_UNTRACKED,
      Lingotek::STATUS_EDITED,
    ];
    if (in_array($current_status, $pristine_statuses)) {
      try {
        $result = $this->lingotek
          ->addTarget($document_id, $locale, $this->lingotekConfiguration
          ->getEntityProfile($entity));
      } catch (LingotekDocumentLockedException $exception) {
        $this
          ->setDocumentId($entity, $exception
          ->getNewDocumentId());
        throw $exception;
      } catch (LingotekDocumentArchivedException $exception) {
        $this
          ->setDocumentId($entity, NULL);
        $this
          ->deleteMetadata($entity);
        throw $exception;
      } catch (LingotekPaymentRequiredException $exception) {
        throw $exception;
      } catch (LingotekApiException $exception) {
        throw $exception;
      }
      if ($result) {
        $this
          ->setTargetStatus($entity, $drupal_language
          ->id(), Lingotek::STATUS_PENDING);

        // If the status was "Importing", and the target was added
        // successfully, we can ensure that the content is current now.
        if ($source_status == Lingotek::STATUS_IMPORTING) {
          $this
            ->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
        }
        return TRUE;
      }
    }
  }
  if ($this
    ->getSourceStatus($entity) == Lingotek::STATUS_DISABLED) {
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
  }
  return FALSE;
}