You are here

protected function ImageFieldTokensTestingTrait::createImageFieldTokensField in ImageField Tokens 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ImageFieldTokensTestingTrait.php \Drupal\Tests\imagefield_tokens\Functional\ImageFieldTokensTestingTrait::createImageFieldTokensField()

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 ''.

Return value

\Drupal\Core\Entity\EntityInterface|\Drupal\field\Entity\FieldConfig Returns field config object.

Throws

\Drupal\Core\Entity\EntityStorageException

2 calls to ImageFieldTokensTestingTrait::createImageFieldTokensField()
ImageFieldTokensFormatterTest::testImageFieldFormatters in tests/src/Functional/ImageFieldTokensFormatterTest.php
Test image formatters on node display.
ImageFieldTokensWidgetTest::testWidgetElement in tests/src/Functional/ImageFieldTokensWidgetTest.php
Tests file widget element.

File

tests/src/Functional/ImageFieldTokensTestingTrait.php, line 43

Class

ImageFieldTokensTestingTrait
Provides a helper method for creating Image fields.

Namespace

Drupal\Tests\imagefield_tokens\Functional

Code

protected function createImageFieldTokensField($name, $type_name, array $storage_settings = [], array $field_settings = [], array $widget_settings = [], array $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();
  $this
    ->entityGetFormDisplay('node', $type_name, 'default')
    ->setComponent($name, [
    'type' => 'imagefield_tokens',
    'settings' => $widget_settings,
  ])
    ->save();
  $this
    ->entityGetDisplay('node', $type_name, 'default')
    ->setComponent($name, [
    'type' => 'image',
    'settings' => $formatter_settings,
  ])
    ->save();
  return $field_config;
}