Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / locale / src / Plugin / QueueWorker / LocaleTranslation.php
1 <?php
2
3 namespace Drupal\locale\Plugin\QueueWorker;
4
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;
10
11 /**
12  * Executes interface translation queue tasks.
13  *
14  * @QueueWorker(
15  *   id = "locale_translation",
16  *   title = @Translation("Update translations"),
17  *   cron = {"time" = 30}
18  * )
19  */
20 class LocaleTranslation extends QueueWorkerBase implements ContainerFactoryPluginInterface {
21
22   /**
23    * The module handler.
24    *
25    * @var \Drupal\Core\Extension\ModuleHandlerInterface
26    */
27   protected $moduleHandler;
28
29   /**
30    * The queue object.
31    *
32    * @var \Drupal\Core\Queue\QueueInterface
33    */
34   protected $queue;
35
36   /**
37    * Constructs a new LocaleTranslation object.
38    *
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
46    *   The module handler.
47    * @param \Drupal\Core\Queue\QueueInterface $queue
48    *   The queue object.
49    */
50   public function __construct(array $configuration, $plugin_id, array $plugin_definition, ModuleHandlerInterface $module_handler, QueueInterface $queue) {
51     parent::__construct($configuration, $plugin_id, $plugin_definition);
52
53     $this->moduleHandler = $module_handler;
54     $this->queue = $queue;
55   }
56
57   /**
58    * {@inheritdoc}
59    */
60   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
61     return new static(
62       $configuration,
63       $plugin_id,
64       $plugin_definition,
65       $container->get('module_handler'),
66       $container->get('queue')->get('locale_translation', TRUE)
67     );
68   }
69
70   /**
71    * {@inheritdoc}
72    *
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
79    * queue data.
80    */
81   public function processItem($data) {
82     $this->moduleHandler->loadInclude('locale', 'batch.inc');
83     list($function, $args) = $data;
84
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'])) {
93       $batch_context = [
94         'sandbox'  => [],
95         'results'  => [],
96         'finished' => 1,
97         'message'  => '',
98       ];
99     }
100     else {
101       $batch_context = $args[$last];
102       unset ($args[$last]);
103     }
104     $args = array_merge($args, [&$batch_context]);
105
106     // Call the batch operation function.
107     call_user_func_array($function, $args);
108
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]);
114     }
115   }
116
117 }