3 namespace Drupal\system\Form;
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;
16 * Configure cron settings for this site.
20 class CronForm extends FormBase {
22 use ConfigFormBaseTrait;
25 * Stores the state storage service.
27 * @var \Drupal\Core\State\StateInterface
34 * @var \Drupal\Core\CronInterface
39 * The date formatter service.
41 * @var \Drupal\Core\Datetime\DateFormatterInterface
43 protected $dateFormatter;
46 * The module handler service.
48 * @var \Drupal\Core\Extension\ModuleHandlerInterface
50 protected $moduleHandler;
53 * Constructs a CronForm object.
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
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.
66 public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, CronInterface $cron, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler) {
67 $this->state = $state;
69 $this->dateFormatter = $date_formatter;
70 $this->moduleHandler = $module_handler;
76 protected function getEditableConfigNames() {
77 return ['system.cron'];
83 public static function create(ContainerInterface $container) {
85 $container->get('config.factory'),
86 $container->get('state'),
87 $container->get('cron'),
88 $container->get('date.formatter'),
89 $container->get('module_handler')
96 public function getFormId() {
97 return 'system_cron_settings';
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>',
109 '#value' => t('Run cron'),
110 '#submit' => ['::runCron'],
112 $status = '<p>' . $this->t('Last run: %time ago.', ['%time' => $this->dateFormatter->formatTimeDiffSince($this->state->get('system.cron_last'))]) . '</p>';
114 '#markup' => $status,
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>',
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.'),
129 '#title' => t('Cron settings'),
130 '#type' => 'details',
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'),
141 $form['actions']['#type'] = 'actions';
142 $form['actions']['submit'] = [
144 '#value' => t('Save configuration'),
145 '#button_type' => 'primary',
154 public function submitForm(array &$form, FormStateInterface $form_state) {
155 $this->config('system.cron')
156 ->set('logging', $form_state->getValue('logging'))
158 $this->messenger()->addStatus(t('The configuration options have been saved.'));
162 * Form submission handler for running cron manually.
164 public function runCron(array &$form, FormStateInterface $form_state) {
165 if ($this->cron->run()) {
166 $this->messenger()->addStatus($this->t('Cron ran successfully.'));
169 $this->messenger()->addError($this->t('Cron run failed.'));