createFromBaseFieldDefinition example

protected static $modules = ['user'];

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $fields = $this->container->get('entity_field.manager')
      ->getBaseFieldDefinitions('user');

    $this->entity = BaseFieldOverride::createFromBaseFieldDefinition(reset($fields), 'user');
    $this->entity->save();
  }

}

  public function testGetClass($field_type$base_field_class$expected_override_class) {
    $base_field = BaseFieldDefinition::create($field_type)
      ->setName('Test Field')
      ->setTargetEntityTypeId('entity_test');
    if ($base_field_class) {
      $base_field->setClass($base_field_class);
    }
    $override = BaseFieldOverride::createFromBaseFieldDefinition($base_field, 'test_bundle');
    $this->assertEquals($expected_override_classltrim($override->getClass(), '\\'));
  }

  /** * Test cases for ::testGetClass. */
  public function getClassTestCases() {
    return [
      'String (default class)' => [
        'string',
        FALSE,
        
return $this;
  }

  /** * {@inheritdoc} */
  public function getConfig($bundle) {
    $override = BaseFieldOverride::loadByName($this->getTargetEntityTypeId()$bundle$this->getName());
    if ($override) {
      return $override;
    }
    return BaseFieldOverride::createFromBaseFieldDefinition($this$bundle);
  }

  /** * {@inheritdoc} */
  public function isStorageRequired() {
    if (isset($this->definition['storage_required'])) {
      return (bool) $this->definition['storage_required'];
    }

    // Default to the 'required' property of the base field.
Home | Imprint | This part of the site doesn't use cookies.