function ImageFieldCreationTrait::createImageField

Same name in other branches
  1. 8.9.x core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php \Drupal\Tests\image\Kernel\ImageFieldCreationTrait::createImageField()
  2. 10 core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php \Drupal\Tests\image\Kernel\ImageFieldCreationTrait::createImageField()
  3. 11.x core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php \Drupal\Tests\image\Kernel\ImageFieldCreationTrait::createImageField()

Create a new image field.

Parameters

string $name: The name of the new field (all lowercase), exclude the "field_" prefix.

string $type_name: The node type that this field will be added to.

array $storage_settings: (optional) A list of field storage settings that will be added to the defaults.

array $field_settings: (optional) A list of instance settings that will be added to the instance defaults.

array $widget_settings: (optional) Widget settings to be added to the widget defaults.

array $formatter_settings: (optional) Formatter settings to be added to the formatter defaults.

string $description: (optional) A description for the field. Defaults to ''.

28 calls to ImageFieldCreationTrait::createImageField()
FilterTest::savePaintingType in core/modules/jsonapi/tests/src/Kernel/Query/FilterTest.php
Creates a painting node type.
ImageAdminStylesTest::testConfigImport in core/modules/image/tests/src/Functional/ImageAdminStylesTest.php
Tests image style configuration import that does a delete.
ImageAdminStylesTest::testStyleReplacement in core/modules/image/tests/src/Functional/ImageAdminStylesTest.php
Tests deleting a style and choosing a replacement style.
ImageFieldAttributesTest::setUp in core/modules/rdf/tests/src/Functional/ImageFieldAttributesTest.php
ImageFieldDefaultImagesTest::testDefaultImages in core/modules/image/tests/src/Functional/ImageFieldDefaultImagesTest.php
Tests CRUD for fields and field storages with default images.

... See full list

File

core/modules/image/tests/src/Kernel/ImageFieldCreationTrait.php, line 33

Class

ImageFieldCreationTrait
Provides a helper method for creating Image fields.

Namespace

Drupal\Tests\image\Kernel

Code

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;
}

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