You are here

function cron_example_form in Examples for Developers 7

The form to provide a link to cron.php.

Related topics

1 string reference to 'cron_example_form'
cron_example_menu in cron_example/cron_example.module
Implements hook_menu().

File

cron_example/cron_example.module, line 37
Demonstrates use of the Cron API in Drupal - hook_cron()

Code

function cron_example_form($form, &$form_state) {
  $form['status'] = array(
    '#type' => 'fieldset',
    '#title' => t('Cron status information'),
  );
  $form['status']['intro'] = array(
    '#markup' => '<div>' . t('The cron example demonstrates hook_cron() and hook_cron_queue_info() processing. If you have administrative privileges you can run cron from this page and see the results.') . '</div>',
  );
  $form['status']['last'] = array(
    '#markup' => '<div>' . t('cron_example_cron() will next execute the first time cron runs after %time (%seconds seconds from now)', array(
      '%time' => date_iso8601(variable_get('cron_example_next_execution', time())),
      '%seconds' => variable_get('cron_example_next_execution', time()) - time(),
    )) . '</div>',
  );
  if (user_access('administer site configuration')) {
    $form['cron_run'] = array(
      '#type' => 'fieldset',
      '#title' => t('Run cron manually'),
    );
    $form['cron_run']['cron_reset'] = array(
      '#type' => 'checkbox',
      '#title' => t("Run cron_example's cron regardless of whether interval has expired."),
      '#default_value' => FALSE,
    );
    $form['cron_run']['cron_trigger'] = array(
      '#type' => 'submit',
      '#value' => t('Run cron now'),
      '#submit' => array(
        'cron_example_form_cron_run_submit',
      ),
    );
  }
  $form['cron_queue_setup'] = array(
    '#type' => 'fieldset',
    '#title' => t('Cron queue setup (for hook_cron_queue_info(), etc.)'),
  );
  $queue_1 = DrupalQueue::get('cron_example_queue_1');
  $queue_2 = DrupalQueue::get('cron_example_queue_2');
  $form['cron_queue_setup']['current_cron_queue_status'] = array(
    '#markup' => '<div>' . t('There are currently %queue_1 items in queue 1 and %queue_2 items in queue 2', array(
      '%queue_1' => $queue_1
        ->numberOfItems(),
      '%queue_2' => $queue_2
        ->numberOfItems(),
    )) . '</div>',
  );
  $form['cron_queue_setup']['num_items'] = array(
    '#type' => 'select',
    '#title' => t('Number of items to add to queue'),
    '#options' => drupal_map_assoc(array(
      1,
      5,
      10,
      100,
      1000,
    )),
    '#default_value' => 5,
  );
  $form['cron_queue_setup']['queue'] = array(
    '#type' => 'radios',
    '#title' => t('Queue to add items to'),
    '#options' => array(
      'cron_example_queue_1' => t('Queue 1'),
      'cron_example_queue_2' => t('Queue 2'),
    ),
    '#default_value' => 'cron_example_queue_1',
  );
  $form['cron_queue_setup']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Add jobs to queue'),
    '#submit' => array(
      'cron_example_add_jobs_to_queue',
    ),
  );
  $form['configuration'] = array(
    '#type' => 'fieldset',
    '#title' => t('Configuration of cron_example_cron()'),
  );
  $form['configuration']['cron_example_interval'] = array(
    '#type' => 'select',
    '#title' => t('Cron interval'),
    '#description' => t('Time after which cron_example_cron will respond to a processing request.'),
    '#default_value' => variable_get('cron_example_interval', 60 * 60),
    '#options' => array(
      60 => t('1 minute'),
      300 => t('5 minutes'),
      3600 => t('1 hour'),
      60 * 60 * 24 => t('1 day'),
    ),
  );
  return system_settings_form($form);
}