You are here

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

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

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

Parameters

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

File

src/LingotekConfigTranslationService.php, line 524

Class

LingotekConfigTranslationService
Service for managing Lingotek configuration translations.

Namespace

Drupal\lingotek

Code

public function addTarget(ConfigEntityInterface &$entity, $locale) {
  $profile = $this->lingotekConfiguration
    ->getConfigEntityProfile($entity);
  $drupal_language = $this->languageLocaleMapper
    ->getConfigurableLanguageForLocale($locale);
  if ($profile
    ->id() === Lingotek::PROFILE_DISABLED || $this
    ->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED || $profile
    ->hasDisabledTarget($drupal_language
    ->getId())) {
    return FALSE;
  }
  if ($locale == $this->languageLocaleMapper
    ->getLocaleForLangcode($entity
    ->language()
    ->getId())) {

    // We don't want to translate from one language to itself.
    return FALSE;
  }
  if ($document_id = $this
    ->getDocumentId($entity)) {
    $source_status = $this
      ->getSourceStatus($entity);
    $current_status = $this
      ->getTargetStatus($entity, $this->languageLocaleMapper
      ->getConfigurableLanguageForLocale($locale)
      ->getId());
    if ($current_status !== Lingotek::STATUS_PENDING && $current_status !== Lingotek::STATUS_CURRENT && $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) {
        $this
          ->setTargetStatus($entity, $this->languageLocaleMapper
          ->getConfigurableLanguageForLocale($locale)
          ->getId(), 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;
}