trait FileFieldCreationTrait
Same name in other branches
- 8.9.x core/modules/file/tests/src/Functional/FileFieldCreationTrait.php \Drupal\Tests\file\Functional\FileFieldCreationTrait
- 10 core/modules/file/tests/src/Functional/FileFieldCreationTrait.php \Drupal\Tests\file\Functional\FileFieldCreationTrait
- 11.x core/modules/file/tests/src/Functional/FileFieldCreationTrait.php \Drupal\Tests\file\Functional\FileFieldCreationTrait
Provides methods for creating file fields.
Hierarchy
- trait \Drupal\Tests\file\Functional\FileFieldCreationTrait
6 files declare their use of FileFieldCreationTrait
- DrupalSelenium2DriverTest.php in core/
tests/ Drupal/ FunctionalJavascriptTests/ Tests/ DrupalSelenium2DriverTest.php - FileFieldValidateTest.php in core/
modules/ file/ tests/ src/ FunctionalJavascript/ FileFieldValidateTest.php - FileFieldWidgetTest.php in core/
modules/ file/ tests/ src/ FunctionalJavascript/ FileFieldWidgetTest.php - InlineBlockPrivateFilesTest.php in core/
modules/ layout_builder/ tests/ src/ FunctionalJavascript/ InlineBlockPrivateFilesTest.php - MaximumFileSizeExceededUploadTest.php in core/
modules/ file/ tests/ src/ FunctionalJavascript/ MaximumFileSizeExceededUploadTest.php
File
-
core/
modules/ file/ tests/ src/ Functional/ FileFieldCreationTrait.php, line 11
Namespace
Drupal\Tests\file\FunctionalView source
trait FileFieldCreationTrait {
/**
* Creates a new file field.
*
* @param string $name
* The name of the new field (all lowercase), exclude the "field_" prefix.
* @param string $entity_type
* The entity type.
* @param string $bundle
* The bundle that this field will be added to.
* @param array $storage_settings
* A list of field storage settings that will be added to the defaults.
* @param array $field_settings
* A list of instance settings that will be added to the instance defaults.
* @param array $widget_settings
* A list of widget settings that will be added to the widget defaults.
*
* @return \Drupal\field\FieldStorageConfigInterface
* The file field.
*/
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;
}
/**
* Attaches a file field to an entity.
*
* @param string $name
* The name of the new field (all lowercase), exclude the "field_" prefix.
* @param string $entity_type
* The entity type this field will be added to.
* @param string $bundle
* The bundle this field will be added to.
* @param array $field_settings
* A list of field settings that will be added to the defaults.
* @param array $widget_settings
* A list of widget settings that will be added to the widget defaults.
*/
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();
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
FileFieldCreationTrait::attachFileField | public | function | Attaches a file field to an entity. |
FileFieldCreationTrait::createFileField | public | function | Creates a new file field. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.