You are here

public function FileFieldTestBase::attachFileField in Drupal 8

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 FileFieldTestBase::attachFileField()
FileFieldTestBase::createFileField in core/modules/file/src/Tests/FileFieldTestBase.php
Creates a new file field.

File

core/modules/file/src/Tests/FileFieldTestBase.php, line 110

Class

FileFieldTestBase
Provides methods specifically for testing File module's field handling.

Namespace

Drupal\file\Tests

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();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
  $display_repository = \Drupal::service('entity_display.repository');
  $display_repository
    ->getFormDisplay($entity_type, $bundle)
    ->setComponent($name, [
    'type' => 'file_generic',
    'settings' => $widget_settings,
  ])
    ->save();

  // Assign display settings.
  $display_repository
    ->getViewDisplay($entity_type, $bundle)
    ->setComponent($name, [
    'label' => 'hidden',
    'type' => 'file_default',
  ])
    ->save();
}