public static User::baseFieldDefinitions(EntityTypeInterface $entity_type)
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:
1 2 | $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\EntityManagerInterface::getFieldDefinitions()
\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()
File
- core/modules/user/src/Entity/User.php, line 430
Class
- User
- Defines the user entity class.
Namespace
Drupal\user\Entity
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | 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' , [ 'type' => '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." )) // @todo: Define this via an options provider once // https://www.drupal.org/node/2329937 is completed. ->addPropertyConstraints( 'value' , array ( 'AllowedValues' => array ( '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." )) // @todo: A default value of NULL is ignored, so we have to specify // an empty field item structure instead. Fix this in ->setDefaultValue( array (0 => array ( 'value' => NULL))) // @todo: Define this via an options provider once // https://www.drupal.org/node/2329937 is completed. ->addPropertyConstraints( 'value' , array ( 'AllowedValues' => array ( '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( array ( // No Length constraint here because the UserName constraint also covers // that. 'UserName' => array (), 'UserNameUnique' => array (), )); $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) // @todo: Define this via an options provider once // https://www.drupal.org/node/2329937 is completed. ->addPropertyConstraints( 'value' , array ( 'AllowedValues' => array ( 'callback' => __CLASS__ . '::getAllowedTimezones' ), )); $fields [ 'status' ] = BaseFieldDefinition::create( 'boolean' ) ->setLabel(t( 'User status' )) ->setDescription(t( 'Whether the user is active or blocked.' )) ->setDefaultValue(FALSE); $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 ; } |
Please login to continue.