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')
        ->setLabel(t('Unit ID'))
        ->setDescription(t('The unit ID.'))
        ->setSetting('unsigned', TRUE);

    $fields['uuid'] = BaseFieldDefinition::create('uuid')
        ->setDescription(t('The unit UUID.'))
    $fields['user_info'] = BaseFieldDefinition::create('string')
        ->setLabel(t('User Info'))
        ->setDescription(t("The User Info"))
        ->setSetting('max_length', 255)
        ->setDisplayOptions('form', array(
          'type' => 'string_textfield',
          'weight' => 10,
        ->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",
 *   },
 * )

1 Answer 1


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".

  1. "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.
  2. 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.
  • Adding default config works perfectly for user, the only thing you need to take care of is configuring the widget and formatter so that it shows up in the UI. That needs to be done in hook_installl(), through the API.
    – Berdir
    Commented Dec 17, 2015 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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