trait ImageFieldCreationTrait in Drupal 9
Same name and namespace in other branches
- 8 core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php \Drupal\Tests\image\Kernel\ImageFieldCreationTrait
- 10 core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php \Drupal\Tests\image\Kernel\ImageFieldCreationTrait
Provides a helper method for creating Image fields.
Hierarchy
- trait \Drupal\Tests\image\Kernel\ImageFieldCreationTrait
7 files declare their use of ImageFieldCreationTrait
- FilterTest.php in core/
modules/ jsonapi/ tests/ src/ Kernel/ Query/ FilterTest.php - ImageFieldTestBase.php in core/
modules/ image/ tests/ src/ Functional/ ImageFieldTestBase.php - ImageFieldTestBase.php in core/
modules/ image/ tests/ src/ FunctionalJavascript/ ImageFieldTestBase.php - ImageFieldWidgetMultipleTest.php in core/
modules/ image/ tests/ src/ FunctionalJavascript/ ImageFieldWidgetMultipleTest.php - JsonApiFunctionalTestBase.php in core/
modules/ jsonapi/ tests/ src/ Functional/ JsonApiFunctionalTestBase.php
File
- core/
modules/ image/ tests/ src/ Kernel/ ImageFieldCreationTrait.php, line 11
Namespace
Drupal\Tests\image\KernelView source
trait ImageFieldCreationTrait {
/**
* Create a new image field.
*
* @param string $name
* The name of the new field (all lowercase), exclude the "field_" prefix.
* @param string $type_name
* The node type that this field will be added to.
* @param array $storage_settings
* (optional) A list of field storage settings that will be added to the
* defaults.
* @param array $field_settings
* (optional) A list of instance settings that will be added to the instance
* defaults.
* @param array $widget_settings
* (optional) Widget settings to be added to the widget defaults.
* @param array $formatter_settings
* (optional) Formatter settings to be added to the formatter defaults.
* @param string $description
* (optional) A description for the field. Defaults to ''.
*/
protected function createImageField($name, $type_name, $storage_settings = [], $field_settings = [], $widget_settings = [], $formatter_settings = [], $description = '') {
FieldStorageConfig::create([
'field_name' => $name,
'entity_type' => 'node',
'type' => 'image',
'settings' => $storage_settings,
'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
])
->save();
$field_config = FieldConfig::create([
'field_name' => $name,
'label' => $name,
'entity_type' => 'node',
'bundle' => $type_name,
'required' => !empty($field_settings['required']),
'settings' => $field_settings,
'description' => $description,
]);
$field_config
->save();
/** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', $type_name)
->setComponent($name, [
'type' => 'image_image',
'settings' => $widget_settings,
])
->save();
$display_repository
->getViewDisplay('node', $type_name)
->setComponent($name, [
'type' => 'image',
'settings' => $formatter_settings,
])
->save();
return $field_config;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ImageFieldCreationTrait:: |
protected | function | Create a new image field. |