function EntityTestHooks::entityBaseFieldInfo

Implements hook_entity_base_field_info().

Attributes

#[Hook('entity_base_field_info')]

File

core/modules/system/tests/modules/entity_test/src/Hook/EntityTestHooks.php, line 70

Class

EntityTestHooks
Hook implementations for entity_test.

Namespace

Drupal\entity_test\Hook

Code

public function entityBaseFieldInfo(EntityTypeInterface $entity_type) : array {
  $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($this->t('Field test'))
      ->setDescription($this->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($this->t('Some custom description'))
      ->setTranslatable(TRUE);
  }
  return $fields;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.