You are here

protected function TestImageFieldTrait::createImageField in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 tests/src/Traits/TestImageFieldTrait.php \Drupal\Tests\fillpdf\Traits\TestImageFieldTrait::createImageField()

Create a new image field.

This is our own version of ImageFieldTestBase::createImageField() only that it supports fieldable entity types other than nodes.

@todo Switch back to the original once the Core version works with other fieldable entities.

Parameters

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

string $entity_type: The entity type this field will be added to.

string $bundle: The bundle 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\field\Entity\FieldConfig The field configuration.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

See also

https://www.drupal.org/project/drupal/issues/3057070

\Drupal\Tests\image\Functional\ImageFieldTestBase::createImageField()

1 call to TestImageFieldTrait::createImageField()
PdfPopulationTest::testImageStamping in tests/src/Functional/PdfPopulationTest.php
Tests Core image stamping.

File

tests/src/Traits/TestImageFieldTrait.php, line 101

Class

TestImageFieldTrait
Provides helper methods for creating Image fields.

Namespace

Drupal\Tests\fillpdf\Traits

Code

protected function createImageField($field_name, $entity_type, $bundle, array $storage_settings = [], array $field_settings = [], array $widget_settings = [], array $formatter_settings = [], $description = '') {
  if (!isset($this->container) || !$this->container instanceof ContainerInterface) {
    throw new \LogicException(__TRAIT__ . '::' . __METHOD__ . ' called without the container being available.');
  }
  FieldStorageConfig::create([
    'field_name' => $field_name,
    'entity_type' => $entity_type,
    'type' => 'image',
    'settings' => $storage_settings,
    'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
  ])
    ->save();

  /** @var \Drupal\field\Entity\FieldConfig $field_config */
  $field_config = FieldConfig::create([
    'field_name' => $field_name,
    'label' => $field_name,
    'entity_type' => $entity_type,
    'bundle' => $bundle,
    'required' => !empty($field_settings['required']),
    'settings' => $field_settings,
    'description' => $description,
  ]);
  $field_config
    ->save();

  /** @var \Symfony\Component\DependencyInjection\ContainerInterface $container */
  $container = $this->container;

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
  $entity_type_manager = $container
    ->get('entity_type.manager');
  $form_values = [
    'targetEntityType' => $entity_type,
    'bundle' => $bundle,
    'mode' => 'default',
    'status' => TRUE,
  ];
  $entity_form_display = $entity_type_manager
    ->getStorage('entity_form_display');

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
  if (!($form_display = $entity_form_display
    ->load("{$entity_type}.{$bundle}.default"))) {
    $form_display = $entity_form_display
      ->create($form_values);
  }
  $form_display
    ->setComponent($field_name, [
    'type' => 'image_image',
    'settings' => $widget_settings,
  ])
    ->save();
  $view_values = [
    'targetEntityType' => $entity_type,
    'bundle' => $bundle,
    'mode' => 'default',
    'status' => TRUE,
  ];
  $entity_view_display = $entity_type_manager
    ->getStorage('entity_view_display');

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display */
  if (!($display = $entity_view_display
    ->load("{$entity_type}.{$bundle}.default"))) {
    $display = $entity_view_display
      ->create($view_values);
  }
  $display
    ->setComponent($field_name, [
    'type' => 'image',
    'settings' => $formatter_settings,
  ])
    ->save();
  return $field_config;
}