Create database fields to an existing table when installing a module I'm new to drupal and I need some help.

I created a new module with name user_extra. Which create necessery table when Installing the module.

But I also need to insert some database field in 'users_field_data' table ('user_extra_id' and 'user_office')

In baseFieldDefinitions I wrote all necessery database table fields for user_extra table.

 public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    // Standard field, used as a unique if primary index.
    $fields['id'] = BaseFieldDefinition::create('integer')
        -&gt;setLabel(t('Unit ID'))
        -&gt;setDescription(t('The unit ID.'))
        -&gt;setReadOnly(TRUE)
        -&gt;setSetting('unsigned', TRUE);

    $fields['uuid'] = BaseFieldDefinition::create('uuid')
        -&gt;setLabel(t('UUID'))
        -&gt;setDescription(t('The unit UUID.'))
        -&gt;setReadOnly(TRUE);
        .
        .
        .
    $fields['user_info'] = BaseFieldDefinition::create('string')
        -&gt;setLabel(t('User Info'))
        -&gt;setDescription(t("The User Info"))
        -&gt;setSetting('max_length', 255)
        -&gt;setDisplayOptions('form', array(
          'type' =&gt; 'string_textfield',
          'weight' =&gt; 10,
        ))
        -&gt;setDisplayConfigurable('form', TRUE);
     return $fields;
     }


When I install the module it inserts all values that I define in baseFieldDefinitions.

But I also need to create 'user_extra_id' and 'user_office' in 'users_field_data' table with setDisplayOptions and setDisplayConfigurable options.

This is the commenting that I made in entity file

/**
 * @ContentEntityType(
 *   id = "user_extra",
 *   label = @Translation("user extra"),
 *   handlers = {
 *     "storage" = "Drupal\user_extra\UserextraStorage",
 *     "storage_schema" = "Drupal\user_extra\UserextraStorageSchema",
 *     "access" = "Drupal\user_extra\UserextraAccessControlHandler",
 *     "list_builder" = "Drupal\user_extra\UserextraListBuilder",
 *     "view_builder" = "Drupal\user_extra\UserextraViewBuilder",
 *     "views_data" = "Drupal\user_extra\UserextraViewsData",
 *     "form" = {
 *       "default" = "Drupal\user_extra\Form\UserextraForm",
 *       "add" = "Drupal\user_extra\Form\UserextraAddForm",
 *       "edit" = "Drupal\user_extra\Form\UserextraEditForm",
 *       "delete" = "Drupal\user_extra\Form\UserextraDeleteForm",
 *     }
 *   },
 *   admin_permission = "administer user_extra",
 *   base_table = "user_extra",
 *   entity_keys = {
 *     "id" = "id",
 *     "label" = "name",
 *     "uuid" = "uuid",
 *   },
 *   links = {
 *     "canonical" = "/admin/user_extra/{user_extra}",
 *     "add-form" = "/admin/user_extra/add",
 *     "delete-form" = "/admin/user_extra/{user_extra}/delete",
 *     "overview-form" = "/admin/user_extra/${user_extra}/overview",
 *     "edit-form" = "/admin/user_extra/{user_extra}",
 *     "collection" = "/admin/user_extra",
 *     "children" = "/admin/user_extra/{user_extra}/children",
 *   },
 * )
 */
There are a couple of inconsistencies with the question and code so I am not sure whether or not this can be accepted as an answer to the strict question "How do I create database fields to an existing table when installing a module".


"user_extra" is defined as a new content entity. What this means is that it is a fundamental new thing that is not related to a Drupal User entity nor the User entity's storage. This might be a similar use case to Profile2.
Modifying base field definition is probably a bit too heavy/complex for adding in the "user_office" and "user_extra_id" data that your site needs. The "Drupal Way" would be to add a "Bundle Field" to the entity.


This could be done as a site builder in the user interface (See Configuration > People), programmatically or providing YAML in config/install (Berdir). See field.storage.user.user_picture.yml.
Adding many fields to the User entity may have a performance impact, which could be solved by using a module such as Profile2 to only load that information on certain pages.