a585aa57cb7c156bbb65cf0460848735b67b462b
[yaffs-website] / web / core / modules / system / src / Form / CronForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Core\Config\ConfigFactoryInterface;
6 use Drupal\Core\CronInterface;
7 use Drupal\Core\Datetime\DateFormatterInterface;
8 use Drupal\Core\Extension\ModuleHandlerInterface;
9 use Drupal\Core\Form\FormBase;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\State\StateInterface;
12 use Symfony\Component\DependencyInjection\ContainerInterface;
13 use Drupal\Core\Form\ConfigFormBaseTrait;
14
15 /**
16  * Configure cron settings for this site.
17  *
18  * @internal
19  */
20 class CronForm extends FormBase {
21
22   use ConfigFormBaseTrait;
23
24   /**
25    * Stores the state storage service.
26    *
27    * @var \Drupal\Core\State\StateInterface
28    */
29   protected $state;
30
31   /**
32    * The cron service.
33    *
34    * @var \Drupal\Core\CronInterface
35    */
36   protected $cron;
37
38   /**
39    * The date formatter service.
40    *
41    * @var \Drupal\Core\Datetime\DateFormatterInterface
42    */
43   protected $dateFormatter;
44
45   /**
46    * The module handler service.
47    *
48    * @var \Drupal\Core\Extension\ModuleHandlerInterface
49    */
50   protected $moduleHandler;
51
52   /**
53    * Constructs a CronForm object.
54    *
55    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
56    *   The factory for configuration objects.
57    * @param \Drupal\Core\State\StateInterface $state
58    *   The state key value store.
59    * @param \Drupal\Core\CronInterface $cron
60    *   The cron service.
61    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
62    *   The date formatter service.
63    * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
64    *   The module handler service.
65    */
66   public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, CronInterface $cron, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler) {
67     $this->state = $state;
68     $this->cron = $cron;
69     $this->dateFormatter = $date_formatter;
70     $this->moduleHandler = $module_handler;
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   protected function getEditableConfigNames() {
77     return ['system.cron'];
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public static function create(ContainerInterface $container) {
84     return new static(
85       $container->get('config.factory'),
86       $container->get('state'),
87       $container->get('cron'),
88       $container->get('date.formatter'),
89       $container->get('module_handler')
90     );
91   }
92
93   /**
94    * {@inheritdoc}
95    */
96   public function getFormId() {
97     return 'system_cron_settings';
98   }
99
100   /**
101    * {@inheritdoc}
102    */
103   public function buildForm(array $form, FormStateInterface $form_state) {
104     $form['description'] = [
105       '#markup' => '<p>' . t('Cron takes care of running periodic tasks like checking for updates and indexing content for search.') . '</p>',
106     ];
107     $form['run'] = [
108       '#type' => 'submit',
109       '#value' => t('Run cron'),
110       '#submit' => ['::runCron'],
111     ];
112     $status = '<p>' . $this->t('Last run: %time ago.', ['%time' => $this->dateFormatter->formatTimeDiffSince($this->state->get('system.cron_last'))]) . '</p>';
113     $form['status'] = [
114       '#markup' => $status,
115     ];
116
117     $cron_url = $this->url('system.cron', ['key' => $this->state->get('system.cron_key')], ['absolute' => TRUE]);
118     $form['cron_url'] = [
119       '#markup' => '<p>' . t('To run cron from outside the site, go to <a href=":cron" class="system-cron-settings__link">@cron</a>', [':cron' => $cron_url, '@cron' => $cron_url]) . '</p>',
120     ];
121
122     if (!$this->moduleHandler->moduleExists('automated_cron')) {
123       $form['automated_cron'] = [
124         '#markup' => $this->t('Enable the <em>Automated Cron</em> module to allow cron execution at the end of a server response.'),
125       ];
126     }
127
128     $form['cron'] = [
129       '#title' => t('Cron settings'),
130       '#type' => 'details',
131       '#open' => TRUE,
132     ];
133
134     $form['cron']['logging'] = [
135       '#type' => 'checkbox',
136       '#title' => t('Detailed cron logging'),
137       '#default_value' => $this->config('system.cron')->get('logging'),
138       '#description' => $this->t('Run times of individual cron jobs will be written to watchdog'),
139     ];
140
141     $form['actions']['#type'] = 'actions';
142     $form['actions']['submit'] = [
143       '#type' => 'submit',
144       '#value' => t('Save configuration'),
145       '#button_type' => 'primary',
146     ];
147
148     return $form;
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   public function submitForm(array &$form, FormStateInterface $form_state) {
155     $this->config('system.cron')
156       ->set('logging', $form_state->getValue('logging'))
157       ->save();
158     $this->messenger()->addStatus(t('The configuration options have been saved.'));
159   }
160
161   /**
162    * Form submission handler for running cron manually.
163    */
164   public function runCron(array &$form, FormStateInterface $form_state) {
165     if ($this->cron->run()) {
166       $this->messenger()->addStatus($this->t('Cron ran successfully.'));
167     }
168     else {
169       $this->messenger()->addError($this->t('Cron run failed.'));
170     }
171   }
172
173 }