You are here

class LingotekEntityController in Lingotek Translation 3.0.x

Same name and namespace in other branches
  1. 8 src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  2. 8.2 src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  3. 4.0.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  4. 3.1.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  5. 3.2.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  6. 3.3.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  7. 3.4.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  8. 3.5.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  9. 3.6.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  10. 3.7.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController
  11. 3.8.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController

Hierarchy

Expanded class hierarchy of LingotekEntityController

File

src/Controller/LingotekEntityController.php, line 15

Namespace

Drupal\lingotek\Controller
View source
class LingotekEntityController extends LingotekControllerBase {
  protected $translations_link;
  public function checkUpload($doc_id) {

    /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.content_translation');
    $entity = $translation_service
      ->loadByDocumentId($doc_id);
    if (!$entity) {

      // TODO: log warning
      return $this
        ->translationsPageRedirect($entity);
    }
    try {
      if ($translation_service
        ->checkSourceStatus($entity)) {
        $this
          ->messenger()
          ->addStatus(t('The import for @entity_type %title is complete.', [
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
        ]));
      }
      else {
        $this
          ->messenger()
          ->addStatus(t('The import for @entity_type %title is still pending.', [
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
        ]));
      }
    } catch (LingotekApiException $exception) {
      $this
        ->messenger()
        ->addError(t('The check for @entity_type status failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    }
    return $this
      ->translationsPageRedirect($entity);
  }
  public function checkTarget($doc_id, $locale) {

    /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.content_translation');
    $entity = $translation_service
      ->loadByDocumentId($doc_id);
    if (!$entity) {

      // TODO: log warning
      return $this
        ->translationsPageRedirect($entity);
    }
    $drupal_language = $this->languageLocaleMapper
      ->getConfigurableLanguageForLocale($locale);
    try {
      if ($translation_service
        ->checkTargetStatus($entity, $drupal_language
        ->id()) === Lingotek::STATUS_READY) {
        $this
          ->messenger()
          ->addStatus(t('The @locale translation for @entity_type %title is ready for download.', [
          '@locale' => $locale,
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
        ]));
      }
      else {
        $this
          ->messenger()
          ->addStatus(t('The @locale translation for @entity_type %title is still in progress.', [
          '@locale' => $locale,
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
        ]));
      }
    } catch (LingotekApiException $exc) {
      $this
        ->messenger()
        ->addError(t('The request for @entity_type translation status failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    }
    return $this
      ->translationsPageRedirect($entity);
  }
  public function addTarget($doc_id, $locale) {

    /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.content_translation');
    $entity = $translation_service
      ->loadByDocumentId($doc_id);
    if (!$entity) {

      // TODO: log warning
      return $this
        ->translationsPageRedirect($entity);
    }
    try {
      if ($translation_service
        ->addTarget($entity, $locale)) {
        $this
          ->messenger()
          ->addStatus(t("Locale '@locale' was added as a translation target for @entity_type %title.", [
          '@locale' => $locale,
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
        ]));
      }
      else {
        $this
          ->messenger()
          ->addWarning(t("There was a problem adding '@locale' as a translation target for @entity_type %title.", [
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
          '@locale' => $locale,
        ]));
      }
    } catch (LingotekDocumentArchivedException $exception) {
      $this
        ->messenger()
        ->addError(t('Document @entity_type %title has been archived. Please upload again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    } catch (LingotekDocumentLockedException $exception) {
      $this
        ->messenger()
        ->addError(t('Document @entity_type %title has a new version. The document id has been updated for all future interactions. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    } catch (LingotekPaymentRequiredException $exception) {
      $this
        ->messenger()
        ->addError(t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.'));
    } catch (LingotekApiException $exception) {
      $this
        ->messenger()
        ->addError(t('The translation request for @entity_type failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]), 'error');
    }
    return $this
      ->translationsPageRedirect($entity);
  }
  public function upload($entity_type, $entity_id) {

    /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.content_translation');
    $entity = $this
      ->entityTypeManager()
      ->getStorage($entity_type)
      ->load($entity_id);
    try {
      if ($translation_service
        ->uploadDocument($entity)) {
        $this
          ->messenger()
          ->addStatus(t('@entity_type %title has been uploaded.', [
          '@entity_type' => ucfirst($entity
            ->getEntityTypeId()),
          '%title' => $entity
            ->label(),
        ]));
      }
      else {
        $this
          ->messenger()
          ->addError(t('The upload for @entity_type %title failed. Check your configuration and profile and try again.', [
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
        ]));
      }
    } catch (LingotekPaymentRequiredException $exception) {
      $this
        ->messenger()
        ->addError(t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.'));
    } catch (LingotekApiException $exception) {
      $this
        ->messenger()
        ->addError(t('The upload for @entity_type %title failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    }
    return $this
      ->translationsPageRedirect($entity);
  }
  public function update($doc_id) {

    /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.content_translation');
    $entity = $translation_service
      ->loadByDocumentId($doc_id);
    try {
      if ($translation_service
        ->updateDocument($entity)) {
        $this
          ->messenger()
          ->addStatus(t('@entity_type %title has been updated.', [
          '@entity_type' => ucfirst($entity
            ->getEntityTypeId()),
          '%title' => $entity
            ->label(),
        ]));
      }
      else {
        $this
          ->messenger()
          ->addError(t('The upload for @entity_type %title failed. Check your configuration and profile and try again.', [
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
        ]));
      }
    } catch (LingotekDocumentArchivedException $exception) {
      $this
        ->messenger()
        ->addError(t('Document @entity_type %title has been archived. Please upload again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    } catch (LingotekDocumentLockedException $exception) {
      $this
        ->messenger()
        ->addError(t('Document @entity_type %title has a new version. The document id has been updated for all future interactions. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    } catch (LingotekPaymentRequiredException $exception) {
      $this
        ->messenger()
        ->addError(t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.'));
    } catch (LingotekApiException $exception) {
      $this
        ->messenger()
        ->addError(t('The update for @entity_type %title failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    }
    return $this
      ->translationsPageRedirect($entity);
  }
  public function download($doc_id, $locale) {

    /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.content_translation');
    $entity = $translation_service
      ->loadByDocumentId($doc_id);
    if (!$entity) {

      // TODO: log warning
      return $this
        ->translationsPageRedirect($entity);
    }
    try {
      if ($translation_service
        ->downloadDocument($entity, $locale)) {
        $this
          ->messenger()
          ->addStatus(t('The translation of @entity_type %title into @locale has been downloaded.', [
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
          '@locale' => $locale,
        ]));
      }
      else {
        \Drupal::logger('lingotek')
          ->warning($this
          ->t('Error happened trying to download (%entity_id,%revision_id).', [
          '%entity_id' => $entity
            ->id(),
          '%revision_id' => $entity
            ->getRevisionId(),
        ]));
        $this
          ->messenger()
          ->addError(t('The translation of @entity_type %title into @locale failed to download.', [
          '@entity_type' => $entity
            ->getEntityTypeId(),
          '%title' => $entity
            ->label(),
          '@locale' => $locale,
        ]));
      }
    } catch (LingotekApiException $exception) {
      $this
        ->messenger()
        ->addError(t('The download for @entity_type %title failed. Please try again.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
      ]));
    } catch (LingotekContentEntityStorageException $storage_exception) {
      $this
        ->messenger()
        ->addError(t('The download for @entity_type %title failed because of the length of one field translation value: %table.', [
        '@entity_type' => $entity
          ->getEntityTypeId(),
        '%title' => $entity
          ->label(),
        '%table' => $storage_exception
          ->getTable(),
      ]));
    }
    return $this
      ->translationsPageRedirect($entity);
  }
  protected function translationsPageRedirect(EntityInterface $entity) {
    $entity_type_id = $entity
      ->getEntityTypeId();
    $uri = Url::fromRoute("entity.{$entity_type_id}.content_translation_overview", [
      $entity_type_id => $entity
        ->id(),
    ]);
    $entity_type = $entity
      ->getEntityType();
    if ($entity_type
      ->hasLinkTemplate('canonical')) {
      return new RedirectResponse($uri
        ->setAbsolute(TRUE)
        ->toString());
    }
    else {
      return new RedirectResponse($this->request
        ->getUri());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LingotekControllerBase::$configFactory protected property The config factory. Overrides ControllerBase::$configFactory
LingotekControllerBase::$formBuilder protected property The form builder. Overrides ControllerBase::$formBuilder
LingotekControllerBase::$languageLocaleMapper protected property The language-locale mapper.
LingotekControllerBase::$logger protected property A logger instance.
LingotekControllerBase::$request protected property A Symfony request instance
LingotekControllerBase::connected public function Checks if site is connected to Lingotek.
LingotekControllerBase::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create 4
LingotekControllerBase::__construct public function Constructs a LingotekControllerBase object. 4
LingotekEntityController::$translations_link protected property
LingotekEntityController::addTarget public function
LingotekEntityController::checkTarget public function
LingotekEntityController::checkUpload public function
LingotekEntityController::download public function
LingotekEntityController::translationsPageRedirect protected function
LingotekEntityController::update public function
LingotekEntityController::upload public function
LingotekSetupTrait::$lingotek protected property A lingotek connector object
LingotekSetupTrait::checkSetup protected function Verify the Lingotek Translation module has been properly initialized.
LingotekSetupTrait::setupCompleted public function Checks if Lingotek module is completely set up.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.