You are here

public function FileFieldPathsTestBase::createImageField in File (Field) Paths 8

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 $third_party_settings: Third party settings.

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

Return value

\Drupal\Core\Entity\EntityInterface A image field configuration entity.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

1 call to FileFieldPathsTestBase::createImageField()
FileFieldPathsUpdateTest::testRetroBasic in tests/src/Functional/FileFieldPathsUpdateTest.php
Test basic Retroactive updates functionality.

File

tests/src/Functional/FileFieldPathsTestBase.php, line 148

Class

FileFieldPathsTestBase
Base class for File (Field) Paths tests.

Namespace

Drupal\Tests\filefield_paths\Functional

Code

public function createImageField($name, $type_name, array $storage_settings = [], array $field_settings = [], array $third_party_settings = [], array $widget_settings = []) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $field_storage = $entity_type_manager
    ->getStorage('field_storage_config')
    ->create([
    'field_name' => $name,
    'entity_type' => 'node',
    'type' => 'image',
    'settings' => $storage_settings,
    'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
  ]);
  $field_storage
    ->save();
  $field_config = $entity_type_manager
    ->getStorage('field_config')
    ->create([
    'field_name' => $name,
    'label' => $name,
    'entity_type' => 'node',
    'bundle' => $type_name,
    'required' => !empty($field_settings['required']),
    'settings' => $field_settings,
    'third_party_settings' => $third_party_settings,
  ]);
  $field_config
    ->save();

  /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $edr */
  $edr = \Drupal::service('entity_display.repository');
  $edr
    ->getFormDisplay('node', $type_name, 'default')
    ->setComponent($name, [
    'type' => 'image_image',
    'settings' => $widget_settings,
  ])
    ->save();
  $edr
    ->getViewDisplay('node', $type_name, 'default')
    ->setComponent($name)
    ->save();
  $this
    ->drupalGet("admin/structure/types/manage/{$this->contentType}/fields/node.{$this->contentType}.{$name}");
  $this
    ->submitForm([], 'Save settings');
  return $field_config;
}