4

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.

3

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->id() === 'user') {
    $fields['field1'] = BaseFieldDefinition::create('text')
      ->setLabel(t('New field 1'))
      ->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.

  • 1
    Be aware that if you add base fields, you can't uninstall your module after there is data in the base field. – Jonathan Sep 18 '16 at 10:24
  • Got it! Thanks for the help. However is this really the best practice? I'm about to create over 20 new fields. That's over 40 new files. For one entity. I don't have much experience on Drupal (this is my spare time project that I promised to do) but so far I've got the feeling that Drupal 8 is all about configuration files. Well I guess I'll get used to this and learn while doing :) – kivikall Sep 29 '16 at 20:36
  • 1
    @Jonathan, this was solved in D8.5, see drupal.org/node/2907785 – 4k4 Jul 12 '19 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.