Alternative ways to programmatically add user fields? Currently i use this to programmatically add user fields:

function openid_connect_entity_base_field_info(EntityTypeInterface $entity_type) {
    if ($entity_type-&gt;id() === 'user') {
        $fields['field_family_name'] = BaseFieldDefinition::create('string')
          -&gt;setLabel(t('Family Name'))
          -&gt;setDescription(t('Add a description'))
          -&gt;setRevisionable(TRUE)
          -&gt;setTranslatable(TRUE)
          -&gt;setDisplayOptions('form', array(
            'type' =&gt; 'string_text_field',
            'weight' =&gt; 30,
            'default_value' =&gt; 0,
          ))
          -&gt;setDisplayConfigurable('form', TRUE);
      }
return $fields;
}


but it results in the admin of the site unable to uninstall my module as shown:


Are there any alternative ways to programmatically add user fields without causing the admin of the website to be unable to uninstall the module that programmatically added the user fields?
I found a solution using .yml files in your modules config/install directory.

1. Create the user field 

go to Configuration -> People -> Account Settings -> Manage fields and press "+ Add field". I'd recommend giving the field a unique machine-readable name to prevent conflicting user fields.



Enable the user field in Manage Form Display and Manage Display if you want it to appear in the users edit profile and registration page.

2.Exporting the user field configuration

go to Configuration -> Development -> Configuration Synchronization -> Export -> Single Item

Once you get to a screen similar to the one below, set configuration type to "Field" then set the configuration name to the field you have created.

In your modules Config/install folder(create one if needed) create a new file named after filename highlighted in red below.

Finally Copy the yml text into the file you just created.



then set configuration type to "Field Storage" then set the configuration name to the user field you have created.

In your modules Config/install folder(create one if needed) create a new file named after filename highlighted in red below (don't copy the one in the example your user field will have a different name).

Finally Copy the yml text into the file you just created.



YOU NEED BOTH YML FILES TO PROGRAMMATICALLY CREATE A USER FIELD

I recommend doing this for each user field you want to programmatically add rather than copying the configuration and changing a few properties.

With this method you can uninstall the module that created these user fields.