You are here

public function FileFieldCreationTrait::attachFileField in Drupal 9

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

Attaches a file field to an entity.

Parameters

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

string $entity_type: The entity type this field will be added to.

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

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

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

1 call to FileFieldCreationTrait::attachFileField()
FileFieldCreationTrait::createFileField in core/modules/file/tests/src/Functional/FileFieldCreationTrait.php
Creates a new file field.

File

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

Class

FileFieldCreationTrait
Provides methods for creating file fields.

Namespace

Drupal\Tests\file\Functional

Code

public function attachFileField($name, $entity_type, $bundle, $field_settings = [], $widget_settings = []) {
  $field = [
    'field_name' => $name,
    'label' => $name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'required' => !empty($field_settings['required']),
    'settings' => $field_settings,
  ];
  FieldConfig::create($field)
    ->save();
  \Drupal::service('entity_display.repository')
    ->getFormDisplay($entity_type, $bundle)
    ->setComponent($name, [
    'type' => 'file_generic',
    'settings' => $widget_settings,
  ])
    ->save();

  // Assign display settings.
  \Drupal::service('entity_display.repository')
    ->getViewDisplay($entity_type, $bundle)
    ->setComponent($name, [
    'label' => 'hidden',
    'type' => 'file_default',
  ])
    ->save();
}