function CronExampleForm::cronRun

Same name in other branches
  1. 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

modules/cron_example/src/Form/CronExampleForm.php, line 214

Class

CronExampleForm
Form with examples on how to use cron.

Namespace

Drupal\cron_example\Form

Code

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.'));
    }
}