Add values to 'config' table and 'key_value' table when creating a new field in an existing table in Drupal 8

509 Views Asked by At

I'm new to drupal. I need to write a module that need to handle user related details.

I'm able to create the module and add the necessery database changes in 'users_field_data' table. by using db_add_field in hook_install()

db_add_field('users_field_data', 'user_office', array(
            'type' => 'varchar',
            'length' => 255,
            'not null' => FALSE,
        ));

But one thing I noticed when installing some other module from drupal is when a new field is added to the database it saves some values to 'config' table and 'key_value'table.

Is there a way that I add the values to 'config' table and 'key_value' table when creating a new field in an existing table in drupal 8.

1

There are 1 best solutions below

0
Eyal On

Adding fields to existing entities should be done using the field and field storage configuration entities.

Take the following steps to accomplish your goals:

  1. Go to the manage fields page of the target entity (for user you can find the link in the configuration page)
  2. Add the required field in the GUI.
  3. Go to the configuration synchronization page and export the field and field storage configuration for the new field.
  4. The new files should be saved in the config/install directory of your module.
  5. Install the module
  6. The field should be added on install.