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