You are here

public static function EntityTestUpdate::baseFieldDefinitions in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/entity_test_update/src/Entity/EntityTestUpdate.php \Drupal\entity_test_update\Entity\EntityTestUpdate::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()

1 call to EntityTestUpdate::baseFieldDefinitions()
EntityDefinitionUpdateTest::testLongNameFieldIndexes in core/tests/Drupal/KernelTests/Core/Entity/EntityDefinitionUpdateTest.php
Check that field schema is correctly handled with long-named fields.

File

core/modules/system/tests/modules/entity_test_update/src/Entity/EntityTestUpdate.php, line 53

Class

EntityTestUpdate
Defines the test entity class for testing definition and schema updates.

Namespace

Drupal\entity_test_update\Entity

Code

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {

  // This entity type is used for generating database dumps from Drupal
  // 8.0.0-rc1, which didn't have the entity key base fields defined in
  // the parent class (ContentEntityBase), so we have to duplicate them here.
  $fields[$entity_type
    ->getKey('id')] = BaseFieldDefinition::create('integer')
    ->setLabel(new TranslatableMarkup('ID'))
    ->setDescription(new TranslatableMarkup('The ID of the test entity.'))
    ->setReadOnly(TRUE)
    ->setSetting('unsigned', TRUE);
  $fields[$entity_type
    ->getKey('uuid')] = BaseFieldDefinition::create('uuid')
    ->setLabel(new TranslatableMarkup('UUID'))
    ->setDescription(new TranslatableMarkup('The UUID of the test entity.'))
    ->setReadOnly(TRUE);
  $fields[$entity_type
    ->getKey('bundle')] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Type'))
    ->setDescription(new TranslatableMarkup('The bundle of the test entity.'))
    ->setRequired(TRUE);
  if ($entity_type
    ->hasKey('revision')) {
    $fields[$entity_type
      ->getKey('revision')] = BaseFieldDefinition::create('integer')
      ->setLabel(new TranslatableMarkup('Revision ID'))
      ->setReadOnly(TRUE)
      ->setSetting('unsigned', TRUE);
  }
  $fields[$entity_type
    ->getKey('langcode')] = BaseFieldDefinition::create('language')
    ->setLabel(new TranslatableMarkup('Language'));
  if ($entity_type
    ->isRevisionable()) {
    $fields[$entity_type
      ->getKey('langcode')]
      ->setRevisionable(TRUE);
  }
  if ($entity_type
    ->isTranslatable()) {
    $fields[$entity_type
      ->getKey('langcode')]
      ->setTranslatable(TRUE);
  }
  $fields['name'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Name'))
    ->setDescription(new TranslatableMarkup('The name of the test entity.'))
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE)
    ->setSetting('max_length', 32)
    ->setDisplayOptions('view', [
    'label' => 'hidden',
    'type' => 'string',
    'weight' => -5,
  ])
    ->setDisplayOptions('form', [
    'type' => 'string_textfield',
    'weight' => -5,
  ]);
  $fields['test_single_property'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Field with a single property'))
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE);
  $fields['test_multiple_properties'] = BaseFieldDefinition::create('multi_value_test')
    ->setLabel(new TranslatableMarkup('Field with multiple properties'))
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE);
  $fields['test_single_property_multiple_values'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Field with a single property and multiple values'))
    ->setCardinality(2)
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE);
  $fields['test_multiple_properties_multiple_values'] = BaseFieldDefinition::create('multi_value_test')
    ->setLabel(new TranslatableMarkup('Field with multiple properties and multiple values'))
    ->setCardinality(2)
    ->setTranslatable(TRUE)
    ->setRevisionable(TRUE);
  $fields['test_non_rev_field'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Non Revisionable Field'))
    ->setDescription(new TranslatableMarkup('A non-revisionable test field.'))
    ->setCardinality(1)
    ->setRevisionable(FALSE)
    ->setTranslatable(TRUE);
  $fields['test_non_mul_field'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Non Translatable Field'))
    ->setDescription(new TranslatableMarkup('A non-translatable test field.'))
    ->setCardinality(1)
    ->setRevisionable(TRUE)
    ->setTranslatable(FALSE);
  $fields['test_non_mulrev_field'] = BaseFieldDefinition::create('string')
    ->setLabel(new TranslatableMarkup('Non Revisionable and Translatable Field'))
    ->setDescription(new TranslatableMarkup('A non-revisionable and non-translatable test field.'))
    ->setCardinality(1)
    ->setRevisionable(FALSE)
    ->setTranslatable(FALSE);
  $fields += \Drupal::state()
    ->get('entity_test_update.additional_base_field_definitions', []);
  return $fields;
}