Skip to content

Commit b83de41

Browse files
thechancegpaul-m
authored andcommitted
Issue #2667956 by thechanceg, Mile23, navneet0693, joachim, marvil07, justAChris: Set default config values during install
1 parent d734deb commit b83de41

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Default form values for \Drupal\cron_example\Form\CronExampleForm.
2+
# @see examples/cron_example/config/schema/cron_example.schema.yml
3+
# @see \Drupal\cron_example\Form\CronExampleForm::getEditableConfigNames()
4+
interval: 300
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Set configuration defaults. This schema describes the cron_example.settings
2+
# config. The defaults for this config are set in
3+
# config/install/cron_example.settings.yml and then used by
4+
# \Drupal\cron_example\Form\CronExampleForm.
5+
# @see https://www.drupal.org/node/1905070
6+
cron_example.settings:
7+
type: config_object
8+
label: 'Cron Example settings'
9+
mapping:
10+
interval:
11+
type: integer
12+
label: 'Period between cron runs'

cron_example/cron_example.module

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
*/
2929
function cron_example_cron() {
3030
// We access our configuration.
31-
$cron_config = \Drupal::configFactory()->getEditable('examples.cron');
31+
$cron_config = \Drupal::configFactory()->getEditable('cron_example.settings');
3232
// Default to an hourly interval. Of course, cron has to be running at least
3333
// hourly for this to work.
3434
$interval = $cron_config->get('interval');

cron_example/src/Form/CronExampleForm.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function getFormId() {
8080
* {@inheritdoc}
8181
*/
8282
public function buildForm(array $form, FormStateInterface $form_state) {
83-
$config = $this->configFactory->get('examples.cron');
83+
$config = $this->configFactory->get('cron_example.settings');
8484

8585
$form['status'] = [
8686
'#type' => 'details',
@@ -187,7 +187,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
187187
* Allow user to directly execute cron, optionally forcing it.
188188
*/
189189
public function cronRun(array &$form, FormStateInterface &$form_state) {
190-
$config = $this->configFactory->getEditable('examples.cron');
190+
$config = $this->configFactory->getEditable('cron_example.settings');
191191

192192
$cron_reset = $form_state->getValue('cron_reset');
193193
if (!empty($cron_reset)) {
@@ -239,7 +239,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
239239
// Update the interval as stored in configuration. This will be read when
240240
// this modules hook_cron function fires and will be used to ensure that
241241
// action is taken only after the appropiate time has elapsed.
242-
$this->configFactory->getEditable('examples.cron')
242+
$this->configFactory->getEditable('cron_example.settings')
243243
->set('interval', $form_state->getValue('cron_example_interval'))
244244
->save();
245245

@@ -250,7 +250,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
250250
* {@inheritdoc}
251251
*/
252252
protected function getEditableConfigNames() {
253-
return ['examples.cron'];
253+
return ['cron_example.settings'];
254254
}
255255

256256
}

cron_example/tests/src/Functional/CronExampleTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class CronExampleTest extends ExamplesBrowserTestBase {
1616

1717
/**
18-
* An editable config object for access to 'examples.cron'.
18+
* An editable config object for access to 'cron_example.settings'.
1919
*
2020
* @var \Drupal\Core\Config\Config
2121
*/
@@ -37,7 +37,7 @@ public function setUp() {
3737
// be shown.
3838
$this->drupalLogin($this->drupalCreateUser(['administer site configuration', 'access content']));
3939

40-
$this->cronConfig = \Drupal::configFactory()->getEditable('examples.cron');
40+
$this->cronConfig = \Drupal::configFactory()->getEditable('cron_example.settings');
4141
}
4242

4343
/**

0 commit comments

Comments
 (0)