Add fields to user from a custom module I'm building a small custom module and I should somehow extend default user entity by adding about 20 more fields to it.

I located User class definitions from Drupal folder (\drupal-8.1.8\core\modules\user\src\Entity\User.php). There I can see field definitions but that's not right place to do modification for sure :)

Also, I suppose I should add User module as a dependency to my custom module, even though user module is part of the core?

I've done some research and I think I should implement a plugin, but I don't know how exactly and what plugin interface to implement etc.
Base fields

You can add base fields to the user entity in a hook:

use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
/**
 * Implements hook_entity_base_field_info().
 */
function mymodule_entity_base_field_info(EntityTypeInterface $entity_type) {
  if ($entity_type-&gt;id() === 'user') {
    $fields['field1'] = BaseFieldDefinition::create('text')
      -&gt;setLabel(t('New field 1'))
      -&gt;setTranslatable(TRUE);
    return $fields;
  }
}


Bundle fields

But most times you probably want to use bundle fields, which can be added and modified in UI and can be easily deployed through configuration. In D8 the user entity is fieldable like nodes.

See this tutorial:

Programming custom fields into your content type

You can apply the same procedure to users. Replace node with user, also the content type with user, because the user entity has no bundles and then you use the entity name as replacement.

In a standard install user_picture is the only user field configured out-of-the-box, which you can use as an example: 

field.field.user.user.user_picture.yml


You can see that user is twice in the config name, for the entity type and the bundle name.

How to add the fields

The easiest way to implement this in a custom module is to add the fields in UI

/admin/config/people/accounts/fields


and then export the configured fields:

/admin/config/development/configuration


If you export the complete configuration before and after you've added the fields you can make a diff to catch all new yaml files.

Put the new configuration files in /config/install of your module. Remove the uuid line from the exported yaml files.