Saving Drupal 8 module configuration - where do we save the data?

A reader asked me: “we save the data, but where did we save it?”. He was referring to my tutorial on creating an admin form in Drupal 8.

This tutorial is great and helped me make a big step in learning to make modules. I have a question though, we save the data, but where did we save it?

This is the code in question:

   * {@inheritdoc}  
  public function submitForm(array &$form, FormStateInterface $form_state) {  
    parent::submitForm($form, $form_state);  

      ->set('welcome_message', $form_state->getValue('welcome_message'))  

The submitForm() method is responsible for saving the form data when the form is submitted. Take a look at the code inside the method:

      ->set('welcome_message', $form_state->getValue('welcome_message'))

Let’s break this down line by line:

  • $this is the admin settings form class.
  • -> is an object operator
  • config('welcome.adminsettings’): welcome.adminsettings is the name of the module's configuration.
  • ->set('welcome_message', $form_state->getValue('welcome_message’)): here it is setting ‘welcome_message’ and get the values from the form state.
  • ->save: save the form data.

And that is all you need to save config data like this. Drupal will automatically save this to the config table in the database.

If you search for the name welcome.adminsettings in the config table of your Drupal sites database, you will find the entry.

Configuration table in Drupal 8

Open the data in the config table:

a:3:{s:7:"welcome";a:1:{s:15:"welcome_message";s:19:"Welcome to the site";}s:5:"_core";a:1:{s:19:"default_config_hash";s:43:"1YWRehpQ5QLV9nBN1B6d_98e8ocmTLRyjHF16oJk_9Q";}s:15:"welcome_message";s:20:"Welcome to the site!";}

And have a look at the admin form:

Admin settings forms

You can see that this contains the very same message ("Welcome to the site!”).

Get the config value with Drush

An easy way to test the config value that is stored is to use the Drush config-get command.

drush config-get welcome.adminsettings

This will return:

  welcome_message: 'Welcome to the site'
  default_config_hash: 1YWRehpQ5QLV9nBN1B6d_98e8ocmTLRyjHF16oJk_9Q
welcome_message: 'Welcome to the site!'

As you can see, this matches the value in the form.

The admin form example is from my book, Conquer Drupal 8 Module Development, which has helped hundreds of people build their first working Drupal 8 module.

If you liked this, you'll love my book, Conquer Drupal 8 Module Development.


Will your book help get past the examples shown in Examples module? I'm stuck on file_managed and while I know in this new D8 environment I'll be "use" classes from core, I look for working examples and it frustrates me that I see file_managed used in /user/edit but I cannot find it being invoked in core/modules/User. Would like to see if your book can help to determine if/which classes I need to "use" for the problem I have.

Blair Wadman's picture

Hi Sam, I cover extending and using classes in the book for various scenarios. But I don't specifically cover file_managed (but may add that in the future!).

Well this is a bit tricky question at least for me. You see, I am developing a site in D8, where a piece of information is to be shown in block. A piece of information, mostly paragraphs and all, which might change once in a year or may be once in two.

Now the thing is, I had a thought on storing that piece of information(s), so I had three options. [I am not sure, there could be other ways as well, just not aware of them as of now.]
1. Make the information hardcoded in code file. Well, I don't want it to be hard coded so eliminated this option.
2. Make a admin configuration form for the block and save those values in config table.
3. Make a admin configuration form for the block and save those values in custom table in DB, created for storing those values and fetch them accordingly.

Now, I am confused, at my very core doubt to go with which, a storage in config table or a storage in custom table in DB ?

All in all, which is better ? Storage in default config table OR storage in custom DB table ? Which is better and WHY ?

New comments for this tutorial have been turned off.