function User::baseFieldDefinitions

Same name in other branches
  1. 9 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::baseFieldDefinitions()
  2. 8.9.x core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::baseFieldDefinitions()
  3. 10 core/modules/user/src/Entity/User.php \Drupal\user\Entity\User::baseFieldDefinitions()

Overrides ContentEntityBase::baseFieldDefinitions

File

core/modules/user/src/Entity/User.php, line 457

Class

User
Defines the user entity class.

Namespace

Drupal\user\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
    
    /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
    $fields = parent::baseFieldDefinitions($entity_type);
    $fields['uid']->setLabel(t('User ID'))
        ->setDescription(t('The user ID.'));
    $fields['uuid']->setDescription(t('The user UUID.'));
    $fields['langcode']->setLabel(t('Language code'))
        ->setDescription(t('The user language code.'))
        ->setDisplayOptions('form', [
        'region' => 'hidden',
    ]);
    $fields['preferred_langcode'] = BaseFieldDefinition::create('language')->setLabel(t('Preferred language code'))
        ->setDescription(t("The user's preferred language code for receiving emails and viewing the site."))
        ->addPropertyConstraints('value', [
        'AllowedValues' => [
            'callback' => __CLASS__ . '::getAllowedConfigurableLanguageCodes',
        ],
    ]);
    $fields['preferred_admin_langcode'] = BaseFieldDefinition::create('language')->setLabel(t('Preferred admin language code'))
        ->setDescription(t("The user's preferred language code for viewing administration pages."))
        ->setDefaultValue([
        0 => [
            'value' => NULL,
        ],
    ])
        ->addPropertyConstraints('value', [
        'AllowedValues' => [
            'callback' => __CLASS__ . '::getAllowedConfigurableLanguageCodes',
        ],
    ]);
    // The name should not vary per language. The username is the visual
    // identifier for a user and needs to be consistent in all languages.
    $fields['name'] = BaseFieldDefinition::create('string')->setLabel(t('Name'))
        ->setDescription(t('The name of this user.'))
        ->setRequired(TRUE)
        ->setConstraints([
        // No Length constraint here because the UserName constraint also covers
        // that.
'UserName' => [],
        'UserNameUnique' => [],
    ]);
    $fields['name']->getItemDefinition()
        ->setClass('\\Drupal\\user\\UserNameItem');
    $fields['pass'] = BaseFieldDefinition::create('password')->setLabel(t('Password'))
        ->setDescription(t('The password of this user (hashed).'))
        ->addConstraint('ProtectedUserField');
    $fields['mail'] = BaseFieldDefinition::create('email')->setLabel(t('Email'))
        ->setDescription(t('The email of this user.'))
        ->setDefaultValue('')
        ->addConstraint('UserMailUnique')
        ->addConstraint('UserMailRequired')
        ->addConstraint('ProtectedUserField');
    $fields['timezone'] = BaseFieldDefinition::create('string')->setLabel(t('Timezone'))
        ->setDescription(t('The timezone of this user.'))
        ->setSetting('max_length', 32)
        ->addPropertyConstraints('value', [
        'AllowedValues' => [
            'callback' => __CLASS__ . '::getAllowedTimezones',
        ],
    ]);
    $fields['timezone']->getItemDefinition()
        ->setClass(TimeZoneItem::class);
    $fields['status'] = BaseFieldDefinition::create('boolean')->setLabel(t('User status'))
        ->setDescription(t('Whether the user is active or blocked.'))
        ->setDefaultValue(FALSE);
    $fields['status']->getItemDefinition()
        ->setClass(StatusItem::class);
    $fields['created'] = BaseFieldDefinition::create('created')->setLabel(t('Created'))
        ->setDescription(t('The time that the user was created.'));
    $fields['changed'] = BaseFieldDefinition::create('changed')->setLabel(t('Changed'))
        ->setDescription(t('The time that the user was last edited.'))
        ->setTranslatable(TRUE);
    $fields['access'] = BaseFieldDefinition::create('timestamp')->setLabel(t('Last access'))
        ->setDescription(t('The time that the user last accessed the site.'))
        ->setDefaultValue(0);
    $fields['login'] = BaseFieldDefinition::create('timestamp')->setLabel(t('Last login'))
        ->setDescription(t('The time that the user last logged in.'))
        ->setDefaultValue(0);
    $fields['init'] = BaseFieldDefinition::create('email')->setLabel(t('Initial email'))
        ->setDescription(t('The email address used for initial account creation.'))
        ->setDefaultValue('');
    $fields['roles'] = BaseFieldDefinition::create('entity_reference')->setLabel(t('Roles'))
        ->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
        ->setDescription(t('The roles the user has.'))
        ->setSetting('target_type', 'user_role');
    return $fields;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.