You are here

public function FileFieldCreationTrait::createFileField in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/Functional/FileFieldCreationTrait.php \Drupal\Tests\file\Functional\FileFieldCreationTrait::createFileField()
  2. 9 core/modules/file/tests/src/Functional/FileFieldCreationTrait.php \Drupal\Tests\file\Functional\FileFieldCreationTrait::createFileField()

Creates a new file field.

Parameters

string $name: The name of the new field (all lowercase), exclude the "field_" prefix.

string $entity_type: The entity type.

string $bundle: The bundle that this field will be added to.

array $storage_settings: A list of field storage settings that will be added to the defaults.

array $field_settings: A list of instance settings that will be added to the instance defaults.

array $widget_settings: A list of widget settings that will be added to the widget defaults.

Return value

\Drupal\field\FieldStorageConfigInterface The file field.

File

core/modules/file/tests/src/Functional/FileFieldCreationTrait.php, line 32

Class

FileFieldCreationTrait
Provides methods for creating file fields.

Namespace

Drupal\Tests\file\Functional

Code

public function createFileField($name, $entity_type, $bundle, $storage_settings = [], $field_settings = [], $widget_settings = []) {
  $field_storage = FieldStorageConfig::create([
    'entity_type' => $entity_type,
    'field_name' => $name,
    'type' => 'file',
    'settings' => $storage_settings,
    'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
  ]);
  $field_storage
    ->save();
  $this
    ->attachFileField($name, $entity_type, $bundle, $field_settings, $widget_settings);
  return $field_storage;
}