tmgmt_globaldoc-8.x-1.0/tmgmt_globaldoc.module
tmgmt_globaldoc.module
<?php
/**
* @file
* Module file of the Translation Management GlobalDoc module.
*/
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Entity\Translator;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\JobItemInterface;
use Drupal\tmgmt\TranslatorInterface;
use Drupal\tmgmt_globaldoc\Plugin\tmgmt\Translator\GlobalDocTranslator;
/**
* Implements hook_cron().
*/
function tmgmt_globaldoc_cron() {
$translators = Translator::loadMultiple();
// First get all translator IDs that use GlobalDoc.
$globaldoc_translator_ids = array_keys(array_filter($translators, function (TranslatorInterface $translator) {
$translator_plugin = $translator->getPlugin();
return $translator_plugin instanceof GlobalDocTranslator;
}));
// Then fetch all active jobs using those translators.
$job_ids = \Drupal::entityQuery('tmgmt_job')
->condition('translator', $globaldoc_translator_ids, 'IN')
->condition('state', [
JobInterface::STATE_ACTIVE,
], 'IN')
->execute();
// Loop over all jobs and check if they have at least one active job item.
foreach ($job_ids as $job_id) {
$item_ids = \Drupal::entityQuery('tmgmt_job_item')
->condition('tjid', $job_id, '=')
->condition('state', [JobItemInterface::STATE_ACTIVE], 'IN')
->sort('tjid', 'DESC')
->sort('tjiid', 'DESC')
->range(0, 1)
->execute();
if ($item_ids) {
$job = Job::load($job_id);
$task_status = $job->getTranslatorPlugin()->getTaskState($job->getTranslator(), $job->getReference());
if ($task_status->return == GlobalDocTranslator::STATUS_SOURCE_COMPLETED) {
$job->getTranslatorPlugin()->fetchTranslation($job);
}
}
}
}
