You are here

public function CronForm::buildForm in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/system/src/Form/CronForm.php \Drupal\system\Form\CronForm::buildForm()

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides FormInterface::buildForm

File

core/modules/system/src/Form/CronForm.php, line 104

Class

CronForm
Configure cron settings for this site.

Namespace

Drupal\system\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['description'] = [
    '#markup' => '<p>' . t('Cron takes care of running periodic tasks like checking for updates and indexing content for search.') . '</p>',
  ];
  $form['run'] = [
    '#type' => 'submit',
    '#value' => t('Run cron'),
    '#submit' => [
      '::runCron',
    ],
  ];
  $status = '<p>' . $this
    ->t('Last run: %time ago.', [
    '%time' => $this->dateFormatter
      ->formatTimeDiffSince($this->state
      ->get('system.cron_last')),
  ]) . '</p>';
  $form['status'] = [
    '#markup' => $status,
  ];
  $cron_url = Url::fromRoute('system.cron', [
    'key' => $this->state
      ->get('system.cron_key'),
  ], [
    'absolute' => TRUE,
  ])
    ->toString();
  $form['cron_url'] = [
    '#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>',
  ];
  if (!$this->moduleHandler
    ->moduleExists('automated_cron')) {
    $form['automated_cron'] = [
      '#markup' => $this
        ->t('Enable the <em>Automated Cron</em> module to allow cron execution at the end of a server response.'),
    ];
  }
  $form['cron'] = [
    '#title' => t('Cron settings'),
    '#type' => 'details',
    '#open' => TRUE,
  ];
  $form['cron']['logging'] = [
    '#type' => 'checkbox',
    '#title' => t('Detailed cron logging'),
    '#default_value' => $this
      ->config('system.cron')
      ->get('logging'),
    '#description' => $this
      ->t('Run times of individual cron jobs will be written to watchdog'),
  ];
  $form['actions']['#type'] = 'actions';
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('Save configuration'),
    '#button_type' => 'primary',
  ];
  return $form;
}