You are here

public static function EntityTest::baseFieldDefinitions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php \Drupal\entity_test\Entity\EntityTest::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 ContentEntityBase::baseFieldDefinitions

See also

\Drupal\Core\Entity\EntityFieldManagerInterface::getFieldDefinitions()

\Drupal\Core\Entity\FieldableEntityInterface::bundleFieldDefinitions()

15 calls to EntityTest::baseFieldDefinitions()
CommentTestBaseField::baseFieldDefinitions in core/modules/comment/tests/modules/comment_base_field_test/src/Entity/CommentTestBaseField.php
Provides base field definitions for an entity type.
EntitySerializedField::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntitySerializedField.php
Provides base field definitions for an entity type.
EntityTestBaseFieldDisplay::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php
Provides base field definitions for an entity type.
EntityTestCompositeConstraint::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestCompositeConstraint.php
Provides base field definitions for an entity type.
EntityTestComputedField::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php
Provides base field definitions for an entity type.

... See full list

14 methods override EntityTest::baseFieldDefinitions()
CommentTestBaseField::baseFieldDefinitions in core/modules/comment/tests/modules/comment_base_field_test/src/Entity/CommentTestBaseField.php
Provides base field definitions for an entity type.
EntitySerializedField::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntitySerializedField.php
Provides base field definitions for an entity type.
EntityTestBaseFieldDisplay::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestBaseFieldDisplay.php
Provides base field definitions for an entity type.
EntityTestCompositeConstraint::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestCompositeConstraint.php
Provides base field definitions for an entity type.
EntityTestComputedField::baseFieldDefinitions in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestComputedField.php
Provides base field definitions for an entity type.

... See full list

File

core/modules/system/tests/modules/entity_test/src/Entity/EntityTest.php, line 69

Class

EntityTest
Defines the test entity class.

Namespace

Drupal\entity_test\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  $fields = parent::baseFieldDefinitions($entity_type);
  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(t('Name'))
    ->setDescription(t('The name of the test entity.'))
    ->setTranslatable(TRUE)
    ->setSetting('max_length', 32)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -5,
  ]);
  $fields['created'] = BaseFieldDefinition::create('created')
    ->setLabel(t('Authored on'))
    ->setDescription(t('Time the entity was created'))
    ->setTranslatable(TRUE);
  $fields['user_id'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('User ID'))
    ->setDescription(t('The ID of the associated user.'))
    ->setSetting('target_type', 'user')
    ->setSetting('handler', 'default')
    ->setDefaultValue([
    0 => [
      'target_id' => 1,
    ],
  ])
    ->setTranslatable(TRUE)
    ->setDisplayOptions('form', [
    'type' => 'entity_reference_autocomplete',
    'weight' => -1,
    'settings' => [
      'match_operator' => 'CONTAINS',
      'size' => '60',
      'placeholder' => '',
    ],
  ]);
  return $fields + \Drupal::state()
    ->get($entity_type
    ->id() . '.additional_base_field_definitions', []);
}