3 namespace Drupal\locale\Plugin\QueueWorker;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
7 use Drupal\Core\Queue\QueueInterface;
8 use Drupal\Core\Queue\QueueWorkerBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
12 * Executes interface translation queue tasks.
15 * id = "locale_translation",
16 * title = @Translation("Update translations"),
17 * cron = {"time" = 30}
20 class LocaleTranslation extends QueueWorkerBase implements ContainerFactoryPluginInterface {
25 * @var \Drupal\Core\Extension\ModuleHandlerInterface
27 protected $moduleHandler;
32 * @var \Drupal\Core\Queue\QueueInterface
37 * Constructs a new LocaleTranslation object.
39 * @param array $configuration
40 * A configuration array containing information about the plugin instance.
41 * @param string $plugin_id
42 * The plugin_id for the plugin instance.
43 * @param array $plugin_definition
44 * The plugin implementation definition.
45 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
47 * @param \Drupal\Core\Queue\QueueInterface $queue
50 public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, QueueInterface $queue) {
51 parent::__construct($configuration, $plugin_id, $plugin_definition);
53 $this->moduleHandler = $module_handler;
54 $this->queue = $queue;
60 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
65 $container->get('module_handler'),
66 $container->get('queue')->get('locale_translation', TRUE)
73 * The translation update functions executed here are batch operations which
74 * are also used in translation update batches. The batch functions may need
75 * to be executed multiple times to complete their task, typically this is the
76 * translation import function. When a batch function is not finished, a new
77 * queue task is created and added to the end of the queue. The batch context
78 * data is needed to continue the batch task is stored in the queue with the
81 public function processItem($data) {
82 $this->moduleHandler->loadInclude('locale', 'batch.inc');
83 list($function, $args) = $data;
85 // We execute batch operation functions here to check, download and import
86 // the translation files. Batch functions use a context variable as last
87 // argument which is passed by reference. When a batch operation is called
88 // for the first time a default batch context is created. When called
89 // iterative (usually the batch import function) the batch context is passed
90 // through via the queue and is part of the $data.
91 $last = count($args) - 1;
92 if (!is_array($args[$last]) || !isset($args[$last]['finished'])) {
101 $batch_context = $args[$last];
104 $args = array_merge($args, [&$batch_context]);
106 // Call the batch operation function.
107 call_user_func_array($function, $args);
109 // If the batch operation is not finished we create a new queue task to
110 // continue the task. This is typically the translation import task.
111 if ($batch_context['finished'] < 1) {
112 unset($batch_context['strings']);
113 $this->queue->createItem([$function, $args]);