0

I can see the form, using the Config API, and submit it. However, it doesn't save the changes. Here is my code:

    <?php

namespace Drupal\my_config\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;


class MyConfigForm extends ConfigFormBase {


  public function getFormId () {
    return 'my_config_settings';
  }

  protected function getEditableConfigNames() {
    return [
      'my_config.config',
    ];
  }


  public function buildForm (array $form, FormStateInterface $form_state) {


    $form['author_config'] = array(
      '#type' => 'textfield',
      '#default_value' => \Drupal::config('config.factory')->get('author_config'),
      '#title' => 'Author of Config',
      '#required' => TRUE,
    );
    $form['language'] = array(
      '#type' => 'radios',
      '#title' => 'Language',
      '#options' => array(
        'EN' => t('English'),
        'SP' => t('Spanish'),
        'FR' => t('French'),
        'DE' => t('German'),
      ),
    );
    $form['drink'] = array (
      '#type' => 'select',
      '#title' => 'Select your drink',
      '#options' => array(
        'RB' => t('Root Beer'),
        'SD' => t('Sunny Delight'),
        'WR' => t('Water'),
        'OJ' => t('Orange Juice'),
      ),
    );
    return parent::buildForm($form, $form_state);
  }


  public function submitForm (array &$form, FormStateInterface $form_state) {

    \Drupal::service('config.factory')->getEditable('my_config.config')->set('author_config', $form_state->getValue('author_config'))
     ->set('language', $form_state->getValue('language'))
         ->set('drink', $form_state->getValue('drink'))->save();
    parent::submitForm($form, $form_state);
  }

}

This is my routing file:

my_config.settings:
  path: '/my-config/config'
  defaults:
    _form: '\Drupal\my_config\Form\MyConfigForm'
    _title: 'Configuration Form'
  requirements:
    _permission: 'administer site configuration'
  • 3
    I think you're setting the values correctly, but the default values for your form elements are being retrieved incorrectly. – Jaypan Mar 12 at 21:09
  • 3
    What are you using to check that the values are(n't) saved? If it's the same form (after the save), you forgot the #default_value for the language and drink – Clive Mar 12 at 21:16
  • 3
    Also you should ideally use $this->config('...')->get() and $this->config('...')->set() rather than the global method; it shouldn't work any differently, but for the sake of DI and testability it's a simple change – Clive Mar 12 at 21:22
  • It is the same form, and I will be adding it to drink and language too. I just noticed the problem with the author_config field first. Thanks for the $this->config('...')->get tip. I'll be sure to do that in the future. – Will Mar 12 at 22:47
1

This is wrong:

'#default_value' => \Drupal::config('config.factory')->get('author_config'),

It should be:

'#default_value' => \Drupal::config('my_config.config')->get('author_config'),

This is the entire form:

$form['author_config'] = array(
  '#type' => 'textfield',
  '#default_value' => \Drupal::config('my_config.config')->get('author_config'),
  '#title' => 'Author of Config',
  '#required' => TRUE,
);
$form['language'] = array(
  '#type' => 'radios',
  '#title' => 'Language',
  '#default_value' => \Drupal::config('my_config.config')->get('language'),
  '#options' => array(
    'EN' => t('English'),
    'SP' => t('Spanish'),
    'FR' => t('French'),
    'DE' => t('German'),
  ),
);
$form['drink'] = array(
  '#type' => 'select',
  '#title' => 'Select your drink',
  '#default_value' => \Drupal::config('my_config.config')->get('drink'),
  '#options' => array(
    'RB' => t('Root Beer'),
    'SD' => t('Sunny Delight'),
    'WR' => t('Water'),
    'OJ' => t('Orange Juice'),
  ),
);

Probably using $this->config the problem will be solved too

  • So would \Drupal::service('config.factory')->getEditable('my_config.config')->set be $this->config('my_config.config')->set – Will Mar 13 at 22:06
  • Yes, and \Drupal::config('my_config.config')->get('author_config') will be $this->config('my_config.config')->get('author_config') – Nicoschi Mar 14 at 8:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.