You are here

function lingotek_entity_update in Lingotek Translation 8

Same name and namespace in other branches
  1. 8.2 lingotek.module \lingotek_entity_update()
  2. 7.7 lingotek.module \lingotek_entity_update()
  3. 7.5 lingotek.module \lingotek_entity_update()
  4. 7.6 lingotek.module \lingotek_entity_update()
  5. 4.0.x lingotek.module \lingotek_entity_update()
  6. 3.0.x lingotek.module \lingotek_entity_update()
  7. 3.1.x lingotek.module \lingotek_entity_update()
  8. 3.2.x lingotek.module \lingotek_entity_update()
  9. 3.3.x lingotek.module \lingotek_entity_update()
  10. 3.4.x lingotek.module \lingotek_entity_update()
  11. 3.5.x lingotek.module \lingotek_entity_update()
  12. 3.6.x lingotek.module \lingotek_entity_update()
  13. 3.7.x lingotek.module \lingotek_entity_update()
  14. 3.8.x lingotek.module \lingotek_entity_update()

Implements hook_entity_update().

File

./lingotek.module, line 166
lingotek.module

Code

function lingotek_entity_update(EntityInterface $entity) {

  // If it's new, has been already processed.
  if (isset($entity->lingotek_processed) && $entity->lingotek_processed) {
    return;
  }

  /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $configuration_service */
  $configuration_service = \Drupal::service('lingotek.configuration');
  if ($entity instanceof \Drupal\Core\Config\Entity\ConfigEntityInterface) {
    if (\Drupal::isConfigSyncing()) {

      // We don't want to react to configuration imports.
      return;
    }

    /** @var \Drupal\lingotek\LingotekConfigTranslationServiceInterface $translation_service */
    $translation_service = \Drupal::service('lingotek.config_translation');
    $entity_type_id = $entity
      ->getEntityTypeId();
    if ($entity instanceof \Drupal\field\Entity\FieldConfig) {
      $entity_type_id = $entity
        ->getTargetEntityTypeId() . '_fields';
    }
    if ($translation_service
      ->isEnabled($entity_type_id)) {
      $profile = $configuration_service
        ->getConfigEntityProfile($entity);
      $has_autoupload = $profile
        ->hasAutomaticUpload();
      $source_status = $translation_service
        ->getSourceStatus($entity);
      $entity_has_changed = $translation_service
        ->hasEntityChanged($entity);
      if ($has_autoupload) {

        // Updated entity with auto-upload
        $translation_service
          ->setSourceStatus($entity, Lingotek::STATUS_EDITED);
        try {
          $document_id = $translation_service
            ->updateDocument($entity);
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_IMPORTING);
          $translation_service
            ->markTranslationsAsDirty($entity);
        } catch (LingotekApiException $exception) {
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
          drupal_set_message(t('The update for @entity_type %title failed. Please try again.', array(
            '@entity_type' => $entity
              ->getEntityTypeId(),
            '%title' => $entity
              ->label(),
          )), 'error');
        }
      }
      elseif (!$has_autoupload) {
        if ($entity_has_changed) {
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_EDITED);
          $translation_service
            ->markTranslationsAsDirty($entity);
        }
      }
    }
  }
  elseif ($entity instanceof ContentEntityInterface) {
    if ($configuration_service
      ->isEnabled($entity
      ->getEntityTypeId(), $entity
      ->bundle())) {
      $profile = $configuration_service
        ->getEntityProfile($entity);
      $has_autoupload = $profile
        ->hasAutomaticUpload();

      /** @var \Drupal\lingotek\LingotekContentTranslationServiceInterface $translation_service */
      $translation_service = \Drupal::service('lingotek.content_translation');
      $source_status = $translation_service
        ->getSourceStatus($entity);
      $entity_has_changed = $translation_service
        ->hasEntityChanged($entity);
      if ($has_autoupload) {

        // New entity with auto-upload
        if ($source_status == NULL || $source_status == Lingotek::STATUS_UNTRACKED) {
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_EDITED);
          try {
            $document_id = $translation_service
              ->uploadDocument($entity);
            drupal_set_message(t('<em>@title</em> sent to Lingotek successfully.', array(
              '@title' => $entity
                ->label(),
            )));
          } catch (LingotekApiException $exception) {
            $translation_service
              ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
            drupal_set_message(t('The upload for @entity_type %title failed. Please try again.', array(
              '@entity_type' => $entity
                ->getEntityTypeId(),
              '%title' => $entity
                ->label(),
            )), 'error');
          }
        }
        elseif ($entity_has_changed) {
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_EDITED);
          try {
            $response = $translation_service
              ->updateDocument($entity);
            $translation_service
              ->setSourceStatus($entity, Lingotek::STATUS_IMPORTING);
            $translation_service
              ->markTranslationsAsDirty($entity);
            drupal_set_message(t('<em>@title</em> was updated and sent to Lingotek successfully.', array(
              '@title' => $entity
                ->label(),
            )));
          } catch (LingotekApiException $exception) {
            $translation_service
              ->setSourceStatus($entity, Lingotek::STATUS_ERROR);
            drupal_set_message(t('The update for @entity_type %title failed. Please try again.', array(
              '@entity_type' => $entity
                ->getEntityTypeId(),
              '%title' => $entity
                ->label(),
            )), 'error');
          }
        }
      }
      elseif (!$has_autoupload) {
        if ($entity_has_changed) {
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_EDITED);
          $translation_service
            ->markTranslationsAsDirty($entity);
        }
      }
    }
  }
}