diff --git a/src/GengoConnector.php b/src/GengoConnector.php index 2814312..f6d5703 100644 --- a/src/GengoConnector.php +++ b/src/GengoConnector.php @@ -431,17 +431,4 @@ class GengoConnector { return $this->delete('translate/job/' . $gengo_job_id); } - /** - * Retrieves a specific job details. - * - * @param int $gengo_job_id - * Gengo job id. - * - * @return object - * Gengo response data. - */ - public function retrieveJob($gengo_job_id) { - return $this->get('translate/job/' . $gengo_job_id); - } - } diff --git a/src/Plugin/tmgmt/Translator/MyGengoTranslator.php b/src/Plugin/tmgmt/Translator/MyGengoTranslator.php index 4ad95e2..a2038db 100644 --- a/src/Plugin/tmgmt/Translator/MyGengoTranslator.php +++ b/src/Plugin/tmgmt/Translator/MyGengoTranslator.php @@ -21,6 +21,7 @@ use Drupal\Core\Url; use Drupal\tmgmt\Entity\JobItem; use Drupal\tmgmt\Entity\RemoteMapping; use Drupal\tmgmt\Translator\AvailableResult; +use Drupal\tmgmt\JobItemInterface; /** * Gengo translation plugin controller. @@ -430,7 +431,7 @@ class MyGengoTranslator extends TranslatorPluginBase implements ContainerFactory * @param \Drupal\tmgmt\Entity\Job $job * Job that will be mapped to remote jobs. */ - public function fetchGengoJobs(Job $job) { + public function fetchGengoJobs(Job $job, $save_translations = TRUE) { // Search for placeholder item. $remotes = RemoteMapping::loadByLocalData($job->id()); @@ -540,7 +541,9 @@ class MyGengoTranslator extends TranslatorPluginBase implements ContainerFactory $matching_remote->save(); } } - $this->saveTranslation($job, $tjiid . '][' . $data_item_key, $response_job); + if ($save_translations) { + $this->saveTranslation($job, $tjiid . '][' . $data_item_key, $response_job); + } } } @@ -557,4 +560,47 @@ class MyGengoTranslator extends TranslatorPluginBase implements ContainerFactory } } + /** + * {@inheritdoc} + */ + public function abortTranslation(JobInterface $job) { + // Update the TMGMT jobs to Gengo jobs mapping. + $this->fetchGengoJobs($job, FALSE); + + $count_aborted = 0; + $remotes = RemoteMapping::loadByLocalData($job->id()); + // There is only 1 job item for each remotes. + /** @var JobItem $job_item */ + $job_item = reset($remotes)->getJobItem(); + + if (!$job_item->isAborted()) { + // Loop on Gengo translation jobs. + foreach ($remotes as $remote) { + if (!empty($remote->remote_identifier_2->value)) { + try { + $connector = new GengoConnector($job->getTranslator(), $this->client); + $connector->abortJob($remote->remote_identifier_2->value); + $count_aborted++; + } catch (TMGMTException $e) { + // Nothing to do. The abortion count isn't incremented. + } + } + } + + if ($count_aborted > 1) { + $job_item->addMessage('@count_aborted jobs out of @count_total have been aborted at Gengo.', array('@count_aborted' => $count_aborted, '@count_total' => count($remotes))); + } + else { + $job_item->addMessage('@count_aborted job out of @count_total has been aborted at Gengo.', array('@count_aborted' => $count_aborted, '@count_total' => count($remotes))); + } + + // Abort the job item even if Gengo job(s) couldn't be aborted. + $job_item->setState(JobItemInterface::STATE_ABORTED, 'Aborted by user.'); + $job_item->save(); + } + + // Abort the job in the current system. + return parent::abortTranslation($job); + } + } diff --git a/tmgmt_mygengo.module b/tmgmt_mygengo.module index 3fde621..0881595 100644 --- a/tmgmt_mygengo.module +++ b/tmgmt_mygengo.module @@ -218,48 +218,3 @@ function tmgmt_mygengo_access_check() { // @todo Add validation. return TRUE; } - -/** - * Implements hook_form_alter(). - */ -function tmgmt_mygengo_form_tmgmt_job_abort_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { - // Add a form submission handler to synchronize job abortion with Gengo. - $form['actions']['submit']['#submit'][] = '_tmgmt_mygengo_abort_form_submit'; -} - -/** - * Submit callback for job abortion on Gengo's side. - */ -function _tmgmt_mygengo_abort_form_submit($form, FormStateInterface $form_state) { - // Update the TMGMT jobs to Gengo jobs mapping. Calling fetchGengoJobs() adds - // a bit of overhead by saving the data to the database and trying to update - // the translations, but it avoids duplicating a lot of code. - /* @see \Drupal\tmgmt_mygengo\Plugin\tmgmt\Translator::fetchGengoJobs */ - $job = $form_state->getFormObject()->getEntity(); - $controller = $job->getTranslator()->getPlugin(); - $controller->fetchGengoJobs($job); - - $job = $form_state->getFormObject()->getEntity(); - $remotes = RemoteMapping::loadByLocalData($job->id()); - - foreach ($remotes as $remote) { - if (!empty($remote->remote_identifier_2->value)) { - $gengo_job_id = $remote->remote_identifier_2->value; - try { - // Gengo's API prevents job abortion if it's not "Available", so we - // we check for this and display a more informative message if it is. - $connector = new GengoConnector($job->getTranslator(), Drupal::httpClient()); - if ($connector->retrieveJob($gengo_job_id)['job']['status'] === 'available') { - $connector->abortJob($gengo_job_id); - } - else { - drupal_set_message(t('Unable to abort job @gengo_job_id. It has already been translated or is in progress.', ['@gengo_job_id' => $gengo_job_id]), 'error'); - } - } - catch (\Exception $e) { - watchdog_exception('tmgmt_mygengo', $e); - drupal_set_message(t('Unable to abort job @gengo_job_id. Error: @error', ['@gengo_job_id' => $gengo_job_id, '@error' => $e->getMessage()]), 'error'); - } - } - } -}