You are here

function lingotek_entity_update in Lingotek Translation 3.4.x

Same name and namespace in other branches
  1. 8 lingotek.module \lingotek_entity_update()
  2. 8.2 lingotek.module \lingotek_entity_update()
  3. 7.7 lingotek.module \lingotek_entity_update()
  4. 7.5 lingotek.module \lingotek_entity_update()
  5. 7.6 lingotek.module \lingotek_entity_update()
  6. 4.0.x lingotek.module \lingotek_entity_update()
  7. 3.0.x lingotek.module \lingotek_entity_update()
  8. 3.1.x lingotek.module \lingotek_entity_update()
  9. 3.2.x lingotek.module \lingotek_entity_update()
  10. 3.3.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 250
Implements Drupal-related hooks for the Lingotek Translation module.

Code

function lingotek_entity_update(EntityInterface $entity) {

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

  /** @var \Drupal\lingotek\LingotekConfigurationServiceInterface $configuration_service */
  $configuration_service = \Drupal::service('lingotek.configuration');
  if ($entity instanceof 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 FieldConfig) {
      $entity_type_id = $entity
        ->getTargetEntityTypeId() . '_fields';
    }
    if ($translation_service
      ->isEnabled($entity_type_id) && !in_array($entity
      ->language()
      ->getId(), [
      LanguageInterface::LANGCODE_NOT_SPECIFIED,
      LanguageInterface::LANGCODE_NOT_APPLICABLE,
    ])) {
      $profile = $configuration_service
        ->getConfigEntityProfile($entity);
      if ($profile === NULL) {

        // If for whatever reason there is no profile, we better be cautious and
        // do nothing. This can happen e.g. if there are modules implementing
        // hook_lingotek_content_entity_get_profile().
        return;
      }
      if ($profile
        ->id() === Lingotek::PROFILE_DISABLED) {
        $translation_service
          ->setSourceStatus($entity, Lingotek::STATUS_DISABLED);
        $translation_service
          ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
        return;
      }
      elseif ($translation_service
        ->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED) {
        return;
      }
      elseif ($translation_service
        ->getSourceStatus($entity) === Lingotek::STATUS_DISABLED) {
        if ($translation_service
          ->getDocumentId($entity) !== NULL) {
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
        }
        else {
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
        }
        $translation_service
          ->checkTargetStatuses($entity);
        return;
      }
      $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 (LingotekDocumentArchivedException $exception) {
          \Drupal::messenger()
            ->addError(t('Document @entity_type %title has been archived. Please upload again.', [
            '@entity_type' => $entity
              ->getEntityTypeId(),
            '%title' => $entity
              ->label(),
          ]));
        } catch (LingotekDocumentLockedException $exception) {
          \Drupal::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) {
          \Drupal::messenger()
            ->addError(t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.'));
        } catch (LingotekApiException $exception) {
          \Drupal::messenger()
            ->addError(t('The update for @entity_type %title failed. Please try again.', [
            '@entity_type' => $entity
              ->getEntityTypeId(),
            '%title' => $entity
              ->label(),
          ]));
        }
      }
      elseif (!$has_autoupload) {
        if ($entity_has_changed && $source_status !== Lingotek::STATUS_UNTRACKED) {
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_EDITED);
          $translation_service
            ->markTranslationsAsDirty($entity);
        }
      }
    }
  }
  elseif ($entity instanceof ContentEntityInterface) {
    if ($configuration_service
      ->isEnabled($entity
      ->getEntityTypeId(), $entity
      ->bundle())) {
      if ($entity instanceof ContentEntityInterface && $configuration_service
        ->isEnabled($entity
        ->getEntityTypeId(), $entity
        ->bundle())) {
        if ($entity->lingotek_metadata->entity !== NULL) {
          if (!$entity->lingotek_metadata->entity
            ->getContentEntityId()) {
            $entity->lingotek_metadata->entity
              ->setEntity($entity);
            $entity->lingotek_metadata->entity
              ->save();
          }
        }
      }

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

        // If workbench moderation is enabled, we need to prevent that content is
        // processed if not the right state.

        /** @var \Drupal\lingotek\Moderation\LingotekModerationFactoryInterface $moderation_factory */
        $moderation_factory = \Drupal::service('lingotek.moderation_factory');
        $moderation_handler = $moderation_factory
          ->getModerationHandler();
        $preventUpload = $moderation_handler
          ->shouldModerationPreventUpload($entity);
        $profile = $configuration_service
          ->getEntityProfile($entity);
        if ($profile === NULL) {

          // If for whatever reason there is no profile, we better be cautious and
          // do nothing. This can happen e.g. if there are modules implementing
          // hook_lingotek_content_entity_get_profile().
          return;
        }
        if ($profile
          ->id() === Lingotek::PROFILE_DISABLED) {
          $translation_service
            ->setSourceStatus($entity, Lingotek::STATUS_DISABLED);
          $translation_service
            ->setTargetStatuses($entity, Lingotek::STATUS_DISABLED);
          return;
        }
        elseif ($translation_service
          ->getSourceStatus($entity) === Lingotek::STATUS_CANCELLED) {
          return;
        }
        elseif ($translation_service
          ->getSourceStatus($entity) === Lingotek::STATUS_DISABLED) {
          if ($translation_service
            ->getDocumentId($entity) !== NULL) {
            $translation_service
              ->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
          }
          else {
            $translation_service
              ->setSourceStatus($entity, Lingotek::STATUS_CURRENT);
          }
          $translation_service
            ->checkTargetStatuses($entity);
          return;
        }
        $has_autoupload = !$preventUpload && $profile
          ->hasAutomaticUpload();
        $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::messenger()
                ->addStatus(t('<em>@title</em> sent to Lingotek successfully.', [
                '@title' => $entity
                  ->label(),
              ]));
            } catch (LingotekApiException $exception) {
              \Drupal::messenger()
                ->addError(t('The upload for @entity_type %title failed. Please try again.', [
                '@entity_type' => $entity
                  ->getEntityTypeId(),
                '%title' => $entity
                  ->label(),
              ]));
            }
          }
          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::messenger()
                ->addStatus(t('<em>@title</em> was updated and sent to Lingotek successfully.', [
                '@title' => $entity
                  ->label(),
              ]));
            } catch (LingotekDocumentArchivedException $exception) {
              \Drupal::messenger()
                ->addError(t('Document @entity_type %title has been archived. Please upload again.', [
                '@entity_type' => $entity
                  ->getEntityTypeId(),
                '%title' => $entity
                  ->label(),
              ]));
            } catch (LingotekDocumentLockedException $exception) {
              \Drupal::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) {
              \Drupal::messenger()
                ->addError(t('Community has been disabled. Please contact support@lingotek.com to re-enable your community.'));
            } catch (LingotekApiException $exception) {
              \Drupal::messenger()
                ->addError(t('The update for @entity_type %title failed. Please try again.', [
                '@entity_type' => $entity
                  ->getEntityTypeId(),
                '%title' => $entity
                  ->label(),
              ]));
            }
          }
        }
        elseif (!$has_autoupload) {
          if ($entity_has_changed && $source_status !== Lingotek::STATUS_UNTRACKED) {
            $translation_service
              ->setSourceStatus($entity, Lingotek::STATUS_EDITED);
            $translation_service
              ->markTranslationsAsDirty($entity);
          }
        }
      }
      else {
        $langcode = $entity
          ->language()
          ->getId();

        // Edited a translation.
        if ($current_status = $translation_service
          ->getTargetStatus($entity, $langcode)) {

          // Only mark as out of date the current ones.
          $to_change = [
            Lingotek::STATUS_CURRENT,
          ];
          if (in_array($current_status, $to_change)) {
            $translation_service
              ->setTargetStatus($entity, $langcode, Lingotek::STATUS_EDITED);
          }
        }
      }
    }
  }
}