User::baseFieldDefinitions

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:

$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

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
    // https://www.drupal.org/node/2318605.
    ->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;
}
doc_Drupal
2016-10-29 09:52:04
Comments
Leave a Comment

Please login to continue.