You are here

public function LingotekConfigTranslationController::upload in Lingotek Translation 8

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

File

src/Controller/LingotekConfigTranslationController.php, line 200

Class

LingotekConfigTranslationController

Namespace

Drupal\lingotek\Controller

Code

public function upload($entity_type, $entity_id, Request $request) {
  if ($entity_type === $entity_id) {

    // It is not a config entity, but a config object.
    $definition = $this->configMapperManager
      ->getDefinition($entity_type);
    $mappers = $this->configMapperManager
      ->getMappers();
    if ($this->translationService
      ->getConfigDocumentId($mappers[$entity_type])) {
      try {
        if ($this->translationService
          ->updateConfig($entity_type)) {
          drupal_set_message($this
            ->t('%label has been updated.', [
            '%label' => $definition['title'],
          ]));
        }
      } catch (LingotekApiException $e) {

        // Mark the document as failed.
        $this->translationService
          ->setConfigSourceStatus($mappers[$entity_type], Lingotek::STATUS_ERROR);
        drupal_set_message($this
          ->t('%label update failed. Please try again.', [
          '%label' => $mappers[$entity_type]
            ->getTitle(),
        ]), 'error');
      }
    }
    else {
      try {
        if ($this->translationService
          ->uploadConfig($entity_type)) {
          drupal_set_message($this
            ->t('%label uploaded successfully', [
            '%label' => $definition['title'],
          ]));
        }
      } catch (LingotekApiException $e) {

        // Mark the document as failed.
        $this->translationService
          ->setConfigSourceStatus($mappers[$entity_type], Lingotek::STATUS_ERROR);
        drupal_set_message($this
          ->t('%label upload failed. Please try again.', [
          '%label' => $mappers[$entity_type]
            ->getTitle(),
        ]), 'error');
      }
    }
    return $this
      ->redirectToConfigTranslateOverview($entity_type);
  }

  // Check if it's a field.
  if (substr($entity_type, -7) == '_fields') {

    // Hack for fields, the entity is field config.
    $entity_type = 'field_config';
  }
  $entity = $this
    ->entityManager()
    ->getStorage($entity_type)
    ->load($entity_id);
  if ($this->translationService
    ->getDocumentId($entity)) {
    try {
      if ($this->translationService
        ->updateDocument($entity)) {
        drupal_set_message($this
          ->t('%label has been updated.', [
          '%label' => $entity
            ->label(),
        ]));
      }
    } catch (LingotekApiException $e) {

      // Mark the document as failed.
      $this->translationService
        ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
      drupal_set_message($this
        ->t('%label update failed. Please try again.', [
        '%label' => $entity
          ->label(),
      ]), 'error');
    }
  }
  else {
    try {
      if ($this->translationService
        ->uploadDocument($entity)) {
        drupal_set_message($this
          ->t('%label uploaded successfully', [
          '%label' => $entity
            ->label(),
        ]));
      }
    } catch (LingotekApiException $e) {

      // Mark the document as failed.
      $this->translationService
        ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
      drupal_set_message($this
        ->t('%label upload failed. Please try again.', [
        '%label' => $entity
          ->label(),
      ]), 'error');
    }
  }
  return $this
    ->redirectToEntityTranslateOverview($entity_type, $entity_id);
}