Same name and namespace in other branches
  1. 8.9.x core/modules/system/tests/modules/entity_test/entity_test.module \entity_test_entity_base_field_info()
  2. 9 core/modules/system/tests/modules/entity_test/entity_test.module \entity_test_entity_base_field_info()

Implements hook_entity_base_field_info().

File

core/modules/system/tests/modules/entity_test/entity_test.module, line 122
Test module for the entity API providing several entity types for testing.

Code

function entity_test_entity_base_field_info(EntityTypeInterface $entity_type) {
  $fields = [];
  if ($entity_type
    ->id() === 'entity_test' && \Drupal::state()
    ->get('entity_test.internal_field')) {
    $fields['internal_string_field'] = BaseFieldDefinition::create('string')
      ->setLabel('Internal field')
      ->setInternal(TRUE);
  }
  if ($entity_type
    ->id() === 'entity_test_mul' && \Drupal::state()
    ->get('entity_test.required_default_field')) {
    $fields['required_default_field'] = BaseFieldDefinition::create('string')
      ->setLabel('Required field with default value')
      ->setRequired(TRUE)
      ->setDefaultValue('this is a default value');
  }
  if ($entity_type
    ->id() === 'entity_test_mul' && \Drupal::state()
    ->get('entity_test.required_multi_default_field')) {
    $fields['required_multi_default_field'] = BaseFieldDefinition::create('string')
      ->setLabel('Required field with default value')
      ->setRequired(TRUE)
      ->setCardinality(FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED)
      ->setDefaultValue([
      [
        'value' => 'this is the first default field item',
      ],
      [
        'value' => 'this is the second default value',
      ],
      [
        'value' => 'you get the idea...',
      ],
    ]);
  }
  if ($entity_type
    ->id() == 'entity_test_mulrev' && \Drupal::state()
    ->get('entity_test.field_test_item')) {
    $fields['field_test_item'] = BaseFieldDefinition::create('field_test')
      ->setLabel(t('Field test'))
      ->setDescription(t('A field test.'))
      ->setRevisionable(TRUE)
      ->setTranslatable(TRUE);
  }
  if ($entity_type
    ->id() == 'entity_test_mulrev' && \Drupal::state()
    ->get('entity_test.multi_column')) {
    $fields['description'] = BaseFieldDefinition::create('shape')
      ->setLabel(t('Some custom description'))
      ->setTranslatable(TRUE);
  }
  return $fields;
}