function CronExampleForm::buildForm

Same name in other branches
  1. 3.x modules/cron_example/src/Form/CronExampleForm.php \Drupal\cron_example\Form\CronExampleForm::buildForm()

Overrides ConfigFormBase::buildForm

File

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

Class

CronExampleForm
Form with examples on how to use cron.

Namespace

Drupal\cron_example\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('cron_example.settings');
    $form['status'] = [
        '#type' => 'details',
        '#title' => $this->t('Cron status information'),
        '#open' => TRUE,
    ];
    $form['status']['intro'] = [
        '#type' => 'item',
        '#markup' => $this->t('The cron example demonstrates hook_cron() and hook_queue_info() processing. If you have administrative privileges you can run cron from this page and see the results.'),
    ];
    $next_execution = $this->state
        ->get('cron_example.next_execution');
    $request_time = $this->time
        ->getRequestTime();
    $next_execution = !empty($next_execution) ? $next_execution : $request_time;
    $args = [
        '%time' => date('c', $this->state
            ->get('cron_example.next_execution')),
        '%seconds' => $next_execution - $request_time,
    ];
    $form['status']['last'] = [
        '#type' => 'item',
        '#markup' => $this->t('cron_example_cron() will next execute the first time cron runs after %time (%seconds seconds from now)', $args),
    ];
    if ($this->currentUser
        ->hasPermission('administer site configuration')) {
        $form['cron_run'] = [
            '#type' => 'details',
            '#title' => $this->t('Run cron manually'),
            '#open' => TRUE,
        ];
        $form['cron_run']['cron_reset'] = [
            '#type' => 'checkbox',
            '#title' => $this->t("Run cron_example's cron regardless of whether interval has expired."),
            '#default_value' => FALSE,
        ];
        $form['cron_run']['cron_trigger']['actions'] = [
            '#type' => 'actions',
        ];
        $form['cron_run']['cron_trigger']['actions']['sumbit'] = [
            '#type' => 'submit',
            '#value' => $this->t('Run cron now'),
            '#submit' => [
                [
                    $this,
                    'cronRun',
                ],
            ],
        ];
    }
    $form['cron_queue_setup'] = [
        '#type' => 'details',
        '#title' => $this->t('Cron queue setup (for hook_cron_queue_info(), etc.)'),
        '#open' => TRUE,
    ];
    $queue_1 = $this->queue
        ->get('cron_example_queue_1');
    $queue_2 = $this->queue
        ->get('cron_example_queue_2');
    $args = [
        '%queue_1' => $queue_1->numberOfItems(),
        '%queue_2' => $queue_2->numberOfItems(),
    ];
    $form['cron_queue_setup']['current_cron_queue_status'] = [
        '#type' => 'item',
        '#markup' => $this->t('There are currently %queue_1 items in queue 1 and %queue_2 items in queue 2', $args),
    ];
    $form['cron_queue_setup']['num_items'] = [
        '#type' => 'select',
        '#title' => $this->t('Number of items to add to queue'),
        '#options' => array_combine([
            1,
            5,
            10,
            100,
            1000,
        ], [
            1,
            5,
            10,
            100,
            1000,
        ]),
        '#default_value' => 5,
    ];
    $form['cron_queue_setup']['queue'] = [
        '#type' => 'radios',
        '#title' => $this->t('Queue to add items to'),
        '#options' => [
            'cron_example_queue_1' => $this->t('Queue 1'),
            'cron_example_queue_2' => $this->t('Queue 2'),
        ],
        '#default_value' => 'cron_example_queue_1',
    ];
    $form['cron_queue_setup']['actions'] = [
        '#type' => 'actions',
    ];
    $form['cron_queue_setup']['actions']['submit'] = [
        '#type' => 'submit',
        '#value' => $this->t('Add jobs to queue'),
        '#submit' => [
            [
                $this,
                'addItems',
            ],
        ],
    ];
    $form['configuration'] = [
        '#type' => 'details',
        '#title' => $this->t('Configuration of cron_example_cron()'),
        '#open' => TRUE,
    ];
    $form['configuration']['cron_example_interval'] = [
        '#type' => 'select',
        '#title' => $this->t('Cron interval'),
        '#description' => $this->t('Time after which cron_example_cron will respond to a processing request.'),
        '#default_value' => $config->get('interval'),
        '#options' => [
            60 => $this->t('1 minute'),
            300 => $this->t('5 minutes'),
            3600 => $this->t('1 hour'),
            86400 => $this->t('1 day'),
        ],
    ];
    return parent::buildForm($form, $form_state);
}