You are here

public function FieldCreationTrait::attachMinisiteField in Mini site 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 FieldCreationTrait::attachMinisiteField()
FieldCreationTrait::createMinisiteField in tests/src/Functional/FieldCreationTrait.php
Creates a new ministe field.

File

tests/src/Functional/FieldCreationTrait.php, line 63

Class

FieldCreationTrait
Provides methods for creating minisite fields.

Namespace

Drupal\Tests\minisite\Functional

Code

public function attachMinisiteField($name, $entity_type, $bundle, array $field_settings = [], array $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();
  LegacyWrapper::getFormDisplay($entity_type, $bundle, 'default')
    ->setComponent($name, [
    'type' => 'file_generic',
    'settings' => $widget_settings,
  ])
    ->save();

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