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.
18 class CronForm extends FormBase {
20 use ConfigFormBaseTrait;
23 * Stores the state storage service.
25 * @var \Drupal\Core\State\StateInterface
32 * @var \Drupal\Core\CronInterface
37 * The date formatter service.
39 * @var \Drupal\Core\Datetime\DateFormatterInterface
41 protected $dateFormatter;
44 * The module handler service.
46 * @var \Drupal\Core\Extension\ModuleHandlerInterface
48 protected $moduleHandler;
51 * Constructs a CronForm object.
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
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.
64 public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, CronInterface $cron, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler) {
65 $this->state = $state;
67 $this->dateFormatter = $date_formatter;
68 $this->moduleHandler = $module_handler;
74 protected function getEditableConfigNames() {
75 return ['system.cron'];
81 public static function create(ContainerInterface $container) {
83 $container->get('config.factory'),
84 $container->get('state'),
85 $container->get('cron'),
86 $container->get('date.formatter'),
87 $container->get('module_handler')
94 public function getFormId() {
95 return 'system_cron_settings';
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>',
107 '#value' => t('Run cron'),
108 '#submit' => ['::runCron'],
110 $status = '<p>' . $this->t('Last run: %time ago.', ['%time' => $this->dateFormatter->formatTimeDiffSince($this->state->get('system.cron_last'))]) . '</p>';
112 '#markup' => $status,
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>',
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.'),
127 '#title' => t('Cron settings'),
128 '#type' => 'details',
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'),
139 $form['actions']['#type'] = 'actions';
140 $form['actions']['submit'] = [
142 '#value' => t('Save configuration'),
143 '#button_type' => 'primary',
152 public function submitForm(array &$form, FormStateInterface $form_state) {
153 $this->config('system.cron')
154 ->set('logging', $form_state->getValue('logging'))
156 drupal_set_message(t('The configuration options have been saved.'));
160 * Form submission handler for running cron manually.
162 public function runCron(array &$form, FormStateInterface $form_state) {
163 if ($this->cron->run()) {
164 drupal_set_message($this->t('Cron ran successfully.'));
167 drupal_set_message($this->t('Cron run failed.'), 'error');