function LocaleCronHooks::cron

Implements hook_cron().

Attributes

#[Hook('cron')]

See also

\Drupal\locale\Plugin\QueueWorker\LocaleTranslation

File

core/modules/locale/src/Hook/LocaleCronHooks.php, line 35

Class

LocaleCronHooks
Cron Hook implementation for locale.

Namespace

Drupal\locale\Hook

Code

public function cron() : void {
  $config = $this->configFactory
    ->get('locale.settings');
  // Update translations only when an update frequency was set by the admin
  // and a translatable language was set.
  // Update tasks are added to the queue here but processed by Drupal's cron.
  if ($config->get('translation.update_interval_days') && locale_translatable_language_list()) {
    // Determine which project+language should be updated.
    $request_time = $this->time
      ->getRequestTime();
    $check_time = $request_time - $config->get('translation.update_interval_days') * 3600 * 24;
    $projects = $this->localeProjectRepository
      ->getAll();
    $projects = array_filter($projects, fn(LocaleTranslatableProject $project): bool => $project->getStatus());
    $outdatedImports = $this->currentImportStorage
      ->getOutdatedImports(array_keys($projects), $request_time, $check_time);
    // For each project+language combination a number of tasks are added to
    // the queue.
    if ($outdatedImports) {
      $options = LocaleDefaultOptions::updateOptions();
      $queue = $this->queueFactory
        ->get('locale_translation', TRUE);
      foreach ($outdatedImports as $project => $languages) {
        $batch = $this->localeFetch
          ->buildUpdateBatch([
          $project,
        ], $languages, $options);
        foreach ($batch['operations'] as $item) {
          $queue->createItem($item);
        }
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.