You are here

public function LingotekConfigTranslationService::requestTranslations in Lingotek Translation 3.6.x

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

Requests translations of a document in all the enabled locales.

Parameters

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

File

src/LingotekConfigTranslationService.php, line 577

Class

LingotekConfigTranslationService
Service for managing Lingotek configuration translations.

Namespace

Drupal\lingotek

Code

public function requestTranslations(ConfigEntityInterface &$entity) {
  $profile = $this->lingotekConfiguration
    ->getConfigEntityProfile($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
      ->language()
      ->getId();
    foreach ($target_languages as $langcode => $language) {
      $locale = $this->languageLocaleMapper
        ->getLocaleForLangcode($langcode);
      if ($langcode !== $entity_langcode) {
        if (!$profile
          ->hasDisabledTarget($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
                ->getConfigEntityProfile($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;
}