public function CronExampleForm::cronRun in Examples for Developers 8
Same name and namespace in other branches
- 3.x modules/cron_example/src/Form/CronExampleForm.php \Drupal\cron_example\Form\CronExampleForm::cronRun()
 
Allow user to directly execute cron, optionally forcing it.
File
- cron_example/
src/ Form/ CronExampleForm.php, line 191  
Class
- CronExampleForm
 - Form with examples on how to use cron.
 
Namespace
Drupal\cron_example\FormCode
public function cronRun(array &$form, FormStateInterface &$form_state) {
  $cron_reset = $form_state
    ->getValue('cron_reset');
  if (!empty($cron_reset)) {
    $this->state
      ->set('cron_example.next_execution', 0);
  }
  // Use a state variable to signal that cron was run manually from this form.
  $this->state
    ->set('cron_example_show_status_message', TRUE);
  if ($this->cron
    ->run()) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('Cron ran successfully.'));
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('Cron run failed.'));
  }
}