You are here

public function GlobalLinkTranslator::abortTranslation in GlobalLink Connect for Drupal 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php \Drupal\globallink\Plugin\tmgmt\Translator\GlobalLinkTranslator::abortTranslation()

Aborts a translation job.

Parameters

\Drupal\tmgmt\JobInterface $job: The job that should have its translation aborted.

Return value

bool TRUE if the job could be aborted, FALSE otherwise.

Overrides TranslatorPluginBase::abortTranslation

File

src/Plugin/tmgmt/Translator/GlobalLinkTranslator.php, line 572

Class

GlobalLinkTranslator
GlobalLink translation plugin controller.

Namespace

Drupal\globallink\Plugin\tmgmt\Translator

Code

public function abortTranslation(JobInterface $job) {
  $this
    ->setTranslator($job
    ->getTranslator());
  $settings = $this->translator
    ->getSettings();
  $remotes = RemoteMapping::loadByLocalData($job
    ->id());
  if (!empty($remotes)) {

    // Because globallink does not support multiple items per job, all of our
    // job items have the same remote identifier. So we need to cancel the
    // submission just one time.
    try {
      $first_remote = array_shift($remotes);
      reset($first_remote);
      $pd_config = $this->glExchangeAdapter
        ->getPDConfig($settings);
      $glexchange = $this->glExchangeAdapter
        ->getGlExchange($pd_config);
      $glexchange
        ->cancelSubmission($first_remote
        ->getRemoteIdentifier1(), 'Submission aborted by user');

      /** @var RemoteMappingInterface $remote */
      foreach ($remotes as $remote) {

        /** @var JobItem $job_item */
        $job_item = $remote
          ->getJobItem();
        if (!$job_item
          ->isAborted()) {
          try {
            $job_item
              ->setState(JobItemInterface::STATE_ABORTED, 'Aborted by user.');
          } catch (TMGMTException $e) {
            $job_item
              ->addMessage('Abortion failed: @error', [
              '@error' => $e
                ->getMessage(),
            ], 'error');
          }
          $job_item
            ->save();
        }
      }
    } catch (\Exception $e) {
      $job
        ->addMessage('Abortion failed: @error', [
        '@error' => $e
          ->getMessage(),
      ], 'error');
    }
  }

  // Abort the job in the current system.
  return parent::abortTranslation($job);
}