function tmgmt_cron in Translation Management Tool 8
Same name and namespace in other branches
- 7 tmgmt.module \tmgmt_cron()
Implements hook_cron().
2 calls to tmgmt_cron()
- ContentEntitySourceUnitTest::testSubmitContinuousOnCron in sources/
content/ tests/ src/ Kernel/ ContentEntitySourceUnitTest.php - Test submit continuous job items on cron.
- LocalTranslatorContinuousTest::testContinuousJobs in translators/
tmgmt_local/ tests/ src/ Functional/ LocalTranslatorContinuousTest.php - Test continuous Jobs in TMGMT local.
File
- ./
tmgmt.module, line 74 - Main module file for the Translation Management module.
Code
function tmgmt_cron() {
$offset = \Drupal::config('tmgmt.settings')
->get('purge_finished');
if ($offset != '_never') {
// Delete all finished translation jobs that haven't been changed for a
// time span longer than the given offset.
$ids = \Drupal::entityQuery('tmgmt_job')
->condition('state', Job::STATE_FINISHED)
->condition('changed', \Drupal::time()
->getRequestTime() - $offset, '<=')
->execute();
if (!empty($ids)) {
$storage = \Drupal::entityTypeManager()
->getStorage('tmgmt_job');
$entities = $storage
->loadMultiple($ids);
$storage
->delete($entities);
}
}
// Submit continuous job items.
$tmgmt_settings = \Drupal::config('tmgmt.settings');
if ($tmgmt_settings
->get('submit_job_item_on_cron')) {
// Look for inactive job items of continuous jobs.
$ids = \Drupal::entityQuery('tmgmt_job_item')
->condition('tjid.entity.job_type', JobInterface::TYPE_CONTINUOUS)
->condition('state', JobItemInterface::STATE_INACTIVE)
->range(0, $tmgmt_settings
->get('job_items_cron_limit'))
->sort('tjiid')
->execute();
// First, group the job items by job.
$job_items_by_job = [];
foreach (JobItem::loadMultiple($ids) as $item) {
$job_items_by_job[$item
->getJobId()][] = $item;
}
// Then submit all items in a group per job.
foreach ($job_items_by_job as $all_items) {
$translator = reset($all_items)
->getTranslatorPlugin();
// Call the hook before requesting the translation.
// @see hook_tmgmt_job_before_request_translation()
\Drupal::moduleHandler()
->invokeAll('tmgmt_job_before_request_translation', [
$all_items,
]);
// We do not want to translate the items that are already translated,
// so we filter them.
$items = array_filter($all_items, function (JobItemInterface $item) {
return $item
->getCountPending() > 0;
});
if (!empty($items)) {
$translator
->requestJobItemsTranslation($items);
}
// Reset it again so getData returns again all the values.
// @see hook_tmgmt_job_after_request_translation()
\Drupal::moduleHandler()
->invokeAll('tmgmt_job_after_request_translation', [
$all_items,
]);
}
}
}