Verified Commit a8fe3f24 authored by Dave Long's avatar Dave Long
Browse files

refactor: #3566768 Change \Drupal\system\Form\CronForm to use ConfigFormBase and use #config_target

By: claudiu.cristea
By: nicxvan
By: alexpott
parent 317ec5e3
Loading
Loading
Loading
Loading
Loading
+0 −6
Original line number Diff line number Diff line
@@ -29701,12 +29701,6 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/system/src/Form/CronForm.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\system\\\\Form\\\\CronForm\\:\\:submitForm\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/system/src/Form/CronForm.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\system\\\\Form\\\\DateFormatDeleteForm\\:\\:create\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
+7 −0
Original line number Diff line number Diff line
@@ -8,8 +8,15 @@

/**
 * Form submission handler for system_cron_settings().
 *
 * @deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. Use
 *   AutomatedCronHooks::automatedCronSettingsSubmit() instead.
 *
 * @see https://www.drupal.org/node/3566774
 * @see \Drupal\automated_cron\Hook\AutomatedCronHooks::automatedCronSettingsSubmit()
 */
function automated_cron_settings_submit(array $form, FormStateInterface $form_state): void {
  @trigger_error(__FUNCTION__ . '() is deprecated in drupal:11.4.0 and is removed from drupal:13.0.0. There is no replacement. See https://www.drupal.org/node/3566774', E_USER_DEPRECATED);
  \Drupal::configFactory()->getEditable('automated_cron.settings')
    ->set('interval', $form_state->getValue('interval'))
    ->save();
+1 −6
Original line number Diff line number Diff line
@@ -45,7 +45,6 @@ public function help($route_name, RouteMatchInterface $route_match): ?string {
   */
  #[Hook('form_system_cron_settings_alter')]
  public function formSystemCronSettingsAlter(&$form, &$form_state) : void {
    $automated_cron_settings = \Drupal::config('automated_cron.settings');
    $options = [3600, 10800, 21600, 43200, 86400, 604800];
    $form['cron']['interval'] = [
      '#type' => 'select',
@@ -53,7 +52,7 @@ public function formSystemCronSettingsAlter(&$form, &$form_state) : void {
      '#description' => $this->t('More information about setting up scheduled tasks can be found by <a href=":url">reading the cron tutorial on drupal.org</a>.', [
        ':url' => 'https://www.drupal.org/docs/8/administering-a-drupal-8-site/cron-automated-tasks',
      ]),
      '#default_value' => $automated_cron_settings->get('interval'),
      '#config_target' => 'automated_cron.settings:interval',
      '#options' => [
        0 => $this->t('Never'),
      ] + array_map([
@@ -61,10 +60,6 @@ public function formSystemCronSettingsAlter(&$form, &$form_state) : void {
        'formatInterval',
      ], array_combine($options, $options)),
    ];
    // Add submit callback.
    $form['#submit'][] = 'automated_cron_settings_submit';
    // Theme this form as a config form.
    $form['#theme'] = 'system_config_form';
  }

}
+19 −74
Original line number Diff line number Diff line
@@ -3,73 +3,34 @@
namespace Drupal\system\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\CronInterface;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\RedundantEditableConfigNamesTrait;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Form\ConfigFormBaseTrait;

/**
 * Configure cron settings for this site.
 *
 * @internal
 */
class CronForm extends FormBase {

  use ConfigFormBaseTrait;

  /**
   * Stores the state storage service.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The cron service.
   *
   * @var \Drupal\Core\CronInterface
   */
  protected $cron;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * Constructs a CronForm object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key value store.
   * @param \Drupal\Core\CronInterface $cron
   *   The cron service.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter service.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, CronInterface $cron, DateFormatterInterface $date_formatter, ModuleHandlerInterface $module_handler) {
    $this->configFactory = $config_factory;
    $this->state = $state;
    $this->cron = $cron;
    $this->dateFormatter = $date_formatter;
    $this->moduleHandler = $module_handler;
class CronForm extends ConfigFormBase {
  use RedundantEditableConfigNamesTrait;

  public function __construct(
    ConfigFactoryInterface $config_factory,
    protected StateInterface $state,
    protected CronInterface $cron,
    protected DateFormatterInterface $dateFormatter,
    protected ModuleHandlerInterface $moduleHandler,
    TypedConfigManagerInterface $typedConfigManager,
  ) {
    parent::__construct($config_factory, $typedConfigManager);
  }

  /**
@@ -88,7 +49,8 @@ public static function create(ContainerInterface $container) {
      $container->get('state'),
      $container->get('cron'),
      $container->get('date.formatter'),
      $container->get('module_handler')
      $container->get('module_handler'),
      $container->get('config.typed')
    );
  }

@@ -144,28 +106,11 @@ public function buildForm(array $form, FormStateInterface $form_state) {
    $form['cron']['logging'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Detailed cron logging'),
      '#default_value' => $this->config('system.cron')->get('logging'),
      '#config_target' => 'system.cron:logging',
      '#description' => $this->t('Run times of individual cron jobs will be written to watchdog'),
    ];

    $form['actions']['#type'] = 'actions';
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Save configuration'),
      '#button_type' => 'primary',
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('system.cron')
      ->set('logging', $form_state->getValue('logging'))
      ->save();
    $this->messenger()->addStatus($this->t('The configuration options have been saved.'));
    return parent::buildForm($form, $form_state);
  }

  /**