You are here

public static function ContentEntityBase::baseFieldDefinitions in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Entity/ContentEntityBase.php \Drupal\Core\Entity\ContentEntityBase::baseFieldDefinitions()
  2. 10 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()

14 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.

... See full list

17 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.

... See full list

File

core/lib/Drupal/Core/Entity/ContentEntityBase.php, line 1323

Class

ContentEntityBase
Implements Entity Field API specific enhancements to the Entity class.

Namespace

Drupal\Core\Entity

Code

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