CronForm.php 3.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\system\Form\CronForm.
 */

namespace Drupal\system\Form;

use Drupal\Core\Config\ConfigFactory;
11
use Drupal\Core\CronInterface;
12
use Drupal\Core\KeyValueStore\StateInterface;
13
use Drupal\Core\Form\ConfigFormBase;
14 15 16 17 18 19
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Configure cron settings for this site.
 */
20
class CronForm extends ConfigFormBase {
21 22 23 24

  /**
   * Stores the state storage service.
   *
25
   * @var \Drupal\Core\KeyValueStore\StateInterface
26 27 28
   */
  protected $state;

29 30 31 32 33 34 35
  /**
   * The cron service.
   *
   * @var \Drupal\Core\CronInterface
   */
  protected $cron;

36 37 38 39 40
  /**
   * Constructs a CronForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The factory for configuration objects.
41
   * @param \Drupal\Core\KeyValueStore\StateInterface $state
42 43 44
   *   The state key value store.
   * @param \Drupal\Core\CronInterface $cron
   *   The cron service.
45
   */
46
  public function __construct(ConfigFactory $config_factory, StateInterface $state, CronInterface $cron) {
47
    parent::__construct($config_factory);
48
    $this->state = $state;
49
    $this->cron = $cron;
50 51 52 53 54 55 56 57
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
58 59
      $container->get('state'),
      $container->get('cron')
60 61 62 63 64 65
    );
  }

  /**
   * {@inheritdoc}
   */
66
  public function getFormId() {
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    return 'system_cron_settings';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, array &$form_state) {
    $config = $this->configFactory->get('system.cron');

    $form['description'] = array(
      '#markup' => '<p>' . t('Cron takes care of running periodic tasks like checking for updates and indexing content for search.') . '</p>',
    );
    $form['run'] = array(
      '#type' => 'submit',
      '#value' => t('Run cron'),
      '#submit' => array(array($this, 'submitCron')),
    );

    $status = '<p>' . t('Last run: %cron-last ago.', array('%cron-last' => format_interval(REQUEST_TIME - $this->state->get('system.cron_last')))) . '</p>';
    $form['status'] = array(
      '#markup' => $status,
    );

    $form['cron_url'] = array(
      '#markup' => '<p>' . t('To run cron from outside the site, go to <a href="!cron">!cron</a>', array('!cron' => url('cron/' . $this->state->get('system.cron_key'), array('absolute' => TRUE)))) . '</p>',
    );

    $form['cron'] = array(
95
      '#title' => t('Cron settings'),
96 97 98 99 100
      '#type' => 'details',
    );
    $form['cron']['cron_safe_threshold'] = array(
      '#type' => 'select',
      '#title' => t('Run cron every'),
101
      '#description' => t('More information about setting up scheduled tasks can be found by <a href="@url">reading the cron tutorial on drupal.org</a>.', array('@url' => url('http://drupal.org/cron'))),
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
      '#default_value' => $config->get('threshold.autorun'),
      '#options' => array(0 => t('Never')) + drupal_map_assoc(array(3600, 10800, 21600, 43200, 86400, 604800), 'format_interval'),
    );

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, array &$form_state) {
    $this->configFactory->get('system.cron')
      ->set('threshold.autorun', $form_state['values']['cron_safe_threshold'])
      ->save();

    parent::submitForm($form, $form_state);
  }

  /**
   * Runs cron and reloads the page.
   */
  public function submitCron(array &$form, array &$form_state) {
    // Run cron manually from Cron form.
125
    if ($this->cron->run()) {
126 127 128 129 130 131 132 133 134 135
      drupal_set_message(t('Cron run successfully.'));
    }
    else {
      drupal_set_message(t('Cron run failed.'), 'error');
    }

    return new RedirectResponse(url('admin/config/system/cron', array('absolute' => TRUE)));
  }

}