You are here

public static function User::baseFieldDefinitions in Drupal 8

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

Provides base field definitions for an entity type.

Implementations typically use the class \Drupal\Core\Field\BaseFieldDefinition for creating the field definitions; for example a 'name' field could be defined as the following:

$fields['name'] = BaseFieldDefinition::create('string')
  ->setLabel(t('Name'));

By definition, base fields are fields that exist for every bundle. To provide definitions for fields that should only exist on some bundles, use \Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions().

The definitions returned by this function can be overridden for all bundles by hook_entity_base_field_info_alter() or overridden on a per-bundle basis via 'base_field_override' configuration entities.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type definition. Useful when a single class is used for multiple, possibly dynamic entity types.

Return value

\Drupal\Core\Field\FieldDefinitionInterface[] An array of base field definitions for the entity type, keyed by field name.

Overrides ContentEntityBase::baseFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()

File

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

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;
}