function FileFieldCreationTrait::attachFileField

Same name in other branches
  1. 9 core/modules/file/tests/src/Functional/FileFieldCreationTrait.php \Drupal\Tests\file\Functional\FileFieldCreationTrait::attachFileField()
  2. 8.9.x core/modules/file/tests/src/Functional/FileFieldCreationTrait.php \Drupal\Tests\file\Functional\FileFieldCreationTrait::attachFileField()
  3. 11.x 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). The Field UI 'field_' prefix is not added to the field name.

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 64

Class

FileFieldCreationTrait
Provides methods for creating file fields.

Namespace

Drupal\Tests\file\Functional

Code

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

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