public static function ContentEntityBase::baseFieldDefinitions in Drupal 10
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::baseFieldDefinitions()
- 9 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\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
See also
\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()
\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()
1 call to ContentEntityBase::baseFieldDefinitions()
- ViewsTestEntity::baseFieldDefinitions in core/
modules/ views/ tests/ src/ Kernel/ Entity/ EntityViewsDataTest.php - Provides base field definitions for an entity type.
10 methods override ContentEntityBase::baseFieldDefinitions()
- 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.
- EntityTestWithBundle::baseFieldDefinitions in core/
modules/ system/ tests/ modules/ entity_test/ src/ Entity/ EntityTestWithBundle.php - Provides base field definitions for an entity type.
- File::baseFieldDefinitions in core/
modules/ file/ src/ Entity/ File.php - Provides base field definitions for an entity type.
- Message::baseFieldDefinitions in core/
modules/ contact/ src/ Entity/ Message.php - Provides base field definitions for an entity type.
File
- core/
lib/ Drupal/ Core/ Entity/ ContentEntityBase.php, line 1334
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;
}