From 508dbbb71ed46ab92a8d7830fb880291666e2439 Mon Sep 17 00:00:00 2001 From: kiamlaluno Date: Mon, 24 Jul 2023 00:22:19 +0200 Subject: [PATCH 1/2] Issue #43: Do not use a configuration value when a state value should be used --- cron_example/cron_example.module | 8 ++++---- cron_example/cron_example.test | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cron_example/cron_example.module b/cron_example/cron_example.module index ddc5462..2a52eac 100644 --- a/cron_example/cron_example.module +++ b/cron_example/cron_example.module @@ -139,7 +139,7 @@ function cron_example_form($form, &$form_state) { */ function cron_example_form_cron_run_submit($form, &$form_state) { if (!empty($form_state['values']['cron_reset'])) { - config_set('cron_example.settings', 'cron_example_next_execution', 0); + state_set('cron_example_next_execution', 0); } // We don't usually use globals in this way. This is used here only to @@ -185,7 +185,7 @@ function cron_example_cron() { $interval = config_get('cron_example.settings', 'cron_example_interval'); // We usually don't want to act every time cron runs (which could be every // minute) so keep a time for the next run in a variable. - if (time() >= config_get('cron_example.settings', 'cron_example_next_execution')) { + if (time() >= state_get('cron_example_next_execution', 0)) { // This is a silly example of a cron job. // It just makes it obvious that the job has run without // making any changes to your database. @@ -193,7 +193,7 @@ function cron_example_cron() { if (!empty($GLOBALS['cron_example_show_status_message'])) { backdrop_set_message(t('cron_example executed at %time', array('%time' => cron_example_date_iso8601(time(0))))); } - config_set('cron_example.settings', 'cron_example_next_execution', time() + $interval); + state_set('cron_example_next_execution', time() + $interval); } } @@ -276,7 +276,7 @@ function cron_example_queue_report_work($worker, $item) { function cron_example_date_iso8601($date) { // The DATE_ISO8601 constant cannot be used here because it does not match // date('c') and produces invalid RDF markup. - return date('c', $date); + return date('c', $date); } /** * @} End of "defgroup cron_example". diff --git a/cron_example/cron_example.test b/cron_example/cron_example.test index 4d3ff87..47179c6 100644 --- a/cron_example/cron_example.test +++ b/cron_example/cron_example.test @@ -29,7 +29,7 @@ class CronExampleTestCase extends BackdropWebTestCase { public function testCronExampleBasic() { // Pretend that cron has never been run (even though simpletest seems to // run it once...) - config_set('cron_example.settings', 'cron_example_next_execution', 0); + state_set('cron_example_next_execution', 0); $this->backdropGet('examples/cron_example'); // Initial run should cause cron_example_cron() to fire. From e4398a1121137a38deca5230b2349157c52d9f52 Mon Sep 17 00:00:00 2001 From: kiamlaluno Date: Mon, 24 Jul 2023 00:38:16 +0200 Subject: [PATCH 2/2] Removed the code setting the configuration value that is now a state value --- cron_example/cron_example.install | 1 - 1 file changed, 1 deletion(-) diff --git a/cron_example/cron_example.install b/cron_example/cron_example.install index e61a087..ac05909 100644 --- a/cron_example/cron_example.install +++ b/cron_example/cron_example.install @@ -9,7 +9,6 @@ */ function cron_example_install() { $config = config('cron_example.settings'); - $config->set('cron_example_next_execution', time()); $config->set('cron_example_interval', 60*60); $config->save(); }