function ContentEntityBase::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 FieldableEntityInterface::baseFieldDefinitions
13 calls to ContentEntityBase::baseFieldDefinitions()
- Comment::baseFieldDefinitions in core/
modules/ comment/ src/ Entity/ Comment.php  - Provides base field definitions for an entity type.
 - ContentModerationState::baseFieldDefinitions in core/
modules/ content_moderation/ src/ Entity/ ContentModerationState.php  - Provides base field definitions for an entity type.
 - EditorialContentEntityBase::baseFieldDefinitions in core/
lib/ Drupal/ Core/ Entity/ EditorialContentEntityBase.php  - Provides base field definitions for an entity type.
 - EntityTest::baseFieldDefinitions in core/
modules/ system/ tests/ modules/ entity_test/ src/ Entity/ EntityTest.php  - Provides base field definitions for an entity type.
 - EntityTestWithBundle::baseFieldDefinitions in core/
modules/ system/ tests/ modules/ entity_test/ src/ Entity/ EntityTestWithBundle.php  - Provides base field definitions for an entity type.
 
16 methods override ContentEntityBase::baseFieldDefinitions()
- Comment::baseFieldDefinitions in core/
modules/ comment/ src/ Entity/ Comment.php  - Provides base field definitions for an entity type.
 - ContentModerationState::baseFieldDefinitions in core/
modules/ content_moderation/ src/ Entity/ ContentModerationState.php  - Provides base field definitions for an entity type.
 - EditorialContentEntityBase::baseFieldDefinitions in core/
lib/ Drupal/ Core/ Entity/ EditorialContentEntityBase.php  - Provides base field definitions for an entity type.
 - EntityTest::baseFieldDefinitions in core/
modules/ system/ tests/ modules/ entity_test/ src/ Entity/ EntityTest.php  - Provides base field definitions for an entity type.
 - EntityTestUpdate::baseFieldDefinitions in core/
modules/ system/ tests/ modules/ entity_test_update/ src/ Entity/ EntityTestUpdate.php  - Provides base field definitions for an entity type.
 
File
- 
              core/
lib/ Drupal/ Core/ Entity/ ContentEntityBase.php, line 1398  
Class
- ContentEntityBase
 - Implements Entity Field API specific enhancements to the Entity class.
 
Namespace
Drupal\Core\EntityCode
public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type->hasKey('id')) {
    $fields[$entity_type->getKey('id')] = BaseFieldDefinition::create('integer')->setLabel(new TranslatableMarkup('ID'))
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
  }
  if ($entity_type->hasKey('uuid')) {
    $fields[$entity_type->getKey('uuid')] = BaseFieldDefinition::create('uuid')->setLabel(new TranslatableMarkup('UUID'))
      ->setReadOnly(TRUE);
  }
  if ($entity_type->hasKey('revision')) {
    $fields[$entity_type->getKey('revision')] = BaseFieldDefinition::create('integer')->setLabel(new TranslatableMarkup('Revision ID'))
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
  }
  if ($entity_type->hasKey('langcode')) {
    $fields[$entity_type->getKey('langcode')] = BaseFieldDefinition::create('language')->setLabel(new TranslatableMarkup('Language'))
      ->setDisplayOptions('view', [
      'region' => 'hidden',
    ])
      ->setDisplayOptions('form', [
      'type' => 'language_select',
      'weight' => 2,
    ]);
    if ($entity_type->isRevisionable()) {
      $fields[$entity_type->getKey('langcode')]
        ->setRevisionable(TRUE);
    }
    if ($entity_type->isTranslatable()) {
      $fields[$entity_type->getKey('langcode')]
        ->setTranslatable(TRUE);
    }
  }
  if ($entity_type->hasKey('bundle')) {
    if ($bundle_entity_type_id = $entity_type->getBundleEntityType()) {
      $fields[$entity_type->getKey('bundle')] = BaseFieldDefinition::create('entity_reference')->setLabel($entity_type->getBundleLabel())
        ->setSetting('target_type', $bundle_entity_type_id)
        ->setRequired(TRUE)
        ->setReadOnly(TRUE);
    }
    else {
      $fields[$entity_type->getKey('bundle')] = BaseFieldDefinition::create('string')->setLabel($entity_type->getBundleLabel())
        ->setRequired(TRUE)
        ->setReadOnly(TRUE);
    }
  }
  return $fields;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.