You are here

protected function ImageReplaceTestBase::createImageField in Image Replace 8

Same name and namespace in other branches
  1. 7 src/Tests/ImageReplaceTestBase.php \Drupal\image_replace\Tests\ImageReplaceTestBase::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: A list of field storage settings that will be added to the defaults.

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

array $widget_settings: A list of widget settings that will be added to the widget defaults.

2 calls to ImageReplaceTestBase::createImageField()
AdminTest::setUp in src/Tests/AdminTest.php
EntityTest::setUp in src/Tests/EntityTest.php

File

src/Tests/ImageReplaceTestBase.php, line 36

Class

ImageReplaceTestBase
Tests functionality of the replace image effect.

Namespace

Drupal\image_replace\Tests

Code

protected function createImageField($name, $type_name, array $storage_settings = [], array $field_settings = [], array $widget_settings = []) {
  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,
  ]);
  $field_config
    ->save();
  $entity_display_repository = \Drupal::service('entity_display.repository');
  $entity_display_repository
    ->getFormDisplay('node', $type_name, 'default')
    ->setComponent($name, [
    'type' => 'image_image',
    'settings' => $widget_settings,
  ])
    ->save();
  $entity_display_repository
    ->getViewDisplay('node', $type_name, 'default')
    ->setComponent($name)
    ->save();
  return $field_config;
}