You are here

public function LingotekEntityController::upload in Lingotek Translation 4.0.x

Same name and namespace in other branches
  1. 8 src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  2. 8.2 src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  3. 3.0.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  4. 3.1.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  5. 3.2.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  6. 3.3.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  7. 3.4.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  8. 3.5.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  9. 3.6.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  10. 3.7.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
  11. 3.8.x src/Controller/LingotekEntityController.php \Drupal\lingotek\Controller\LingotekEntityController::upload()
1 call to LingotekEntityController::upload()
LingotekEntityController::addTarget in src/Controller/LingotekEntityController.php
1 string reference to 'LingotekEntityController::upload'
lingotek.routing.yml in ./lingotek.routing.yml
lingotek.routing.yml

File

src/Controller/LingotekEntityController.php, line 224

Class

LingotekEntityController

Namespace

Drupal\lingotek\Controller

Code

public function upload($entity_type, $entity_id) {
  $entity = $this
    ->entityTypeManager()
    ->getStorage($entity_type)
    ->load($entity_id);
  if (!$entity) {

    // TODO: log warning
    return $this
      ->translationsPageRedirect($entity);
  }
  $bundleInfos = $this->entityTypeBundleInfo
    ->getBundleInfo($entity
    ->getEntityTypeId());
  if (!$entity
    ->getEntityType()
    ->isTranslatable() || !$bundleInfos[$entity
    ->bundle()]['translatable']) {
    \Drupal::messenger()
      ->addWarning(t('Cannot upload @type %label. That @bundle_label is not enabled for translation.', [
      '@type' => $bundleInfos[$entity
        ->bundle()]['label'],
      '%label' => $entity
        ->label(),
      '@bundle_label' => $entity
        ->getEntityType()
        ->getBundleLabel(),
    ]));
    return $this
      ->translationsPageRedirect($entity);
  }
  if (!$this->lingotekConfiguration
    ->isEnabled($entity
    ->getEntityTypeId(), $entity
    ->bundle())) {
    $this
      ->messenger()
      ->addWarning(t('Cannot upload @type %label. That @bundle_label is not enabled for Lingotek translation.', [
      '@type' => $bundleInfos[$entity
        ->bundle()]['label'],
      '%label' => $entity
        ->label(),
      '@bundle_label' => $entity
        ->getEntityType()
        ->getBundleLabel(),
    ]));
    return $this
      ->translationsPageRedirect($entity);
  }

  /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
  $translation_service = \Drupal::service('lingotek.content_translation');
  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);
}