You are here

public function LingotekEntityController::addTarget in Lingotek Translation 3.7.x

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

File

src/Controller/LingotekEntityController.php, line 172

Class

LingotekEntityController

Namespace

Drupal\lingotek\Controller

Code

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);
  }
  $bundleInfos = $this->entityTypeBundleInfo
    ->getBundleInfo($entity
    ->getEntityTypeId());
  if (!$entity
    ->getEntityType()
    ->isTranslatable() || !$bundleInfos[$entity
    ->bundle()]['translatable']) {
    \Drupal::messenger()
      ->addWarning(t('Cannot request target for @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 request target for @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);
  }
  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()
      ->addWarning(t('Document @entity_type %title has been archived. Uploading again.', [
      '@entity_type' => $entity
        ->getEntityTypeId(),
      '%title' => $entity
        ->label(),
    ]));
    return $this
      ->upload($entity
      ->getEntityTypeId(), $entity
      ->id());
  } catch (LingotekDocumentNotFoundException $exc) {
    $this
      ->messenger()
      ->addError(t('Document @entity_type %title was not found. 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);
}