You are here

public function LingotekContentTranslationService::requestTranslations in Lingotek Translation 3.3.x

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

Requests translations of a document in all the enabled locales.

Parameters

\Drupal\Core\Entity\ContentEntityInterface &$entity: The entity being requested for translations.

Throws

\Drupal\lingotek\Exception\LingotekPaymentRequiredException

\Drupal\lingotek\Exception\LingotekDocumentArchivedException

\Drupal\lingotek\Exception\LingotekDocumentLockedException

\Drupal\lingotek\Exception\LingotekApiException

Overrides LingotekContentTranslationServiceInterface::requestTranslations

File

src/LingotekContentTranslationService.php, line 863

Class

LingotekContentTranslationService
Service for managing Lingotek content translations.

Namespace

Drupal\lingotek

Code

public function requestTranslations(ContentEntityInterface &$entity) {
  $profile = $this->lingotekConfiguration
    ->getEntityProfile($entity);
  if ($profile
    ->id() === Lingotek::PROFILE_DISABLED || $this
    ->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED) {
    return FALSE;
  }
  $languages = [];
  if ($document_id = $this
    ->getDocumentId($entity)) {
    $target_languages = $this->languageManager
      ->getLanguages();
    $target_languages = array_filter($target_languages, function (LanguageInterface $language) {
      $configLanguage = ConfigurableLanguage::load($language
        ->getId());
      return $this->lingotekConfiguration
        ->isLanguageEnabled($configLanguage);
    });
    $entity_langcode = $entity
      ->getUntranslated()
      ->language()
      ->getId();
    foreach ($target_languages as $langcode => $language) {
      $locale = $this->languageLocaleMapper
        ->getLocaleForLangcode($langcode);
      if ($langcode !== $entity_langcode) {
        $source_status = $this
          ->getSourceStatus($entity);
        $current_status = $this
          ->getTargetStatus($entity, $langcode);
        if ($current_status !== Lingotek::STATUS_PENDING && $current_status !== Lingotek::STATUS_CURRENT && $current_status !== Lingotek::STATUS_EDITED && $current_status !== Lingotek::STATUS_READY) {
          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) {
            $languages[] = $langcode;
            $this
              ->setTargetStatus($entity, $langcode, 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);
            }
          }
        }
      }
    }
  }
  if ($this
    ->getSourceStatus($entity) == Lingotek::STATUS_DISABLED) {
    $this
      ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
  }
  return $languages;
}